Matplotlib Tips

$Id: tips-plots.html,v 1.2 2023/06/26 19:33:25 daichi Exp $
TeXのテキストに\bm等を使いたい
usetexをオンにする. そうするとticksのフォントが変になるので, 下のようにしてカバー
mpl.rc('text', usetex=True)
mpl.rcParams['text.latex.preamble'] = r"\usepackage{bm}\usepackage{helvet}"
plot (xx, yy, label=(r'$\bm{\alpha}$ Fixed'))
fmt = mpl.ticker.StrMethodFormatter("{x}")
gca().xaxis.set_major_formatter(fmt)
gca().yaxis.set_major_formatter(fmt)
matplotlib標準でサポートされている文字のリスト (anacondal3の場合)
/opt/anaconda3/lib/python3.9/site-packages/matplotlib/_mathtext_data.py
凡例を図の外に配置する
legend (loc='upper left', bbox_to_anchor=(1.05,1))
Colorbarの細かい目盛りをオフにする&ラベルの縦位置を調整
cbar = colorbar (ticks=[1,0.5,0.2,0.1,0.01])
cbar.ax.minorticks_off ()
cbar.ax.set_yticklabels(['1', '0.5', '0.2', '0.1', '0.01'])
cbar.set_label (r'$p(z|d)$', rotation=0, labelpad=30, y=0.57, fontsize=24)
x軸, y軸の目盛の間隔を, 最小値・最大値を使わずに指定したい
from matplotlib.ticker import MultipleLocator
gca().xaxis.set_major_locator (MultipleLocator(100))
のようにする. 下のような関数を書いておくと簡単にできて便利.
def xtickwidth (n, ax_=None):
    ax = gca() if ax_ is None else ax_
    ax.xaxis.set_major_locator (MultipleLocator(n))
boxplotのラベルのフォントの大きさを変える
boxplotに直接変えるオプションはなさそうなので, 以下のようにする.
from matplotlib import rcParams
rcParams['xtick.labelsize'] = 28

daichi
Last modified: Wed Apr 10 22:12:28 2024