1
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('Iris.csv')

plot = plt.scatter(df['SepalLengthCm'], df['PetalLengthCm'])
plot.savefig('ScatterIris.png')

I'm trying to do some really basic matplotlib stuff and it keeps raising errors.

C:\Users\Robert\Anaconda3\python.exe 
C:/Users/Robert/PycharmProjects/linear_regression/ML.py
Traceback (most recent call last):
  File "C:/Users/Robert/PycharmProjects/linear_regression/ML.py", line 9, in <module>
plot.savefig('ScatterIris.png')
AttributeError: 'PathCollection' object has no attribute 'savefig'

First I couldn't use the .show() attribute and then I couldn't use the .savefig() attribute. Is there something wrong with my matplotlib installation?

For reference I tried changing the backend of my matplotib in matplotlibrc to a couple different ones and the same error everytime.

Edit @ nbryans

plt.scatter(df['SepalLengthCm'], df['PetalLengthCm']).savefig('ScatterIris.png')

Same error comes up

Edit 2:

Yeah you guys were right I can save figures and use the show() attribute/method. Thanks!

3
  • 1
    Should be plt.savefig() Commented May 11, 2017 at 16:10
  • Your edit doesn't matter... You're still trying to call a method (savefig) that doesn't exist for PathCollection objects. Commented May 11, 2017 at 16:13
  • 1
    So you may choose any of the answers to accept (doesn't matter which one, they are all roughly the same), such that this question will not stay unsolved. Commented May 11, 2017 at 17:01

3 Answers 3

1

You need to call pyplot's savefig method.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('Iris.csv')

plt.scatter(df['SepalLengthCm'], df['PetalLengthCm'])
plt.savefig('ScatterIris.png')

The same is true if you were using the pandas plotting function,

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('Iris.csv')
df.plot(kind="scatter", x='SepalLengthCm', y= 'PetalLengthCm')

plt.savefig('ScatterIris.png')
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to assign a plot variable and you should just do plt.show(). Try:

plt.scatter(df['SepalLengthCm'], df['PetalLengthCm'])

plt.savefig('ScatterIris.png') # or plt.show()

Comments

0

This is because savefig() is a function of pyplot (i.e.plt) and not of the recently createdplot`. It saves whatever the current plot you created is. Thus, it should be:

plt.savefig()

Similarly, just to see your plot, it would be

plt.show()

Comments

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.