Skip to main content
 首页 » 编程设计

python之在 Bokeh 0.5.0 中,无法指定图表的大小,并且以前的版本出现在同一个输出文件中

2025年05月04日10dflying

“以前的版本”问题与 Updated Bokeh to 0.5.0, now plots all previous versions of graph in one window 中所述的问题相同.我在 iPython notebook 中运行它;每次我重新运行单元格时,输出的 html 文件都会显示所有以前的版本,加上新版本。

另一个问题是我无法在输出文件中指定绘图的大小和/或纵横比。

这是整个脚本:

x = [0.4, 0.6, 0.8, 0.5] 
y = [0.8, 0.5, 0.8, 0.9] 
import bokeh.plotting as bplt 
bplt.output_file('output.html') 
bplt.figure(tools="wheel_zoom", width=1000, height=3000) 
bplt.hold() 
bplt.circle(x, y, color='red',  
       line_color='black', fill_alpha = 0.8, size = 10, 
       title = 'Ternary plot', background_fill='#dddddd') 
bplt.line(x=[0,0.5],y=[1,0.134]) 
bplt.line(x=[0.5,1],y=[0.134,1]) 
bplt.line(x=[1,0],y=[1,1]) 
bplt.show() 

无论我为宽度和高度设置什么值,输出总是相同的大小和纵横比。

请您参考如下方法:

width 和 height 必须从字形函数(如 circle 等)中删除作为设置绘图宽度和高度的选项因为一些字形也有它们自己的 width 和 height 属性并且存在冲突。但是,它可能应该保留用于 figure 调用,作为 plot_width 和 plot_height 的别名。我在 figure 中添加了一张票以将这些恢复为别名:

https://github.com/ContinuumIO/bokeh/issues/897

与此同时,您可以将 plot_width 和 plot_height 传递给 figure(...) ,它会自动调整绘图的大小你要。

如果要清除plotting.py session 状态,reset_output() 是在0.5.1 中添加的。这是一个完整的工作脚本:

x = [0.4, 0.6, 0.8, 0.5] 
y = [0.8, 0.5, 0.8, 0.9] 
import bokeh.plotting as bplt 
bplt.reset_output() 
bplt.output_file('output.html') 
bplt.figure(tools="wheel_zoom", plot_width=100, plot_height=300) 
bplt.hold() 
bplt.circle(x, y, color='red',  
   line_color='black', fill_alpha = 0.8, size = 10, 
   title = 'Ternary plot', background_fill='#dddddd') 
bplt.line(x=[0,0.5],y=[1,0.134]) 
bplt.line(x=[0.5,1],y=[0.134,1]) 
bplt.line(x=[1,0],y=[1,1]) 
bplt.show()