在散景中一张带有两个不同y轴范围的图表? [英] One chart with two different y axis ranges in Bokeh?
问题描述
我想要一个在左侧y轴上带有数量"信息的条形图,然后在右侧的收益率%"上覆盖一个散点图/折线图.我可以分别创建每个图表,但是不知道如何将它们组合成一个图.
I would like a Bar chart with Quantity information on the left y-axis, and then overlay a Scatter/Line plot with Yield % on the right. I can create each of these charts separately, but do not know how to combine them into a single plot.
在matplotlib中,我们将使用twinx()
创建第二个图形,然后在各个图形上使用yaxis.tick_left()
和yaxis.tick_right()
.
In matplotlib, we would create a second figure using twinx()
, and then use yaxis.tick_left()
and yaxis.tick_right()
on the respective figures.
是否有一种方法可以与Bokeh做类似的事情?
Is there a method for doing something similar with Bokeh?
推荐答案
是的,现在在散景图中可以有两个y轴. 下面的代码显示了对设置第二个y轴重要的脚本部分 到通常的图形绘制脚本.
Yes, now it is possible to have two y axes in Bokeh plots. The code below shows script parts significant in setting up the second y axis to the usual figure plotting script.
# Modules needed from Bokeh.
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.models import LinearAxis, Range1d
# Seting the params for the first figure.
s1 = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000,
plot_height=600)
# Setting the second y axis range name and range
s1.extra_y_ranges = {"foo": Range1d(start=-100, end=200)}
# Adding the second axis to the plot.
s1.add_layout(LinearAxis(y_range_name="foo"), 'right')
# Setting the rect glyph params for the first graph.
# Using the default y range and y axis here.
s1.rect(df_j.timestamp, mids, w, spans, fill_color="#D5E1DD", line_color="black")
# Setting the rect glyph params for the second graph.
# Using the aditional y range named "foo" and "right" y axis here.
s1.rect(df_j.timestamp, ad_bar_coord, w, bar_span,
fill_color="#D5E1DD", color="green", y_range_name="foo")
# Show the combined graphs with twin y axes.
show(s1)
我们得到的情节看起来像这样:
And the plot we get looks like this:
如果您想在第二个轴上添加标签,则可以通过编辑对
If you want to add a label to the second axis, this can be accomplished by editing the call to LinearAxis
as follows:
s1.add_layout(LinearAxis(y_range_name="foo", axis_label='foo label'), 'right')
这篇关于在散景中一张带有两个不同y轴范围的图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!