3

I have a python script which uses the following import list:

import matplotlib
matplotlib.style.use('ggplot')

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

It then imports a textfile into a dataframe, creates some additional columns in the df and creates two plots.

I need to be able to call it from Matlab (for a number of tedious reasons...) and I did this by typing the following at the Matlab prompt:

dos('myfile.py')

Which threw the following error:

Traceback (most recent call last): 
  File "D:\Documents\Python Scripts\XXXXXXXXX.py", line 8, in     <module> 
    matplotlib.style.use('ggplot') 
AttributeError: 'module' object has no attribute 'style' 

However if I comment the line asking for the ggplot style, it runs fine and generates the plots correctly in standard matplotlib formatting.

Any suggestions as to why this might be the case?

Best Regards,

Ben

3
  • The error is pretty self explanatory, no? matplotlib doesn't have a style attribute. pyplot, however, does. If you check the examples from matplotlib, you'll see that you want to use plt.style.use('ggplot') instead. Commented Apr 28, 2015 at 12:19
  • It is, but I was confused as to why the code worked fine when I ran it in an IPython console and displayed the plots correctly. Commented Apr 28, 2015 at 12:40
  • @BMichell the reason it would have worked in IPython is that you ran the maptplotlib magic %matplotlib before Commented Sep 23, 2015 at 13:55

1 Answer 1

14

style is an attribute of pyplot, not matplotlib

try:

import matplotlib.pyplot as plt
plt.style.use('ggplot')
Sign up to request clarification or add additional context in comments.

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.