31

I am trying to set a style in matplotlib as per tutorial http://matplotlib.org/users/style_sheets.html

import matplotlib.pyplot as plt
plt.style.use('ggplot')

but what I get in return is:

AttributeError: 'module' object has no attribute 'style'

My matplotlib version is 1.1.1 (and I'm on a Mac running Mavericks). Where are the styles in this version?

thanks!

5 Answers 5

23

In ipython notebook I also had to include %matplotlib inline, otherwise I would still get the same error.

%matplotlib inline
import matplotlib
matplotlib.style.use('ggplot')
1
  • 1
    Thanks for the suggestion! Adding %matplotlib inline worked for me as well. I do not get the error message AttributeError: 'module' object has no attribute 'style' anymore. Commented Aug 3, 2016 at 9:31
22

My matplotlib version is 1.1.1

There's your problem. The style package was added in version 1.4. You should update your version.

3
  • I upgraded it to 1.4.2, but I get the same results: 'module' object has no attribute 'style'. If I try to import style module from matplotlib itself, as mentioned in some tutorials, it says ImportError: cannot import name style.
    – kurtgn
    Commented Nov 30, 2014 at 11:08
  • 5
    @kurtgn Can you please check that the upgrade really worked? (Import matplotlib and then print matplotlib.__version__.)
    – Carsten
    Commented Nov 30, 2014 at 11:28
  • You were right! The upgrade didn't work. I still have 1.1.1. This is very weird though, because pip freeze says I have matplotlib==1.4.2. I uninstalled matplotlib globally and locally (in virtualenv), in both states pip freeze said I had no matplotlib at all, at which point I installed 1.4.2, which turns out to be still 1.1.1. And I'm stuck.
    – kurtgn
    Commented Nov 30, 2014 at 17:34
10

For people using matplotlib 2.x and discovering this question can use the following snippet:

import matplotlib.style
import matplotlib as mpl
mpl.style.use('classic')   # any style.

This is described in the documentation here. Note the import matplotlib.style is important.

7

I tried all solutions listed on StackOverflow but somehow none of these work for me. Finally I found a method which worked. Following are the details: Environment: OS : Ubuntu 16 Python Version : 3.5. MatPlotLib Version : 2.0.2

Correct Way of importing 'style module'

import matplotlib
matplotlib.use
import matplotlib.pyplot as plt
plt.style.use('ggplot')

The matplotlib help reads:

:func:~matplotlib.use (ignore syntax as "`" did not work either on command line or script file) a function for setting the matplotlib backend. If used, this function must be called immediately after importing matplotlib for the first time. In particular, it must be called before importing pylab (if pylab is imported).

Somehow without issuing this command, it was not possible to access the 'Style' module.

Hope this helps.

1

For MatPlotLib version 3.6.3 and above, the following code to be used to use "seaborn" style as the seaborn has been deprecated since version 3.6:

import matplotlib
matplotlib.use
import matplotlib.pyplot as plt
plt.style.use("seaborn-v0_8")

Not the answer you're looking for? Browse other questions tagged or ask your own question.