我试图制作一系列条形图,每个城市一个图表,每个图表显示Y轴上的计数(范围70 - 210)和X轴我喜欢有21个酒吧,每个工作日和时间段组合一个(7x3 = 21)。这是数据
import pandas as pd
import matplotlib.pyplot as plt
data = [
['CITY','DAY','TIME_BIN', 'COUNT'],
['PHOENIX', "Friday", 1, 70],
['PHOENIX', "Thursday", 2, 80],
['PHOENIX', "Wednesday", 3, 90],
['ATLANTA', "Sunday", 1, 130],
['ATLANTA', "Monday", 2, 150],
['ATLANTA', "Tuesday", 3, 160],
['CHICAGO', "Saturday", 1, 180],
['CHICAGO', "Friday", 2, 200],
['CHICAGO', "Friday", 3, 210],
]
df = pd.DataFrame(data[1:],columns=data[0])
print(df)
CITY DAY TIME_BIN COUNT
0 PHOENIX Friday 1 70
1 PHOENIX Thursday 2 80
2 PHOENIX Wednesday 3 90
3 ATLANTA Sunday 1 130
4 ATLANTA Monday 2 150
5 ATLANTA Tuesday 3 160
6 CHICAGO Saturday 1 180
7 CHICAGO Friday 2 200
8 CHICAGO Friday 3 210
我希望输出是下面两次尝试的某种组合。将数组功能与条形图结合使用。
# Successful attempt at making an array of charts but wrong type
df[['DAY', 'TIME_BIN']].hist(by=df['CITY'])
plt.show()
# Bar chart with proper counts but x-axis did not combine properly
ax = df.plot(x=['DAY', 'TIME_BIN'],
y='COUNT',
kind='bar',
color=["g","b"])
plt.show()
答案 0 :(得分:1)
使用附加参数绘制此类分类数据的简单解决方案是使用searborn。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = [
['CITY','DAY','TIME_BIN', 'COUNT'],
['PHOENIX', "Friday", 1, 70],
['PHOENIX', "Thursday", 2, 80],
['PHOENIX', "Wednesday", 3, 90],
['ATLANTA', "Sunday", 1, 130],
['ATLANTA', "Monday", 2, 150],
['ATLANTA', "Tuesday", 3, 160],
['CHICAGO', "Saturday", 1, 180],
['CHICAGO', "Friday", 2, 200],
['CHICAGO', "Friday", 3, 210],
]
df = pd.DataFrame(data[1:],columns=data[0])
g = sns.factorplot(x="DAY", y ='COUNT', hue='TIME_BIN', col="CITY", col_wrap=3,
data=df,
kind="bar", size=3, aspect=.8)
g.set_xticklabels(rotation=30, ha="right")
plt.tight_layout()
plt.show()
使用pandas,您可以在具有多个列的数据框上使用subplots=True
参数,这将为每列提供一个子图。为此,首先要从“DAY”和“Time_bin”列创建一个MultiIndex,然后围绕“CITY”列进行旋转。
import pandas as pd
import matplotlib.pyplot as plt
data = [
['CITY','DAY','TIME_BIN', 'COUNT'],
['PHOENIX', "Friday", 1, 70],
['PHOENIX', "Thursday", 2, 80],
['PHOENIX', "Wednesday", 3, 90],
['ATLANTA', "Sunday", 1, 130],
['ATLANTA', "Monday", 2, 150],
['ATLANTA', "Tuesday", 3, 160],
['CHICAGO', "Saturday", 1, 180],
['CHICAGO', "Friday", 2, 200],
['CHICAGO', "Friday", 3, 210],
]
df = pd.DataFrame(data[1:],columns=data[0])
df.set_index(['DAY','TIME_BIN'], inplace=True)
piv = df.pivot(columns="CITY").plot(kind="bar", subplots=True)
plt.tight_layout()
plt.show()