python - 从样式调色板获取 matplotlib 颜色

标签 python matplotlib data-visualization

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

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

最佳答案

我不确定我是否完全理解您要实现的目标,但您可能会发现 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})

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

plt.style.library['bmh']['axes.facecolor']

给出:

u'#eeeeee'

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

import matplotlib.pyplot as plt

关于python - 从样式调色板获取 matplotlib 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36628158/

相关文章:

r - np 包结果的偏回归图

javascript - d3 选择 .attr ('x1' ) 返回 r.getAttribute 不是函数

python - 基数排序, "Queue"对象不可迭代

matplotlib - 在 matplotlib 中,有没有办法异步弹出一个图形?

python - Matplotlib : the colorbar keeps shrinking

ggplot 中facet_grid 中的按行着色

python - 如何在脉冲信号上使用 Numpy.FFT

python - 正则表达式:匹配空格连接的单词列表,不包括最后一个空格

python - 执行 Numba 生成的程序集

python - mathplotlib.dates.DateFormatter() 自 strftime() 以来是否已弃用?