2018/01/06

matplotlib.pyplot のticks(目盛線)、特に補助目盛線(minor ticks)を増やす。

matplotlibで作図するとどれもticks(目盛線)少なすぎる。
自動で判別するとしても論文に載せるにはあまりに見栄えが悪い。
こういうとき、実にmatplotlibというのが扱いづらい。

同じパラメータを変更するメソッドがいくつもある。
使い方がまるで統一できないので、一般的な方法を調べてもわからない。

簡単に殺意が芽生える。

ということで補助目盛線を入れる方法。
参考:http://leancrew.com/all-this/2013/07/multiple-axes-and-minor-tick-marks/

from matplotlib import pyplot as pl
from matplotlib.ticker import AutoMinorLocator

    fig, axs = pl.subplots(2, sharex=True)
    for ax in axs: # subplot全部まとめて同じ処理
        ax.tick_params(which = 'both', # 目盛で囲むのが好き
                       right = 'on',
                       top = 'on',
                       direction = 'in') # なんでデフォルトで外側に ticks を伸ばそうと思ったのか。意味不明。
        ax.tick_params(which = 'major', # 目盛をもう少し長く
                       length = 8)
        ax.tick_params(which = 'minor',
                       length = 3)

# ここがミソ。おそらくデフォルトでは minor_locatorがかなりサボり気味
# minor_locatorをtickerのAutoMinorLocatorで自動的に1セット5本割り当てている。
        ax.xaxis.set_minor_locator(AutoMinorLocator(5)) 
        ax.yaxis.set_minor_locator(AutoMinorLocator(5)) 

分かるか!!こんなん!!
調べようがない。

0 件のコメント: