散景中带有两个 y 轴“计数”和“百分比”的直方图?

问题描述 投票:0回答:1

我是散景新手。在散景中创建数据直方图(有两个 y 轴,一个是“计数”,一个是“百分比”)的简洁方法是什么?我使用四边形创建带有“count”轴标签的直方图,然后使用 extra_y_ranges 和 add_layout 添加第二个轴。不幸的是,y 轴并不总是对齐的。我确信有更好的方法来做到这一点。另外,如何旋转右侧 y_axis 标签?我希望它的方向与左 y 轴相同。 我最终也喜欢覆盖发行版的 pdf。

感谢您的帮助。

这是我用“quad”和 extra_y_ranges 创建的图,我明白了这一点,但不喜欢“百分比”的方向。

python histogram bokeh orientation
1个回答
0
投票

我的建议是

  1. 根据您的数据将
    Range1d
    bokeh.models
    设置到您的两个 y 轴
  2. 绘制
    quad
    两次,第二个 y 轴不带任何颜色
  3. 使用
    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)

© www.soinside.com 2019 - 2024. All rights reserved.