【问题标题】:Matplotlib, globally set number of ticks. X-axis, Y-axis, colorbarMatplotlib,全局设置刻度数。 X轴、Y轴、彩条
【发布时间】:2012-05-13 08:10:22
【问题描述】:

对于我喜欢使用的字体大小,我发现 5 个刻度是 matplotlib 中几乎每个轴上最令人愉悦的刻度数。我也喜欢修剪 沿 x 轴的最小刻度,以避免重叠刻度标签。因此,对于我制作的几乎每一个情节,我都发现自己使用以下代码。

from matplotlib import pyplot as plt
from matplotlib.ticker import MaxNLocator

plt.imshow( np.random.random(100,100) )
plt.gca().xaxis.set_major_locator( MaxNLocator(nbins = 7, prune = 'lower') )
plt.gca().yaxis.set_major_locator( MaxNLocator(nbins = 6) )
cbar = plt.colorbar()
cbar.locator = MaxNLocator( nbins = 6)
plt.show()

是否有我可以使用的 rc 设置,以便我的 x 轴、y 轴和颜色条的默认定位器默认为上面的 MaxNLocator,x 轴上有修剪选项?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您为什么不直接编写一个自定义模块myplotlib 来设置这些默认值?

    import myplt
    myplt.setmydefaults()
    

    全局 rc 设置可能会破坏依赖这些设置不被修改的其他应用程序。

    【讨论】:

    • 我喜欢这个主意。类似于继承 matplotlib 的自定义类?我不确定 myplt.setmydefaults 会是什么样子。
    • 其实不是一个类。我猜只是一个调用plt.whatever的方法。
    【解决方案2】:

    matplotlib.ticker.MaxNLocator 类有一个属性可以用来设置默认值:

    default_params = dict(nbins = 10,
                          steps = None,
                          trim = True,
                          integer = False,
                          symmetric = False,
                          prune = None)
    

    例如,脚本开头的这一行将在每次 MaxNLocator 被轴对象使用时创建 5 个刻度。

    from matplotlib.ticker import *
    MaxNLocator.default_params['nbins']=5
    

    但是,默认定位器是matplotlib.ticker.AutoLocator,基本上是通过硬连线参数调用MaxNLocator,这样如果没有进一步的hack,以上将不会产生全局效果。

    要将默认定位器更改为MaxNLocator,我能找到的最好方法是使用自定义方法覆盖matplotlib.scale.LinearScale.set_default_locators_and_formatters

    import matplotlib.axis, matplotlib.scale 
    def set_my_locators_and_formatters(self, axis):
        # choose the default locator and additional parameters
        if isinstance(axis, matplotlib.axis.XAxis):
            axis.set_major_locator(MaxNLocator(prune='lower'))
        elif isinstance(axis, matplotlib.axis.YAxis):
            axis.set_major_locator(MaxNLocator())
        # copy & paste from the original method
        axis.set_major_formatter(ScalarFormatter())
        axis.set_minor_locator(NullLocator())
        axis.set_minor_formatter(NullFormatter())
    # override original method
    matplotlib.scale.LinearScale.set_default_locators_and_formatters = set_my_locators_and_formatters
    

    这具有很好的副作用,即能够为 X 和 Y 刻度指定不同的选项。

    【讨论】:

      【解决方案3】:

      按照 Anony-Mousse 的建议

      创建一个文件 myplt.py

      #!/usr/bin/env python
      # File: myplt.py
      
      from matplotlib import pyplot as plt
      from matplotlib.ticker import MaxNLocator
      
      plt.imshow( np.random.random(100,100) )
      plt.gca().xaxis.set_major_locator( MaxNLocator(nbins = 7, prune = 'lower') )
      plt.gca().yaxis.set_major_locator( MaxNLocator(nbins = 6) )
      cbar = plt.colorbar()
      cbar.locator = MaxNLocator( nbins = 6)
      plt.show()
      

      在您的代码或 ipython 会话中

      import myplt
      

      【讨论】:

        猜你喜欢
        • 2012-03-14
        • 2020-06-03
        • 1970-01-01
        • 2019-07-03
        • 1970-01-01
        • 2021-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多