python - Python Bokeh:在缩放时将 X 轴重新启动为 0
问题描述
我在下面的代码创建了一个简单的 xy 线图。
当我放大时,我希望 x 轴代码再次从 0 开始,而不是 3.9/无论缩放的 x 点在图像中是什么。
无缩放:
缩放后:
我怎么做?
代码:
from bokeh.io import output_file, show, save
from bokeh.layouts import column
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
data = []
x = list(range(11))
y0 = x
y1 = [10 - xx for xx in x]
y2 = [abs(xx - 5) for xx in x]
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1, y2=y2))
for i in range(3):
p = figure(title="Title " + str(i), plot_width=300, plot_height=300)
if len(data):
p.x_range = data[0].x_range
p.y_range = data[0].y_range
p.circle('x', 'y0', size=10, color="navy", alpha=0.5, legend_label='line1', source=source)
p.legend.location = 'top_right'
p.legend.click_policy = "hide"
data.append(p)
plot_col = column(data)
# show the results
show(plot_col)
解决方案
这是一个不寻常的要求,没有一个内置的东西会以这种方式运行。如果放大到区间[4,7],范围将更新[4, 7],因此轴将显示[4, 7]的标签。如果只需显示不同的刻度标签就足够了,即使基础范围开始/结束保持其通常的值,那么您可以使用自定义扩展来生成您想要的任何自定义标签。用户指南中有一个示例已经几乎完全符合您的要求:
您还可以使用FuncTickFormatter
, 例如(未经测试)做一些更简单的事情
p.xaxis.formatter = FuncTickFormatter(code="""
return tick - ticks[0]
""")
推荐阅读
- amazon-web-services - 确定 API 密钥所属的使用计划 - AWS API Gateway
- rust - 将字符串剥离成行并在 Rust 的 char 范围内找到特定行
- python - 有没有办法可以添加一个类的两个实例?
- airflow - 在 Airflow 中执行 Talend Job .sh 文件 - Bash 命令失败时出错
- google-sheets - 需要配方解决方案
- python - 从行和列到列和行的顺序矩阵
- r - R:具有非 NA 值的子集行 dplyr
- python - 安装 mayavi 错误,退出状态为 -11
- java - 如何在 DTO 中隐藏 JsonProperty?
- sql - 根据 DB2 中的今天日期获取最近十二个月的记录