如何在Altair-python中取消堆积的条形图?

时间:2019-03-16 18:26:53

标签: python data-visualization stacked-chart altair

全部

我的数据集如下所示。我正在尝试使用Altair库来绘制可视化图,以使星期几在x轴上,拾取在y轴上,有两个图,其中一个图上有雪= Y,其他积雪= N,并且颜色基于我的自治市镇。我成功绘制了两个图。但是,所有条都相互堆叠。我想把地块拆开。以下是我的ALtair代码。

数据集吞吐量

{'borough': {0: 'Bronx', 1: 'Brooklyn', 3: 'Manhattan', 4: 'Queens', 5: 'Staten Island',29094: 'Bronx', 29095: 'Brooklyn', 29097: 'Manhattan', 29098: 'Queens', 29099: 'Staten Island'}, 'pickups': {0: 152, 1: 1519, 3: 5258, 4: 405, 5: 6,29094: 67, 29095: 990, 29097: 3828, 29098: 580, 29099: 0}, 'snow': {0: 'N', 1: 'N', 3: 'N', 4: 'N', 5: 'N',29094: 'N', 29095: 'N', 29097: 'N', 29098: 'N', 29099: 'N'}, 'day_of_week': {0: 'Wednesday', 1: 'Wednesday', 3: 'Wednesday', 4: 'Wednesday', 5: 'Wednesday',29094: 'Monday', 29095: 'Monday', 29097: 'Monday', 29098: 'Monday', 29099: 'Monday'}}

代号:

alt.Chart(df).mark_bar().encode(
    x='day_of_week:O',
    y='pickups:Q',
    color='borough:N',
    column='snow:N'
)

1 个答案:

答案 0 :(得分:1)

您可以按照Altair's Grouped Bar Chart的示例,结合使用列编码和x编码来创建未堆叠的钢筋。对于您的数据,它可能看起来像这样:

import pandas as pd
import altair as alt

data = {'borough': {0: 'Bronx', 1: 'Brooklyn', 3: 'Manhattan', 4: 'Queens', 5: 'Staten Island',29094: 'Bronx', 29095: 'Brooklyn', 29097: 'Manhattan', 29098: 'Queens', 29099: 'Staten Island'}, 'pickups': {0: 152, 1: 1519, 3: 5258, 4: 405, 5: 6,29094: 67, 29095: 990, 29097: 3828, 29098: 580, 29099: 0}, 'snow': {0: 'N', 1: 'N', 3: 'N', 4: 'N', 5: 'N',29094: 'N', 29095: 'N', 29097: 'N', 29098: 'N', 29099: 'N'}, 'day_of_week': {0: 'Wednesday', 1: 'Wednesday', 3: 'Wednesday', 4: 'Wednesday', 5: 'Wednesday',29094: 'Monday', 29095: 'Monday', 29097: 'Monday', 29098: 'Monday', 29099: 'Monday'}}
df = pd.DataFrame(data)

alt.Chart(df).mark_bar().encode(
    x=alt.X('borough:N', axis=None),
    y='pickups:Q',
    color='borough:N',
    column='day_of_week:N'
).properties(width=80)

enter image description here