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
にハードワイヤード パラメーターで呼び出すため、上記はさらにハッキングしない限りグローバルな効果はありません。
デフォルトのロケータを に変更するには、カスタム メソッドで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 の目盛りの両方に異なるオプションを指定できるという良い副作用があります。