简体   繁体   English

如何使用altair将标题放在分组的条形图中

[英]How to center the title in a grouped bar chart with altair

I'm trying to center the title in a grouped bar chart. 我正在尝试将标题放在分组的条形图中。

By reading the docs (), I thought I could manage to do this by specifying the orient and anchor parameters in the configure_title method. 通过阅读docs(),我认为我可以通过在configure_title方法中指定orient和anchor参数来设法做到这一点。

This however does not seem to be working for me. 但是,这似乎不适用于我。 I tried several values for the anchor parameter but it looks like it is not applied and sticks to its default value. 我为anchor参数尝试了多个值,但看起来好像没有应用,并坚持使用其默认值。

Is there something I'm missing? 有什么我想念的吗?

I am working with the version 2.4.1 of altair. 我正在使用altair的2.4.1版本。

import altair as alt
import pandas as pd


# dataset
df = pd.DataFrame([[1, 'A', 'G1'],
                   [2, 'A', 'G2'],
                   [5, 'B', 'G1'],
                   [10, 'B', 'G2'],
                   [4, 'C', 'G1'],
                   [9, 'C', 'G2'],
                   [1, 'D', 'G1'],
                   [1, 'D', 'G2'],
                   [4, 'E', 'G1'],
                   [9, 'E', 'G2'],
                   [8, 'F', 'G1'],
                   [1, 'F', 'G2'],
                   [2, 'G', 'G1'],
                   [3, 'G', 'G2'],
                   [2, 'H', 'G1'],
                   [1, 'H', 'G2'],
                   [3, 'I', 'G1'],
                   [8, 'I', 'G2'],
                   [7, 'J', 'G1'],
                   [5, 'J', 'G2'],
                  ], columns=['Count', 'Category', 'Group'])


chart = alt.Chart(df, title=f"Count of categories in each group",
         width=50, height=600).mark_bar().encode(   column=alt.Column('Category:O'),
                                                    color=alt.Color('Group', type='nominal', scale=alt.Scale(range=['#1f77b4', '#2ca02c'])),
                                                    x=alt.X(field="Group", type="nominal",
                                                            axis=None,
                                                            title="Category",
                                                           ),
                                                    y=alt.Y(field="Count", type="quantitative",
                                                            axis=alt.Axis(title="Number of records",
                                                                          titlePadding=10.0),
                                                           ),
)
chart = chart.configure_title(fontSize=20, offset=5, orient='top', anchor='middle')

# display the chart
chart

标题左上方而不是中间上方的图表

The title remains at the top left instead of being at the top middle. 标题保持在左上方,而不是在上方中间。

This is an issue in Vega-Lite 2 that has been fixed in Vega-Lite 3. Once you update to Altair version 3, it should work as expected without any change to your code: 这是Vega-Lite 2中的一个问题,已在Vega-Lite 3中修复。更新到Altair版本3后,它应该可以按预期工作,而无需更改代码: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM