4

私が使用したいフォント サイズについては、matplotlib のほぼすべての軸で 5 ティックが最も視覚的に好ましいティック数であることがわかりました。また、目盛りラベルが重ならないように、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()

x 軸、y 軸、およびカラーバーのデフォルトのロケータがデフォルトで x 軸にプルーニング オプションを指定した上記の MaxNLocator になるように使用できる rc 設定はありますか?

4

3 に答える 3

3

myplotlibこれらのデフォルトを好きなように設定するカスタム モジュールを作成してみませんか?

import myplt
myplt.setmydefaults()

グローバル rc 設定は、これらの設定が変更されないように依存している他のアプリケーションを壊す可能性があります。

于 2012-05-03T19:01:18.573 に答える
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にハードワイヤード パラメーターで呼び出すため、上記はさらにハッキングしない限りグローバルな効果はありません。

デフォルトのロケータを に変更するには、カスタム メソッドで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 の目盛りの両方に異なるオプションを指定できるという良い副作用があります。

于 2013-03-09T15:58:05.863 に答える
1

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-05-03T19:09:53.397 に答える