1

I have a piece of sample Python that makes a waterfall visual. It uses the bokeh lib

It looks great and works well in Jupyter but when I come to use it in PowerBI I get an error saying that no image was created

the code uses show(p) which seems to open an internet explorer page when I run it in PowerBI

I tried a matplotlib example and it uses : my_plot.get_figure().savefig("waterfall.png",dpi=200,bbox_inches='tight') is there something similar for bokeh lib ?

from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.models.formatters import NumeralTickFormatter
import pandas as pd

#output_notebook()

# Create the initial dataframe
index = ['sales','returns','credit fees','rebates','late charges','shipping']
data = {'amount': [350000,-30000,-7500,-25000,95000,-7000]}
df = pd.DataFrame(data=data,index=index)

# Determine the total net value by adding the start and all additional transactions
net = df['amount'].sum()

df['running_total'] = df['amount'].cumsum()
df['y_start'] = df['running_total'] - df['amount']

# Where do we want to place the label?
df['label_pos'] = df['running_total']

df_net = pd.DataFrame.from_records([(net, net, 0, net)],
                                   columns=['amount', 'running_total', 'y_start', 'label_pos'],
                                   index=["net"])
df = df.append(df_net)

df['color'] = 'grey'
df.loc[df.amount < 0, 'color'] = 'red'
df.loc[df.amount > 0, 'color'] = 'green'
df.loc[df.amount > 300000, 'color'] = 'blue'
df.loc[df.amount < 0, 'label_pos'] = df.label_pos - 10000
df["bar_label"] = df["amount"].map('{:,.0f}'.format)

TOOLS = "box_zoom,reset,save"
source = ColumnDataSource(df)
p = figure(tools=TOOLS, x_range=list(df.index), y_range=(0, net+40000),
           plot_width=800, title = "Sales Waterfall")

p.segment(x0='index', y0='y_start', x1="index", y1='running_total',
          source=source, color="color", line_width=55)

p.grid.grid_line_alpha=0.3
p.yaxis[0].formatter = NumeralTickFormatter(format="($ 0 a)")
p.xaxis.axis_label = "Transactions"

labels = LabelSet(x='index', y='label_pos', text='bar_label',
                  text_font_size="8pt", level='glyph',
                  x_offset=-20, y_offset=0, source=source)
p.add_layout(labels)
show(p)
bigreddot
  • 33,642
  • 5
  • 69
  • 122
John
  • 189
  • 1
  • 10

1 Answers1

2

There is a chapter of the User's Guide dedicated to Exporting Plots:

from bokeh.io import export_png
export_png(plot, filename="plot.png")

Note that you will need to have the necessary optional dependencies (PhantomJS and selenium) installed.

bigreddot
  • 33,642
  • 5
  • 69
  • 122
  • Thanks I will give it a go. Do you think PowerBI will pick up the file correctly ? – John Jul 05 '19 at 07:25
  • I don't have any experience with PowerBI so I can't comment. Only that if you are looking for the equivalent of MPL `savefig` this is it. – bigreddot Jul 05 '19 at 17:50
  • I have give it a go today and it doesn't look like PowerBI likes it. Do you know how I could change the colors of the bars in this Matplotlib example (https://pbpython.com/waterfall-chart.html) basically all I need is the first and last bar to be blue then a green if its above 0 and a red if its below. I am very new to Python and this one has got me confused. I can change the colours of all the bars but not different based on value. Any help would be very much appreciated ! – John Jul 05 '19 at 20:54
  • 1
    I am afraid I don't know much about MPL, and it would not be appropriate to answer here in any case. New separate questions should get new separate posts on SO, otherwise it diminishes the value of the site by making information difficult to index and access. – bigreddot Jul 05 '19 at 21:34