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

问题描述 投票:0回答:1

所有,

我的数据集看起来如下。我正在尝试使用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'}}

Altair代码:

alt.Chart(df).mark_bar().encode(
    x='day_of_week:O',
    y='pickups:Q',
    color='borough:N',
    column='snow:N'
)
python data-visualization stacked-chart altair
1个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.