Home > Enterprise >  Cannot access Matplotlib styles and 'plt.style' is empty
Cannot access Matplotlib styles and 'plt.style' is empty

Time:10-03

I'm using python 3.9.1 and matplotlib version 3.4.3

I'm trying to show a graph plotted with matplotlib with one of the default styles (e.g. 'ggplot', or 'dark_background'), but python seems unable to find the styles. If I run the program without a style specified, I receive no errors and the program plots a graph in the default style. But when trying to run my program with the 'dark_background' style, python returns:

OSError: 'dark_background' not found in the style library and input is not a valid URL
or path; see `style.available` for list of available styles

As recommended by the error message, I tried to view the available styles with print(plt.style.available), but that just returned an empty list: [].

I've googled the output of plt.style.available from other people, and they seem to get a list of styles returned to them looking like this:

['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']

Whereas my output is still just [].

Do I have to download the styles in addition to matplotlib? (which I downloaded through pip) Am I calling on plt.style incorrectly?

Would appreciate some help.

CodePudding user response:

Should work with just the following commands:

>>> import matplotlib.pyplot as plt
>>> plt.style.use("dark_background")
>>> print(plt.style.available)
['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']

An empty list means the style files are not installed in your system, unless some custom configuration is overwriting the default setup.

CodePudding user response:

I finally figured out how to fix this. The problem was that I installed matplotlib using the pip3 install matplotlib command. When I used the python3 -m pip install -U matplotlib command, it seems to have resolved the issue. For some reason, the plt.style class does not seem to have any data, when I use the pip3 installation method. Strange, but for anyone else having this problem, here you go!

  • Related