看着的来源plt.style.use https://github.com/matplotlib/matplotlib/blob/734c09f164819854a13d77b128da283952eb08d4/lib/matplotlib/style/core.py#L64-L113,我没有看到任何迹象表明使用新样式可以存储正在使用的样式的名称。
我想过手动检查当前rcParams
针对每种可用的样式,如下所示:
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))
但没有一个可用的样式与我的默认样式匹配。然后我还打印了不匹配的原因:
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))
部分输出:
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
注意第二项:w
vs white
。现在这是一个问题。命名颜色不容易通过不同的名称或 RGBA 值等来测试与相同颜色的相等性。
然后我考虑使用转换每种颜色matplotlib.colors.to_rgba()
,但是如果你想正确地做到这一点,你需要完全解析参数,包括像这样的gem:
Discarding style grayscale: figure.facecolor,w vs figure.facecolor,0.75
即使后一个值对应于白色,我们也需要首先解析该值并找到隐藏在其中的颜色。
对我来说,唯一安全的实施似乎是plt.style.use
将样式的名称存储在某处。但如果其中有任何事情会发生什么rcParams
是手动更改的吗?那么就不会有be当前加载的样式。这是反对存储样式名称的反驳。每当参数更改时,都必须进行检查以使最后一个参数无效plt.style.use
调用的样式名称。我不确定您的问题是否有明显的解决方案。