4

I am having trouble in plotting a bar graph on this Dataset.

+------+------------+--------+
| Year | Discipline | Takers |
+------+------------+--------+
| 2010 | BSCS       |   213  |
| 2010 | BSIS       |   612  |
| 2010 | BSIT       |   796  |
| 2011 | BSCS       |   567  |
| 2011 | BSIS       |   768  |
| 2011 | BSIT       |   504  |
| 2012 | BSCS       |   549  |
| 2012 | BSIS       |   595  |
| 2012 | BSIT       |   586  |
+------+------------+--------+

I'm trying to plot a bar chart with 3 bars representing the number of takers for each year. This is the algorithm I did.

import matplotlib.pyplot as plt
import pandas as pd

Y = df_group['Takers']
Z = df_group['Year']

df = pd.DataFrame(df_group['Takers'], index = df_group['Discipline'])
df.plot.bar(figsize=(20,10)).legend(["2010", "2011","2012"])

plt.show()

I'm expecting to show something like this graph

With the same legends

enter image description here

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Benz
  • 69
  • 7

3 Answers3

7

First reshape by DataFrame.pivot, plot and last add labels by this:

ax = df.pivot('Discipline', 'Year','Takers').plot.bar(figsize=(10,10))

for p in ax.patches: 
    ax.annotate(np.round(p.get_height(),decimals=2), (p.get_x()+p.get_width()/2., p.get_height()), ha='center', va='center', xytext=(0, 10), textcoords='offset points')

pic

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks! It's finally clearer now.. But is there a function in panda that will sort the bar graph? which will show the bars descending from left to right perhaps? – Benz Jun 21 '19 at 16:36
  • I did this, `ax = df_group.pivot('Discipline', 'Year','Takers').sort_values('Discipline', ascending=False).plot.bar(title=("Number of Takers per Discipline from 2010 to 2012"), figsize=(20,10))` but the bars isn't sorting – Benz Jun 21 '19 at 17:35
  • @Benz need `ax = df_group.pivot('Discipline', 'Year','Takers').sort_index(ascending=False).plot.bar(title=("Number of Takers per Discipline from 2010 to 2012"), figsize=(20,10))` – jezrael Jun 21 '19 at 17:41
  • Oh sorry I said it wrong.. I wanted to sort the graph by Takers instead of the Discipline column. but when I tried putting .sot_values('Takers') it gives and error KeyError: 'Takers' – Benz Jun 21 '19 at 17:49
  • @Benz - Still not sure if understand, do you need plot first group like 213, 549, 567, secnd like 595, 612, 768 ? – jezrael Jun 22 '19 at 05:39
4

With Seaborn, you can directly use your Dataframe:

import seaborn as sns
ax = sns.barplot(data=df, x="Discipline", hue="Year", y="Takers")

To add the labels, you can use the snippet from jezrael:

for p in ax.patches: 
    ax.annotate(np.round(p.get_height(),decimals=2), (p.get_x()+p.get_width()/2., p.get_height()), ha='center', va='center', xytext=(0, 10), textcoords='offset points')
plt.tight_layout()

grouped barplot

jrjc
  • 21,103
  • 9
  • 64
  • 78
0

Just add 2 more lines before plt.show() in your code and you will get your result. The whole code is given below.

import matplotlib.pyplot as plt
import pandas as pd

Y = df_group['Takers']
Z = df_group['Year']

df = pd.DataFrame(df_group['Takers'], index = df_group['Discipline'])
df.plot.bar(figsize=(20,10)).legend(["2010", "2011","2012"])

for i,v in enumerate(Y):
    plt.text(x=i, y=v+2, s=v)
    # Here i= index value  
    # v= real value which you have in variable Y.
    # x and y are coordinate.
    # 's' is the value you want to show on the plot.

plt.show()
Rahul charan
  • 765
  • 7
  • 15