python - 如何在 Altair 的 HConcatChart 中配置图表位置

标签 python data-visualization altair

我正在尝试在 Altair 中水平连接两个图表,但我无法让它们看起来像我想要的那样。

它们的样子是这样的:

enter image description here

这是我正在使用的代码:

pick_ausbildung = alt.selection_single(fields = ["Ausbildungsstand"], on = "mouseover")

ausbildung_chart  = alt.Chart(umfrage,
                             title = "Ausbildungsstand").mark_bar().encode(
    y=alt.Y("Ausbildungsstand", axis = alt.Axis(title = None)),
    x="count()",
    color = alt.condition(pick_ausbildung, 
                          alt.Color("Ausbildungsstand:N", 
                                    legend = None), alt.value("lightgrey")),
    tooltip = ["Ausbildungsstand","count()"]).properties(height=200).add_selection(pick_ausbildung)

g_ausbildung_chart = alt.Chart(umfrage).mark_bar().encode(
    x="Geschlecht",
    y="count()",
    color = "Geschlecht",
    tooltip = ["Geschlecht","count()"]).properties(width=300).transform_filter(pick_ausbildung)

ausbildung_chart|g_ausbildung_chart

基本上,我想将图表“Ausbildungsstand”放置在图表区域的中间。我的意思是,我想将它与 Canvas 的上边缘分开。

我可以通过调整图表的高度来获得我想要的结果(如果它们具有相同的高度,则它们是对齐的),但我想知道如何在“ Canvas ”内移动图表.

预先感谢您的帮助。

最佳答案

您可以使用alt.hconcat()函数并传递 center=True。例如:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'label': ['A', 'B', 'C', 'D', 'E'],
    'value': [3, 5, 4, 6, 2],
})

chart1 = alt.Chart(df).mark_bar().encode(y='label', x='value')
chart2 = alt.Chart(df).mark_bar().encode(x='label', y='value')

alt.hconcat(chart1, chart2, center=True)

enter image description here

关于python - 如何在 Altair 的 HConcatChart 中配置图表位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66067285/

相关文章:

r - 在 ggplot2 密度图中说明均值和标准差

javascript - Google Charts - Google Visualization arraytoDataTable 无法识别数值,除非 "treated"

python - 有没有办法动态更改 Altair 中选定点的 zorder?

python - 从 pandas DataFrame 中的日期时间列中提取月份

python-3.x - 如何从 Pandas DataFrame 绘制家谱?

python-3.x - Altair 通过绑定(bind)范围 slider 对日期时间值进行选择和转换过滤器似乎不适用于相等条件或选择器本身

python - Altair:不对轴进行排序

python - 使用机器学习进行简单的机器人控制

python - CherryPy、线程和成员变量;潜在的问题?

python - 使用 Python 多重处理读取大文件