【问题标题】:Get matplotlib color from style palette从样式调色板中获取 matplotlib 颜色
【发布时间】:2016-04-14 15:54:48
【问题描述】:

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

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

【问题讨论】:

    标签: python matplotlib data-visualization


    【解决方案1】:

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

    【讨论】:

    • 谢谢!以及如何获得当前激活的配色方案?
    • 我认为使用matplotlib 的内置函数没有简单的方法可以做到这一点。据我所知,这归结为检查您的rcParams 中的活动值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多