0

I have the following dataframe

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

I have plotted a bar chart with 2 bars one representing 2015 and the other 2016 using this code

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()

However the x axi shows the index 0 1 2 3 4

- I want it to show the drug names

How would I go about doing this?

Sarah
  • 3
  • 3
  • 7
  • 1
    Can you please show what you have tried yet? – Abhijeetk431 Dec 02 '17 at 00:40
  • @Abhijeetk431 I have edited my post to show how I plot one bar chart but I am not sure how to merge them to create 2 graphs – Sarah Dec 02 '17 at 00:42
  • Why don't you: -add the years to your dataframes -merge the 2 dataframes on drug names play with this df. It is not clear to me how and why you want a non numerical value for the y axis. – rpanai Dec 02 '17 at 01:06
  • @user32185 I have tried to use concat to join the two dataframes but it doesn't work it is pulling the dataframe for 16 underneath instead on of 2015. Do you know I could do this? – Sarah Dec 02 '17 at 01:12
  • @user32185 I have successfully merged the 2 dataframes but I am unsure how to plot this now on a bar chart with on bar displaying 2015 and the onther 2016 – Sarah Dec 02 '17 at 01:22
  • @Abhijeetk431 I have edited my question showing what I have tried to compare 2 years – Sarah Dec 02 '17 at 01:44
  • @Sarah please correct me if I'm wrong: I think you'll rather want drug names on the x-axis then a legend with 2 colors, 1 for 2015 and 1 for 2016, finally on the y axis you want numb of occurance. – rpanai Dec 02 '17 at 01:57
  • @user32185 I agree I have updated my code to get exactly as you have described my one and only problem now is how to change my x axis from the index numbers of 0 1 2 3 4 to display the drug names? – Sarah Dec 02 '17 at 02:03
  • @user32185 I have one last question -- How can I display the actual occurrence on the bar itself just so that its easier to see what values it is – Sarah Dec 02 '17 at 02:28
  • @Sarah does my answer solve your problem? If yes it will be nice from you to approve it. Then I see that you're new here but someone could point out that you need to ask separate questions instead to continue under the original one. Anyway [here](https://stackoverflow.com/q/25447700/4819376) is the answer for your last request. – rpanai Dec 02 '17 at 02:35
  • @user32185 I added the following code for p in ax.patches: ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005)) but nothing changed in my graph? – Sarah Dec 02 '17 at 02:46
  • @user32185 Yes it did thank you so much for your time and patience I will approve your answer. Oh I see, I will in future ask separate questions .. sorry – Sarah Dec 02 '17 at 02:48

1 Answers1

1

I guess that you can start to play from this.

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

rpanai
  • 12,515
  • 2
  • 42
  • 64