Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib,全局设置记号数。X轴、Y轴、色条_Python_Matplotlib - Fatal编程技术网

Python Matplotlib,全局设置记号数。X轴、Y轴、色条

Python Matplotlib,全局设置记号数。X轴、Y轴、色条,python,matplotlib,Python,Matplotlib,对于我喜欢使用的字体大小,我发现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

对于我喜欢使用的字体大小,我发现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轴和颜色栏的默认定位器默认为x轴上带有修剪选项的上述MaxNLocator?

为什么不编写一个自定义模块
myplotlib
,将这些默认值设置为您喜欢的值

import myplt
myplt.setmydefaults()

全局rc设置可能会中断依赖这些设置的其他应用程序,使其无法修改。

正如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

matplotlib.ticker.MaxNLocator类具有一个可用于设置默认值的属性:

default_params = dict(nbins = 10,
                      steps = None,
                      trim = True,
                      integer = False,
                      symmetric = False,
                      prune = None)
例如,脚本开头的这一行将在axis对象每次使用
MaxNLocator
时创建5个记号

from matplotlib.ticker import *
MaxNLocator.default_params['nbins']=5
但是,默认定位器是
matplotlib.ticker.AutoLocator
,基本上使用硬连线参数调用
MaxNLocator
,因此,如果没有进一步的破解,上述定位器将不会产生全局效果

要将默认定位器更改为
maxlocator
,我能找到的最好方法是使用自定义方法覆盖
matplotlib.scale.LinearScale.set_default_locators_和_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刻度指定不同的选项。

我喜欢这个想法。类似于继承matplotlib的自定义类?我不确定myplt.setmydefaults会是什么样子。实际上不是一个类。只是一个调用
plt的方法。我想是吧。