はじめに
matplotlibのax.tick_params()で目盛りのフォーマットを細かく調整する方法について説明する。
コード&解説
モジュールのインポートなど
バージョン
データの作成
np.logspaceでログで均等なデータを作成して、xとする。xをexp(-x)とすることで指数関数的減衰データを作成する。
作成したデータの図を表示
データを表示すると以下のようになる。

目盛りの長さを変更
set_xscale(“log”)でx軸がログスケールになり、補助目盛りが出てくる。しかしながら、この補助目盛りは小さくて視認性が悪い。そこで、目盛りの長さを調整することで見やすくする。tick_params(axis=”x”,which=’minor’, length=5)のようにすることで目盛りがのびて見やすくなる。
axisで調整する軸(“x” or “y”)を選べて、whichで調整する目盛り(主目盛り(“major”)、または補助目盛り(“minor”))を選べる。

目盛りの幅と色の調整
ax.tick_paramsで、幅はwidthで、色はcolorで調整できる。

目盛りの方向を指定
directionで目盛りの方向を変えることができる。”in”とすると内側になり、”inout”とすると内側と双側の両方に表示される。外側だけの時は”out”とする。

目盛りを全ての軸に表示
tick_paramでbottom=True, top=True, left=True, right=Trueとすることで各軸に目盛りを表示できる。非表示にしたいときはFalseとする。

軸と軸ラベルの距離を変更する
ax[1].tick_params(pad=10)にようにpadで調整できる。

軸ラベルのサイズを変更する
labelsizeで変更できる。”large”とかも使える。

軸ラベルの回転
labelrotationで角度を指定することで回転できる。

参考
matplotlib.axes.Axes.tick_params — Matplotlib 3.5.1 documentation
コメント