将散景图导出为图像

时间:2019-07-04 16:31:51

标签: python python-3.x powerbi bokeh pandas-bokeh

我有一块示例Python,使瀑布可视化。 它使用了bokeh lib

它看起来很棒,在Jupyter中也能很好地工作,但是当我在PowerBI中使用它时,出现一个错误,提示未创建任何图像

代码使用show(p),当我在PowerBI中运行它时似乎打开了Internet Explorer页面

我尝试了一个matplotlib示例,它使用了: my_plot.get_figure()。savefig(“ waterfall.png”,dpi = 200,bbox_inches ='tight') 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)

1 个答案:

答案 0 :(得分:0)

《用户指南》有一章专门介绍Exporting Plots

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

请注意,您将需要安装必要的可选依赖项(PhantomJS和selenium)。