简体   繁体   English

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

[英]How to unstacked grouped bar chart in Altair- python?

All, 所有,

My dataset looks like following.I am trying to use Altair library to plot my visualization such that day of week is on x-axis, pickups is on y-axis, there are two plots one plot with snow= Y and other plot with snow=N, and color is based on my borough. 我的数据集看起来像following.I想使用Altair库来绘制可视化我这样一周中的一天是在x轴上,皮卡是在y轴上,有两个地块一个地块雪= Y等情节雪= N,并且颜色基于我的自治市镇。 I was successful in plotting two plots. 我成功绘制了两个图。 However, all the bars are stacked on each other. 但是,所有条都相互堆叠。 I would like to unstack the plots. 我想把地块拆开。 Below is my ALtair code. 下面是我的ALtair代码。

dput of dataset 数据集的输出

{'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 Code: Altair代码:

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

You can create unstacked bars using a column encoding in combination with an x encoding, following the example of Altair's Grouped Bar Chart . 您可以按照Altair的分组条形图的示例,结合使用列编码和x编码来创建未堆叠的条形。 For your data it might look something like this: 对于您的数据,它可能看起来像这样:

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)

在此处输入图片说明

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

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