python - Altair 并排分组条形图而不是单个图表

标签 python data-visualization altair

我尝试创建图表,但最终并排显示两个图表。 如何获得单个图表,而不是像 Altair 分组条形图 example 中显示的那样.

df = pd.DataFrame({
    'strike': [200,225,250,275,300,325,350,200,225,250,275,300,325,350],
    'opttype': ['ce','ce','ce','ce','ce','ce','ce','pe','pe','pe','pe','pe','pe','pe' ],
    'oi': [100,150,500,800,450,200,77,50,500,210,300,150,60,17]
    
})
c = alt.Chart(df).mark_bar().encode(
    x='strike:N',
    y='oi:Q',
    color='opttype:N',
    column='opttype:N'
)

输出 output

我尝试按照此 link 中的指定添加轴,但是我无法获得组合图。也许我在这里遗漏了一些东西。

我还想知道官方文档示例如何在没有“轴”的情况下正确工作。

最佳答案

当前最好的方法是切换 xcolumn 编码,然后使每个面板的宽度变窄:

c = alt.Chart(df).mark_bar().encode(
    x='opttype:N',
    y='oi:Q',
    color='opttype:N',
    column='strike:N'
).properties(width=50)

enter image description here

然后,如果您愿意,您可以调整轴和标题标签,如链接示例中所示。例如,您可以将标题标签移至底部,如下所示:

c = alt.Chart(df).mark_bar().encode(
    x=alt.X('opttype:N', axis=None),
    y='oi:Q',
    color='opttype:N',
    column=alt.Column('strike:N', header=alt.Header(orient='bottom'))
).properties(width=50)

enter image description here

这无疑是一个不完美的解决方案。 Vega-Lite 正在添加 offset 编码,这将使这成为可能,而无需使用多个列面,但这尚不可用。

关于python - Altair 并排分组条形图而不是单个图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70024520/

相关文章:

python - 从 Flask-Mail 发送邮件(SMTPSenderRefused 530)

python - 如何可视化 DGL 数据集中的图表?

python - 你如何在 Altair 中设置 Axis FontSize?

python - 我做了一个初学者项目,创建了一个计算器,并推送到了 Github。我在哪里可以找到我的代码?

Python:用 3D bool 索引 3D 数组并返回相同大小的 3D 数组......优雅

r - 如何创建序列对象中组的子集的序列索引图?

python - 根据不同列中的另外 2 个值求和一列的值

python - Altair:如何向包含行的条形图添加标签

python - 堆积条形图。创建一个包含所有类别信息的集成工具提示 (Altair)

python - Json 搜索任意字段