python-3.x - Bokeh:隐藏图例上的某些内容时更新缩放图

标签 python-3.x bokeh

我在 Bokeh 中有以下图表:

enter image description here

我想知道 Bokeh 库中是否有一些命令允许我在隐藏图例上的某些系列时更新 y 轴(或框缩放我的图)。示例:当我隐藏图例中的第一对条形时,我希望结果如下:

enter image description here

最佳答案

这是一个例子:

进口:

from bokeh.models import ColumnDataSource, Legend, CustomJS
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
import numpy as np
output_notebook()

抽奖代码:

x = np.linspace(0, 4*np.pi, 100)
y1 = np.sin(x)
y2 = y1 + 1.2
y3 = 0.1 * x**2
fig = figure(plot_height=250)
source = ColumnDataSource(data=dict(x=x, y1=y1, y2=y2, y3=y3))

line1 = fig.line("x", "y1", source=source, legend="Y1", color="red", line_width=3)
line2 = fig.line("x", "y2", source=source, legend="Y2", color="green", line_width=3)
line3 = fig.line("x", "y3", source=source, legend="Y3", color="blue", line_width=3)

legend = fig.legend[0]
legend.click_policy = "hide"

def callback(fig=fig, legend=fig.legend[0]):
    y_range = fig.y_range
    y_range.have_updated_interactively = False
    y_range.renderers = [item.renderers[0] for item in legend.items if item.renderers[0].visible]
    Bokeh.index[fig.id].plot_canvas_view.update_dataranges()

for item in legend.items:
    item.renderers[0].js_on_change("visible", CustomJS.from_py_func(callback))

show(fig)

结果:

http://nbviewer.jupyter.org/gist/ruoyu0088/8e2d5fb768ee837d3cb59943f944c61f

关于python-3.x - Bokeh:隐藏图例上的某些内容时更新缩放图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48165349/

相关文章:

python - Python套接字服务器无法解码来自OAUTH的重定向

python - 如何在 python 中制作华夫饼图表? (方形饼图)

python - 修复 Bokeh 放大和缩小的边界

python - 如何更改 Bokeh 使用的 Python 版本?

python - 判断一个文件是 "more likely"json还是csv

Python 使用 psycopg2 将 DataFrame 写入 AWS redshift

python - 叠加静态 Bokeh 图

python - 在 Bokeh 中绘制 300-400 个字形的最有效方法是什么?

python - 如何用Python制作一个像C++一样的计算器

Python:列表理解无法正常工作