Pandas 在单个条形图上绘制多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52527186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas plot multiple columns on a single bar chart
提问by aks85
I'm trying to plot multiple columns onto a single bar chart.
我正在尝试将多个列绘制到单个条形图上。
First, I defined them:
首先,我定义了它们:
public_action_sum = df['Q17.4_1'].sum()
testified_sum = df['Q17.4_4'].sum()
met_el_sum = df['Q17.4_5'].sum()
...
and then merged those sums into a single dataframe:
actions_combined = [public_action_sum, testified_sum, met_el_sum, policy_sum, bill_sum]
然后将这些总和合并到一个数据帧中:
actions_combined = [public_action_sum, testified_sum, met_el_sum, policy_sum, bill_sum]
I'm trying to use pandas.DataFrame.plot.bar
and have written:
我正在尝试使用pandas.DataFrame.plot.bar
并已编写:
pd.DataFrame.plot.bar(x['Q17.4_1','Q17.4_4','Q17.4_5','Q17.4_6','Q17.4_7'],y=actions_combined,rot=0)
but am getting the error "missing 1 required positional argument: 'self'
但我收到错误“缺少 1 个必需的位置参数:‘self’
I've spent the last hour and a half trying to figure this iout, and am not sure where I'm going wrong. Thoughts? THANKS!
我花了最后一个半小时试图弄清楚这个问题,但不确定我哪里出错了。想法?谢谢!
回答by rahlf23
Here is a short example dataframe:
这是一个简短的示例数据框:
Date Open High Low Close Adj Close Volume
0 1996-01-01 4.06250 4.12500 3.8750 3.90625 3.093209 7048800
1 1996-02-01 3.84375 3.96875 3.5000 3.62500 2.870497 12864000
2 1996-03-01 3.50000 4.25000 3.5000 4.12500 3.266428 9526400
3 1996-04-01 4.06250 4.68750 4.0625 4.50000 3.563378 5693600
4 1996-05-01 4.40625 4.65625 4.1250 4.21875 3.340666 30480000
Then you can do:
然后你可以这样做:
import pandas as pd
import matplotlib.pyplot as plt
df[['Open','High','Low','Close','Adj Close']].plot(kind='bar')
plt.show()
Which gives:
这使: