绘制比较熊猫年数的条形图

时间:2017-12-02 00:20:03

标签: python python-3.x pandas matplotlib jupyter-notebook

我有以下数据框

visit signup_path #whatever is the correct route to the page with the signup form
within '#t-signup-form' do
  fill_in 'first_name', with: 'Eve'
  fill_in 'last_name', with: 'Farmer'
  fill_in 'email', with: 'eve@example.com'
  fill_in 'password', with: 'Something'
  find('button').click
end
assert_content('You are signed up!') # assert for visible change in page that indicates signup has succeeded
visit account_path # route to whatever page you want to go to
... # do whatever actions are required to update the account

我已经绘制了一个条形图,其中2个条形图代表2015年,另一个条形代码2016年使用此代码

Date_x  BNF Chapter_x   VTM_NM  Occurance_x     Date_y  BNF Chapter_y   Occurance_y
0   2016-12-01  1   Not Specified   2994      2015-12-01    1           3212
1   2016-12-01  1   Mesalazine      2543      2015-12-01    1           2397
2   2016-12-01  1   Omeprazole      2307      2015-12-01    1           2370
3   2016-12-01  1   Esomeprazole    1535      2015-12-01    1           1516
4   2016-12-01  1   Lansoprazole    1511      2015-12-01    1           1547

然而,x axi显示指数0 1 2 3 4

fig = plt.figure() # Create matplotlib figure
ax = fig.add_subplot(111) # Create matplotlib axes
width = 0.4

df.Occurance_x.plot(kind='bar', color='red', ax=ax, width=width, position=1)
df.Occurance_y.plot(kind='bar', color='blue', ax=ax, width=width, position=0)

ax.set_ylabel('Occurance')
plt.legend(['Date_x', 'Date_y'], loc='upper right')
ax.set_title('BNF Chapter 1 Top 5 drugs prescribed')


plt.show()

我将如何做到这一点?

1 个答案:

答案 0 :(得分:0)

我想你可以从这开始玩。

import pandas as pd
df = pd.DataFrame({"date_x":[2015]*5,
                   "Occurance_x":[2994, 2543, 2307, 1535, 1511],
                   "VTM_NM":["Not Specified", "Mesalazine", "Omeprazole",
                             "Esomeprazole", "Lansoprazole"],
                   "date_y":[2016]*5,
                   "Occurance_y":[3212, 2397, 2370, 1516, 1547]})

ax = df[["VTM_NM","Occurance_x", "Occurance_y"]].plot(x='VTM_NM', 
                                                      kind='bar', 
                                                      color=["g","b"],
                                                      rot=45)
ax.legend(["2015", "2016"]);

bar plot