我是散景新手。在散景中创建数据直方图(有两个 y 轴,一个是“计数”,一个是“百分比”)的简洁方法是什么?我使用四边形创建带有“count”轴标签的直方图,然后使用 extra_y_ranges 和 add_layout 添加第二个轴。不幸的是,y 轴并不总是对齐的。我确信有更好的方法来做到这一点。另外,如何旋转右侧 y_axis 标签?我希望它的方向与左 y 轴相同。 我最终也喜欢覆盖发行版的 pdf。
感谢您的帮助。
我的建议是
Range1d
从 bokeh.models
设置到您的两个 y 轴quad
两次,第二个 y 轴不带任何颜色axis_label_orientation
设置标签的方向这是一个最小的例子:
这是存储在 pandas DataFrame 中的非常小的数据集
import pandas as pd
df = pd.DataFrame({'data':[12,1,2,11,22,3,4]})
df['percent'] = df['data'] / df["data"].sum()
df['left'] = df.index - 0.4
df['right'] = df.index + 0.4
散景部分如下所示。
from bokeh.models import ColumnDataSource, LinearAxis, Range1d
from bokeh.plotting import figure, show, output_notebook
output_notebook()
source= ColumnDataSource(df)
# figure
p = figure(width=700, height=300)
# default axis
p.quad(top='data', bottom=0, left='left', right='right', source=source)
p.yaxis.axis_label = "count"
p.yaxis.axis_label_orientation = 'horizontal'
p.y_range = Range1d(0, df['data'].max() * 1.05)
# second axis
p.extra_y_ranges['percent'] = Range1d(0, df['percent'].max() * 1.05)
red_circles = p.quad(
top='data',
bottom=0,
left='left',
right='right',
source=source,
color=None,
y_range_name="percent"
)
ax2 = LinearAxis(
axis_label="percent",
y_range_name="percent",
axis_label_orientation = 'horizontal'
)
p.add_layout(ax2, 'right')
# output
show(p)