jay*_*o03 3 matplotlib data-analysis pandas seaborn
我有pandas数据帧,一个索引(datetime)和三个变量(int)
date A B C
2017-09-05 25 261 31
2017-09-06 261 1519 151
2017-09-07 188 1545 144
2017-09-08 200 2110 232
2017-09-09 292 2391 325
Run Code Online (Sandbox Code Playgroud)
我可以用基本的熊猫图创建分组条形图.
df.plot(kind='bar', legend=False)
Run Code Online (Sandbox Code Playgroud)
但是,我想在Seaborn或其他图书馆展示,以提高我的技能.
我找到了非常接近的答案(熊猫:如何绘制两个类别和四个系列的条形图?).
在其建议的答案中,它具有代码
ax=sns.barplot(x='', y='', hue='', data=data)
Run Code Online (Sandbox Code Playgroud)
如果我将此代码应用于我的,我不知道我的'y`是什么.
ax=sns.barplot(x='date', y=??, hue=??, data=data)
Run Code Online (Sandbox Code Playgroud)
如何使用Seaborn或其他库绘制多个变量?
我想melt
如果想要使用需要barplot
:
data = df.melt('date', var_name='a', value_name='b')
print (data)
date a b
0 2017-09-05 A 25
1 2017-09-06 A 261
2 2017-09-07 A 188
3 2017-09-08 A 200
4 2017-09-09 A 292
5 2017-09-05 B 261
6 2017-09-06 B 1519
7 2017-09-07 B 1545
8 2017-09-08 B 2110
9 2017-09-09 B 2391
10 2017-09-05 C 31
11 2017-09-06 C 151
12 2017-09-07 C 144
13 2017-09-08 C 232
14 2017-09-09 C 325
ax=sns.barplot(x='date', y='b', hue='a', data=data)
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
Run Code Online (Sandbox Code Playgroud)
与熊猫的解决方案DataFrame.plot.bar
和set_index
:
df.set_index('date').plot.bar()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4700 次 |
最近记录: |