首页 > 解决方案 > pyplot 从 2.1.1 到 2.2.2 的不同行为

问题描述

我已经完成了一个 python 脚本来准备我的情节。我使用 debian 来准备这个,其中 matplot lib 的版本是 2.1.1 ,而现在我正在转向 Archlinux,我在其中处理版本 2.2.2 ,问题是在 2.1.1 中我以这种方式定义所有参数(例如循环器颜色):

plt.rc_context({ 'axes.prop_cycle': cycler('color', ['#8DA0CB', '#E78AC3', '#A6D854', '#FFD92F', '#E5C494', '#B3B3B3', '#66C2A5', '#FC8D62'] )}) 

在 2.2.2 我找到了这个解决方案:

plt.rc('axes', prop_cycle=(cycler('color', ['#8DA0CB', '#E78AC3', '#A6D854', '#FFD92F', '#E5C494', '#B3B3B3', '#66C2A5', '#FC8D62'])))

问题是我已经定义了所有这些参数:

plt.rc_context({'axes.edgecolor': self.parameter['box'] })   # BOX colors
         plt.rc_context({'axes.linewidth':'1.2' })   # BOX width
         plt.rc_context({'axes.xmargin':'0' })     
         plt.rc_context({'axes.ymargin':'0' })     
         plt.rc_context({'axes.labelcolor':self.parameter['axeslabel']})     
         plt.rc_context({'axes.axisbelow':'True' })     
         plt.rc_context({'xtick.color': self.parameter['xtickcolor']})   # doesn't affect the text
         plt.rc_context({'ytick.color': self.parameter['ytickcolor']})   # doesn't affect the text
         #plt.rc_context({ 'axes.prop_cycle': self.colors('tthmod')}) 
         plt.rc_context({ 'axes.prop_cycle': cycler('color', ['#8DA0CB', '#E78AC3', '#A6D854', '#FFD92F', '#E5C494', '#B3B3B3', '#66C2A5', '#FC8D62'] )}) 
         plt.rc_context({ 'grid.linestyle': '--'}) 
         plt.rc_context({ 'grid.alpha': '1'})
         #plt.rc_context({ 'grid.color': '#E5E5E5'})
         plt.rc_context({ 'grid.color': '#FFFFFF'})

我在哪里可以找到解决方案??我的意思是让我们以“我的方式”工作或如何更改语法以获得相同的结果!谢谢

编辑 si ma perche` non mi prende quelle versioni che avevo scritto (cosa che faceva la versione 2.1.1) mi lascia pensare che qualcosa sia cambiato!!

标签: python-3.xmatplotlibplot

解决方案


一次指定多个 rc 参数的一种可能方法是使用字典并matplotlib.rcParams用它更新。

import matplotlib.pyplot as plt
from cycler import cycler

myparams = {'axes.edgecolor': "red",   # BOX colors
            'axes.linewidth': 1.2,   # BOX width
            'axes.xmargin': 0,    
            'axes.ymargin': 0,     
            'axes.labelcolor': "crimson",     
            'axes.axisbelow': True,   
            'xtick.color': "blue",   # doesn't affect the text
            'ytick.color': "gold",   # doesn't affect the text 
            'axes.prop_cycle': cycler('color', ['#8DA0CB', '#E78AC3', '#A6D854', '#FFD92F', '#E5C494', '#B3B3B3', '#66C2A5', '#FC8D62']), 
            'grid.linestyle': '--', 
            'grid.alpha': '1',
            'grid.color': '#E5E5E5'}
plt.rcParams.update(myparams)

相反,如果您想使用上下文,则可以这样做

with plt.rc_context(myparams):
    plt.plot([1,2,3])

无论如何,在上下文之外使用plt.rc_context(如问题)可能没有太大意义。


推荐阅读