Luca
Luca

Reputation: 10996

cannot plot with matplotlib and pandas

I am using OSX (Mojave 10.14.3) and am having a strange issue plotting a pandas (0.24.2) dataframe using matplotlib (3.0.3). I am using python 3.7.3.

So, the code is as:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
                  'name':['john','mary','peter','jeff','bill','lisa','jose'],
                  'age':[23,78,22,19,45,33,20],
                  'gender':['M','F','M','M','M','F','M'],
                  'state':['california','dc','california','dc','california','texas','texas'],
                  'num_children':[2,0,0,3,2,1,4],
                  'num_pets':[5,1,0,5,2,2,3]
                  })

df.plot(kind='scatter',x='num_children',y='num_pets',color='red')
plt.show()

All this does is show an empty window with nothing in it. I was expecting a scatterplot with 7 points. The example is taken from the web tutorial as is.

EDIT

plt.savefig('myfilename.png')

Savefig works.

Upvotes: 0

Views: 497

Answers (1)

Luca
Luca

Reputation: 10996

I am not sure if this will help anyone but I basically had to install python as a framework to make it work. I was using Anaconda, so something like:

conda install python.app
pythonw script.py  # note using pythonw

I, then, was able to get the plot to render correctly by using the macosx backend:

import matplotlib as mpl
mpl.use('MacOSX')

Upvotes: 1

Related Questions