在带误差线的 Altair 分层条形图中丢失排序
Sorting lost in Altair layered bar chart with error bars
我正在为我的条形图使用自定义排序并且效果很好。但是,当我想向其添加误差线并使用分层图表时,排序就不再考虑了。我还定义了 axis = None
,这也没有考虑在内。
以下是数据示例:
df = pd.DataFrame(
{'size' : ['huge', 'huge', 'huge', 'huge', 'huge', 'huge', 'big', 'big', 'big', 'big', 'big', 'big', 'small', 'small', 'small', 'small', 'small', 'small'],
'weight': ['10 mg', '10 mg', '10 g', '10 g', '10 kg', '10 kg', '10 mg', '10 mg', '10 g', '10 g', '10 kg', '10 kg','10 mg', '10 mg', '10 g', '10 g', '10 kg', '10 kg'],
'value': [3.5,2.6,5.1,6.5,2.3,4.6,7.1,2.8,6.9,1.5,2.6,2.8,6.9,2.3,4.6,3.5,2.6,5.1]
}
)
仅使用条形图即可
alt.Chart(df).mark_bar().encode(
x = alt.X('weight:O', title=None, axis=None, sort=['10 kg', '10 g', '10 mg']),
y = alt.Y('mean(value)', title='Value'),
color = alt.Color('weight:O', sort=['10 kg', '10 g', '10 mg']),
column = alt.Column('size', sort=['huge', 'big', 'small'])
)
但不再有错误栏:
error_bars = alt.Chart().mark_errorbar(extent='ci').encode(
x=alt.X('weight:O', sort=['10 kg', '10 g', '10 mg']),
y='value:Q'
)
bars = alt.Chart().mark_bar().encode(
x = alt.X('weight:O', title=None, axis=None, sort=['10 kg', '10 g', '10 mg']),
y = alt.Y('mean(value)', title='Value'),
color = alt.Color('weight:O', sort=['10 kg', '10 g', '10 mg'])
)
alt.layer(bars, error_bars, data=df).facet(
column = alt.Column('size', sort=['huge', 'big', 'small'])
)
在两个图中,axis
和 title
已设置为 None
,但未在分层图中考虑。奇怪的是,排序考虑了图例(参见 color = ...
),但没有考虑 x 轴(在每个尺寸内)。
有没有办法解决这个问题,还是我没有正确使用分层图表?
要在分层图表中隐藏轴,您应该在两个图层中设置 axis=None
和 title=None
:
error_bars = alt.Chart().mark_errorbar(extent='ci').encode(
x=alt.X('weight:O', title=None, axis=None, sort=['10 kg', '10 g', '10 mg']),
y='value:Q'
)
bars = alt.Chart().mark_bar().encode(
x = alt.X('weight:O', title=None, axis=None, sort=['10 kg', '10 g', '10 mg']),
y = alt.Y('mean(value)', title='Value'),
color = alt.Color('weight:O', sort=['10 kg', '10 g', '10 mg'])
)
alt.layer(bars, error_bars, data=df).facet(
column = alt.Column('size', sort=['huge', 'big', 'small'])
)
您会注意到我的图表版本具有正确的排序顺序:这是因为我使用的是 Altair 4.0 版。 Altair/Vega-Lite 的早期版本中有一个 bug 阻止排序在分层图表中正常运行。
更新到 Altair 4.0 或更新版本,您的排序将正常工作。
我正在为我的条形图使用自定义排序并且效果很好。但是,当我想向其添加误差线并使用分层图表时,排序就不再考虑了。我还定义了 axis = None
,这也没有考虑在内。
以下是数据示例:
df = pd.DataFrame(
{'size' : ['huge', 'huge', 'huge', 'huge', 'huge', 'huge', 'big', 'big', 'big', 'big', 'big', 'big', 'small', 'small', 'small', 'small', 'small', 'small'],
'weight': ['10 mg', '10 mg', '10 g', '10 g', '10 kg', '10 kg', '10 mg', '10 mg', '10 g', '10 g', '10 kg', '10 kg','10 mg', '10 mg', '10 g', '10 g', '10 kg', '10 kg'],
'value': [3.5,2.6,5.1,6.5,2.3,4.6,7.1,2.8,6.9,1.5,2.6,2.8,6.9,2.3,4.6,3.5,2.6,5.1]
}
)
仅使用条形图即可
alt.Chart(df).mark_bar().encode(
x = alt.X('weight:O', title=None, axis=None, sort=['10 kg', '10 g', '10 mg']),
y = alt.Y('mean(value)', title='Value'),
color = alt.Color('weight:O', sort=['10 kg', '10 g', '10 mg']),
column = alt.Column('size', sort=['huge', 'big', 'small'])
)
但不再有错误栏:
error_bars = alt.Chart().mark_errorbar(extent='ci').encode(
x=alt.X('weight:O', sort=['10 kg', '10 g', '10 mg']),
y='value:Q'
)
bars = alt.Chart().mark_bar().encode(
x = alt.X('weight:O', title=None, axis=None, sort=['10 kg', '10 g', '10 mg']),
y = alt.Y('mean(value)', title='Value'),
color = alt.Color('weight:O', sort=['10 kg', '10 g', '10 mg'])
)
alt.layer(bars, error_bars, data=df).facet(
column = alt.Column('size', sort=['huge', 'big', 'small'])
)
在两个图中,axis
和 title
已设置为 None
,但未在分层图中考虑。奇怪的是,排序考虑了图例(参见 color = ...
),但没有考虑 x 轴(在每个尺寸内)。
有没有办法解决这个问题,还是我没有正确使用分层图表?
要在分层图表中隐藏轴,您应该在两个图层中设置 axis=None
和 title=None
:
error_bars = alt.Chart().mark_errorbar(extent='ci').encode(
x=alt.X('weight:O', title=None, axis=None, sort=['10 kg', '10 g', '10 mg']),
y='value:Q'
)
bars = alt.Chart().mark_bar().encode(
x = alt.X('weight:O', title=None, axis=None, sort=['10 kg', '10 g', '10 mg']),
y = alt.Y('mean(value)', title='Value'),
color = alt.Color('weight:O', sort=['10 kg', '10 g', '10 mg'])
)
alt.layer(bars, error_bars, data=df).facet(
column = alt.Column('size', sort=['huge', 'big', 'small'])
)
您会注意到我的图表版本具有正确的排序顺序:这是因为我使用的是 Altair 4.0 版。 Altair/Vega-Lite 的早期版本中有一个 bug 阻止排序在分层图表中正常运行。
更新到 Altair 4.0 或更新版本,您的排序将正常工作。