我需要为图形添加一些背景线,例如 x=0
和 y=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/