从样式调色板获取 matplotlib 颜色

Dim*_*iev 3 python data-visualization matplotlib

我需要为我的图形添加一些背景线,例如x=0y=0和对角线的粗线。每次更改图形的配色方案/“样式”时,我都必须手动更改这些线条的颜色。

有没有办法检索当前图形样式的颜色?

Pri*_*mer 6

我不确定我是否完全理解您想要实现的目标,但您可能会发现它plt.style.library很有用:

让我们以bmh风格为例。

调用plt.style.library['bmh']将产生:

RcParams({u'axes.edgecolor': u'#bcbcbc',
          u'axes.facecolor': u'#eeeeee',
          u'axes.grid': True,
          u'axes.labelsize': u'large',
          u'axes.prop_cycle': cycler(u'color', [u'#348ABD', u'#A60628', u'#7A68A6', u'#467821', u'#D55E00', u'#CC79A7', u'#56B4E9', u'#009E73', u'#F0E442', u'#0072B2']),
          u'axes.titlesize': u'x-large',
          u'legend.fancybox': True,
          u'lines.linewidth': 2.0,
          u'mathtext.fontset': u'cm',
          u'patch.antialiased': True,
          u'patch.edgecolor': u'#eeeeee',
          u'patch.facecolor': u'blue',
          u'patch.linewidth': 0.5,
          u'text.hinting_factor': 8})
Run Code Online (Sandbox Code Playgroud)

所以如果你需要一个特定的设置,你可以像这样访问它:

plt.style.library['bmh']['axes.facecolor']
Run Code Online (Sandbox Code Playgroud)

这使:

u'#eeeeee'
Run Code Online (Sandbox Code Playgroud)

PS 上面的代码假设你有这个导入:

import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)