【问题标题】:How to center the title in a grouped bar chart with altair如何使用 altair 在分组条形图中将标题居中
【发布时间】:2019-09-05 05:00:42
【问题描述】:

我正在尝试使标题在分组条形图中居中。

通过阅读文档(),我认为我可以通过在 configure_title 方法中指定 orient 和 anchor 参数来做到这一点。

但是,这似乎对我不起作用。我为锚参数尝试了几个值,但看起来它没有应用并坚持其默认值。

我有什么遗漏吗?

我正在使用 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

标题保留在左上角,而不是在中上。

【问题讨论】:

    标签: python altair


    【解决方案1】:

    这是 Vega-Lite 2 中的一个问题,已在 Vega-Lite 3 中修复。更新到 Altair 版本 3 后,它应该可以按预期工作,而无需对代码进行任何更改:

    【讨论】:

    • 亲爱的杰克,感谢您的迅速回复!将altair更新到版本3有什么技巧吗?我正在使用 conda,但找不到实现此目的的方法。我尝试使用我的环境(python 3.6.6)以及使用 conda-forge 通道的带有 python 3.7.3 的新环境。我也无法升级 vega(仍然是 2.0.1)。
    • 目前您可以使用pip install altair==3.0.0rc1 安装预发行版。最终的 3.0 版本即将发布。
    • 但是请注意,您需要更新前端扩展程序(jupyterlab、笔记本等)以与 vega-lite 3 兼容。
    • 再次感谢 Jake,但我注意到我的环境有一个奇怪的行为(Kubuntu 18.04 上的 python 3.7.3)......我将 jupyterlab 更新到最新版本(0.35.4)和 altair 到版本 3.0 python 3.7.3 上的 .0rc1。但是,当我导入 altair 时,我仍然会使用 alt.__version__ (2.4.1) 显示旧版本,并且标题仍然没有与情节的中间对齐。使用 conda list 或直接在终端中显示正确版本的 altair (3.0.0rc1),所以我想我缺少一个额外的步骤来使 jupyterlab 与 vega-lite 3 兼容?
    • 听起来您将 altair 3 安装在与您在 jupterlab 中使用的内核不同的位置(确保您正在运行 pip install 并且 pip 连接到您在 Jupyterlab 中使用的 python 内核;请参阅stackoverflow.com/questions/39007571/…)。此外,一旦弄清楚这一点,您将需要 jupyterlab 1.0 预发行版才能查看 Altair 3 绘图(这是我们尚未发布 Altair 3 的原因之一)。
    猜你喜欢
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2016-04-26
    • 2018-02-10
    • 2019-05-10
    相关资源
    最近更新 更多