一个图表在Bokeh中有两个不同的y轴范围?

bre*_*000 38 python bokeh

我想在左侧y轴上显示数量信息的条形图,然后在右侧覆盖带有Yield%的散点图/线图.我可以单独创建这些图表,但不知道如何将它们组合成单个图表.

在matplotlib中,我们将使用twinx(),然后使用yaxis.tick_left()和创建第二个yaxis.tick_right()数字.

有没有办法与Bokeh做类似的事情?

tom*_*kas 45

是的,现在可以在Bokeh图中有两个y轴.下面的代码显示了将第二个y轴设置为通常的图形绘制脚本的重要脚本部分.

# 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)
Run Code Online (Sandbox Code Playgroud)

我们得到的情节看起来像这样:

在此输入图像描述

如果要向第二个轴添加标签,可以通过编辑调用来完成此操作LinearAxis,如下所示:

s1.add_layout(LinearAxis(y_range_name="foo", axis_label='foo label'), 'right')
Run Code Online (Sandbox Code Playgroud)


Ken*_*ers 10

这篇文章帮助我完成了你想要的效果.

以下是该帖子的内容:

from bokeh.plotting import figure, output_file, show
from bokeh.models.ranges import Range1d
import numpy


output_file("line_bar.html")

p = figure(plot_width=400, plot_height=400)

# add a line renderer
p.line([1, 2, 3, 4, 5], [6, 7, 6, 4, 5], line_width=2)

# setting bar values
h = numpy.array([2, 8, 5, 10, 7])

# Correcting the bottom position of the bars to be on the 0 line.
adj_h = h/2

# add bar renderer
p.rect(x=[1, 2, 3, 4, 5], y=adj_h, width=0.4, height=h, color="#CAB2D6")

# Setting the y  axis range   
p.y_range = Range1d(0, 12)

p.title = "Line and Bar"

show(p)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果要将第二个轴添加到绘图中,请执行p.extra_y_ranges上述帖子中所述的操作.还有别的,你应该弄清楚.

例如,在我的项目中,我有这样的代码:

s1 = figure(plot_width=800, plot_height=400, tools=[TOOLS, HoverTool(tooltips=[('Zip', "@zip"),('((Rides/day)/station)/capita', "@height")])],
           title="((Rides/day)/station)/capita in a Zipcode (Apr 2015-Mar 2016)")

y = new_df['rides_per_day_per_station_per_capita']
adjy = new_df['rides_per_day_per_station_per_capita']/2

s1.rect(list(range(len(new_df['zip']))), adjy, width=.9, height=y, color='#f45666')
s1.y_range = Range1d(0, .05)
s1.extra_y_ranges = {"NumStations": Range1d(start=0, end=35)}
s1.add_layout(LinearAxis(y_range_name="NumStations"), 'right')
s1.circle(list(range(len(new_df['zip']))),new_df['station count'], y_range_name='NumStations', color='blue')
show(s1)
Run Code Online (Sandbox Code Playgroud)

结果是:

在此输入图像描述