Joseph
Joseph

Reputation: 253

How to plot using matplotlib and pandas?

I'm currently doing this tutorial here: http://nbviewer.ipython.org/urls/bitbucket.org/hrojas/learn-pandas/raw/master/lessons/01%20-%20Lesson.ipynb

I am currently on the last section where you have to plot a graph. I am running the code in my own idle and not iPython. Here is my code:

q=df['Births'].plot()

MaxValue = df['Births'].max()

MaxName = df['Names'][df['Births'] == df['Births'].max()].values

Text = str(MaxValue) + " - " + MaxName

plt.annotate(Text, xy=(1, MaxValue), xytext=(8,0),xycoords=('axes fraction', 'data'), textcoords ='offset points')
print("The most popular name")
df[df['Births'] == df['Births'].max()]

print(q)

The output I am getting is:

The most popular name
Axes(0.125,0.1;0.775x0.8)

How do I get it to actually show the graph?

Upvotes: 2

Views: 263

Answers (2)

David Hagan
David Hagan

Reputation: 1166

Adding plt.show() should do the trick, although if I may make a recomendation..this data would be better represented by a bar chart. Something like the following should do the trick:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib

df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])

matplotlib.style.use('ggplot')

ax = df['Births'].plot(kind = 'bar')
ax.set_xticklabels(df.Names)

ax.annotate('Most Popular Name: {}: {} births'.format(df.max().Names, df.max().Births),
        xy=(1, 1), 
        xytext=(1, 800))

ax.set_title("Number of Births by Name")
ax.set_ylabel("No. Births")

plt.show()

enter image description here

Upvotes: 2

ndou
ndou

Reputation: 1178

If you are not in interactive mode, you might need to add plt.figure() before your code, and plt.show() after.

Upvotes: 1

Related Questions