初始化y轴以0为中心

时间:2022-11-29 18:57:07

I'm generating some line plots that show percent error results. It would be nice if the y-axis was centered at 0 when the plots first load. I know I can manually set the bounds for the y-axis, but it would be tedious to have to set them manually for each figure.

我正在生成一些显示百分比错误结果的线图。如果在第一次加载时y轴以0为中心,那将是很好的。我知道我可以手动设置y轴的边界,但是必须为每个图形手动设置它们是很繁琐的。

Is there a way to set the figures to center at 0 along the y-axis?

有没有办法将数字设置为沿y轴居中为0?

Here's some code that may provide additional details -

以下是一些可能提供其他详细信息的代码 -

from bokeh.plotting import figure, output_file, show
from bokeh.io import gridplot, hplot

# prepare some data
x = [1, 2, 3, 4, 5]
y1 = [-10, -9, 23, 4, -6]
y2 = [-15, -4, 26, 32, -45]
y3 = [-42, -20, -13, -34, -59]
y4 = [-23, -34, -32, -43, -53]

# output to static HTML file
output_file("lines.html", title="line plot example")

# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
p.line(x, y1, line_width=2)
p.line(x, y2, line_width=2)

p2 = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
p2.line(x, y3, line_width=2)
p2.line(x, y4, line_width=2)

p = hplot(p, p2)
show(p)

This generates two plots. The one on the right shows the problem I'm running into. Since all of the values are negative, the y-axis bounds are narrowed to approximately -10 to -60. I would like that chart to be bound at -60 to 60, so that it's aligned at 0.

这会生成两个图。右边的那个显示了我遇到的问题。由于所有值都是负的,因此y轴边界变窄到大约-10到-60。我希望该图表在-60到60之间绑定,以便它在0处对齐。

Update: I ended up just defining a function that will return the absolute max of the y values. Then, I am doing the following to set the limits:

更新:我最后只定义了一个函数,它将返回y值的绝对最大值。然后,我正在执行以下操作来设置限制:

axisLimit = getAxisRange(list1, list2) #get absolute max from the two lists
p.y_range = Range1d(start=-1*axisLimit, end=axisLimit)

1 个解决方案

#1


0  

There's no Bokeh method to center a plot around an axis like you describe. What did you did in your update seems like a good solution. You're always welcome to open an issue on the Bokeh project tracker at https://github.com/bokeh/bokeh/issues

没有Bokeh方法可以像您描述的那样围绕轴绘图。你在更新中做了什么似乎是一个很好的解决方案。欢迎您随时在Bokeh项目跟踪器上打开一个问题:https://github.com/bokeh/bokeh/issues

#1


0  

There's no Bokeh method to center a plot around an axis like you describe. What did you did in your update seems like a good solution. You're always welcome to open an issue on the Bokeh project tracker at https://github.com/bokeh/bokeh/issues

没有Bokeh方法可以像您描述的那样围绕轴绘图。你在更新中做了什么似乎是一个很好的解决方案。欢迎您随时在Bokeh项目跟踪器上打开一个问题:https://github.com/bokeh/bokeh/issues