How do I find the name of a matplotlib style?

Is it possible to find out the name of the current matplotlib style? I know that I can get a list of all available styles using plt.style.available

, but I want to get the name of the style being used. I am working in spyder ipython console, python 3.5, but I would be surprised if the answer to this depends on my work environment :)

+3


source to share


1 answer


Looking at the sourceplt.style.use

, I see no indication that using the new style does anything to store the name of the style being used.

I thought about manually checking the current one rcParams

for 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 match the default style. Then I also printed the reasons for the inconsistency:

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

      

Pay attention to the second element: w

vs white

. Now this is a problem. Named colors are not easily tested as being the same with the same color, different names or RGBA values, etc.

Then I thought about converting each color with matplotlib.colors.to_rgba()

, but if you want to do it right, you need to fully parse the parameters, including such as:

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

      

Even if the last value is white, we need to parse that value first and find the color inside.

It seems to me that the only safe implementation would be to plt.style.use

store the style name somewhere. But what happens if something rcParams

is manually changed? Then there will be no style that is currently loaded. This is a counterargument against keeping the style name. Whenever parameters have been changed, there must be a check that invalidates the last invocation style name plt.style.use

. I'm not sure if there is an obvious solution to your problem.

+1


source







All Articles