0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

よく使うmatplotlibの書式

Last updated at Posted at 2025-08-05

matplotlibで論文用のグラフを出力する時によく使うコードです。

example.py
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm

#グラフの書式を弄る
plt.rcParams['font.family'] = 'Arial'
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
plt.rcParams['font.size'] = 12

#figureを作る。背景を透明にするときはfacecolorなし
fig = plt.figure(figsize=(6,4), dpi=120, tight_layout=True, facecolor='w')
#axesを作る
ax = fig.add_subplot(111)

#プロットする
ax.plot(x, y, label='label')

#凡例を入れる。fameon=Falseで枠線なし
ax.legend(frameon=False)

#xy軸の範囲を指定。なくてもいいがその場合軸が0にならない
ax.set_xlim(0, 12)
ax.set_ylim(0, 100)

#軸ラベルを入れる。フォントサイズは少し大きくすると見やすい
ax.set_xlabel('xlabel', fontname='Arial', fontsize=16)
ax.set_ylabel('ylabel', fontname='Arial', fontsize=16)

#軸の目盛りを指定。なくても自動でやってくれる
#ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(5))
#ax.yaxis.set_major_locator(mpl.ticker.MultipleLocator(25))

#出力。fig.savefigの方はファイルを出力する
plt.show()
#fig.savefig('fig.png')
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?