简体   繁体   English

排序在 Altair 分层条形图中丢失,带有误差条

[英]Sorting lost in Altair layered bar chart with error bars

I am using a custom sort for my bar chart and it works well.我正在为条形图使用自定义排序,并且效果很好。 However when I want to add error bars to it and use a layered chart then the sorting is not taking into account anymore.但是,当我想向其中添加误差线并使用分层图表时,不再考虑排序。 I also defined axis = None and that is also not taking into account.我还定义了axis = None ,这也没有考虑到。

Here is an example of the data:下面是一个数据示例:

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]
    }
)

Using just the bar chart works仅使用条形图有效

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'])
)

在此处输入图片说明

But not anymore with the error bars:但不再有误差条:

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'])
)

在此处输入图片说明

In both plots the axis and title have been set to None but is not taking into account in the layered chart.在这两个图中, axistitle都设置为None但在分层图表中未考虑在内。 The weird thing is that the sorting is taking into account for the legend (see color = ... ) but not for the x-axis (within each size).奇怪的是排序考虑了图例(见color = ... )而不是 x 轴(在每个尺寸内)。

Is there a way around this or am I not using the layered charts correctly?有没有办法解决这个问题,或者我没有正确使用分层图表?

To hide the axis in the layered chart, you should set axis=None and title=None in both layers:要隐藏分层图表中的轴,您应该在两个图层中设置axis=Nonetitle=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'])
)

在此处输入图片说明

You'll notice that my version of the chart has the correct sort order: this is because I'm using Altair version 4.0.您会注意到我的图表版本具有正确的排序顺序:这是因为我使用的是 Altair 4.0 版。 There was a bug in earlier versions of Altair/Vega-Lite that prevented sort from behaving properly in layered charts.在早期版本的 Altair/Vega-Lite 中存在一个错误,该错误会阻止排序在分层图表中正常运行。

Update to Altair 4.0 or newer and your sorting will work.更新到 Altair 4.0 或更高版本,您的排序将起作用。

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

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