Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find matplotlib style name?

Is it possible to know the name of the current matplotlib style? I know that I can get the list of all available styles using plt.style.available, but what I want is the possibility of getting the name of the style currently in use. I am working in the ipython console of spyder, python 3.5, but I would be surprised if answer to this would depend on my working environment :)

like image 776
Manish Goel Avatar asked Jul 07 '17 10:07

Manish Goel


People also ask

Where are matplotlib styles stored?

The list of available matplotlib themes is stored in a list called plt. style.

How do I change the style in matplotlib?

Another way to change the visual appearance of plots is to set the rcParams in a so-called style sheet and import that style sheet with matplotlib. style. use . In this way you can switch easily between different styles by simply changing the imported style sheet.

What is the default matplotlib style?

The new default colormap used by matplotlib. cm. ScalarMappable instances is 'viridis' (aka option D).

What is style use (' Fivethirtyeight ') in Python?

This shows an example of the "fivethirtyeight" styling, which tries to replicate the styles from FiveThirtyEight.com. import matplotlib.pyplot as plt import numpy as np plt. style. use('fivethirtyeight') x = np. linspace(0, 10) # Fixing random state for reproducibility np.


1 Answers

Looking at the source of plt.style.use, I don't see any signs that using a new style does anything to store the name of the style being used.

I thought about manually checking the current rcParams against every available style, something like this:

import matplotlib.pyplot as plt

for style in plt.style.available:
    for key in plt.style.library[style]:
        if plt.rcParams[key] != plt.style.library[style][key]:
            break
    else:
        print('Current style is consistent with {}.'.format(style))

But none of the available styles matched for my default style. Then I also printed the reasons for mismatch:

for style in plt.style.available:
    for key in plt.style.library[style]:
        val_now,val_style = plt.rcParams[key],plt.style.library[style][key]
        if val_now != val_style:
            print('Discarding style {}: {} vs {} for key {}'.format(style,val_now,val_style,key))
            break
    else:
        print('Current style is consistent with {}.'.format(style))

Part of the output:

Discarding style seaborn-paper: 0.6 vs 0.4 for key xtick.minor.width
Discarding style seaborn-whitegrid: w vs white for key figure.facecolor
Discarding style seaborn-talk: 0.8 vs 1.3 for key grid.linewidth

Note the second item: w vs white. Now this is a problem. Named colors don't easily test as equal with the same color by different names or RGBA values etc.

I then thought about converting every color using matplotlib.colors.to_rgba(), but if you want to do it properly, you need to fully parse the parameters, including gems like this:

Discarding style grayscale: figure.facecolor,w vs figure.facecolor,0.75

Even if the latter value corresponded to white, we'd need to parse that value first and find the color buried inside.

To me it seems that the only safe implementation would be to make plt.style.use store the name of the style somewhere. But then what happens if anything in the rcParams is changed manually? Then there won't be a style which is currently loaded. This is then a counterargument against storing the style name. Whenever the params were changed, there would have to be a check that invalidates the last plt.style.use call's style name. I'm not sure there's an obvious solution to your problem.

like image 181