简体   繁体   English

Python Bokeh:在gridplot中多次绘制相同的图表

[英]Python Bokeh: Plotting same chart multiple times in gridplot

I'm currently trying to get an overview of plots of data of different dates. 我目前正在尝试概述不同日期的数据图。 To get a good feeling of the data I would like to plot relevant plots next to each other. 为了获得对数据的良好感觉,我想将相关的图绘制在一起。 This means I want to use the same plot multiple times in the gridplot command. 这意味着我想在gridplot命令中多次使用相同的图。 However what I noticed is that when i use the same chart multiple times it will only show it once in the final .html file. 然而我注意到的是,当我多次使用相同的图表时,它只会在最终的.html文件中显示一次。 My first attempt at solving this was to use a copy.deepcopy for the charts, but this gave the following error: 我解决这个问题的第一个尝试是对图表使用copy.deepcopy,但这会产生以下错误:

RuntimeError: Cannot get a property value 'label' from a LineGlyph instance before HasProps.__init__

My approach has been as follows: 我的方法如下:

from bokeh.charts import Line, output_file, show, gridplot
import pandas as pd
output_file('test.html')
plots = []

df = pd.DataFrame([[1,2], [3,1], [2,2]])
print(df)
df.columns = ['x', 'y']
for i in range(10):
    plots.append(Line(df, x='x', y='y', title='Forecast: ' + str(i),
                 plot_width=250, plot_height=250))

plot_matrix = []
for i in range(len(plots)-1, 2, -1):
    plot_matrix.append([plots[i-3], plots[i-2], plots[i]])
p = gridplot(plot_matrix)
show(p)

The results of which is a an html page with a grid plot with a lot of missing graphs. 其结果是一个带有网格图的html页面,其中包含大量缺失的图形。 Each graph is exactly shown once (instead of the 3 times required), which leads me to think that the gridplot does not like me using the same object multiple times. 每个图形都精确显示一次(而不是需要的3次),这使我认为网格图不喜欢我多次使用同一个对象。 An obvious solve is to simply create every graph 3 times as a different object, which I will do for now, but not only is this inefficient, it also hurts my eyes when looking at my code. 一个明显的解决方案是简单地将每个图形作为一个不同的对象创建3次,我现在要做的,但这不仅效率低,而且在查看我的代码时也会伤到我的眼睛。 I'm hoping somebody has a more elegant solution for my problem. 我希望有人能为我的问题找到更优雅的解决方案。

EDIT: made code runable 编辑:使代码可运行

This is not possible. 这是不可能的。 Bokeh plots (or Bokeh objects in general) may not be re-used in layouts. Bokeh图(或一般的Bokeh对象)可能无法在布局中重复使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM