0

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.

7
  • Your code works on my computer, with pandas version '0.21.1' and matplotlib version 2.2.0. What versions do you have? Commented May 7, 2019 at 13:43
  • I updated the question with the versions. Are you using a mac as well? Commented May 7, 2019 at 13:45
  • Are you using a jupyter notebook? Commented May 7, 2019 at 13:58
  • 2
    Do other plots work (plt.plot(range(10)); plt.show()`?) If not, they you likely have a backend problem; do you get any errors? My guess would be your install is crossed somehow. Commented May 7, 2019 at 14:02
  • 1
    Someone reported a similar problem yesterday: stackoverflow.com/questions/56013105/… Commented May 7, 2019 at 15:09

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

3 Comments

Right you can’t run macosx backend w/o using pythonw for now. As of 3.1 you should be able to. However it’s strange that you got a blank figure. Were there any warnings?
@JodyKlymak No warnings. My default backend was TkAgg and I still get a blank figure with that for some reason...
Ok I don’t think anaconda installs tk by default so it was probably trying to use the system to libraries? If you specifically install tk in conda does it work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.