LoginSignup
2
1

論文に載せる図のためのMatplotlibのテンプレート(JupyterNotebook想定)!

Last updated at Posted at 2024-05-03

結論

次のコードをpython notebookの最初のセルで実行
→図を作る際は,default_ax関数にaxを渡すだけ.

import matplotlib.pyplot as plt

%config InlineBackend.figure_format = 'retina'   # 図の解像度を上げる

plt.rcParams['font.family'] = 'Times New Roman' # font familyの設定
plt.rcParams['mathtext.fontset'] = 'stix' # math fontの設定
plt.rcParams["legend.fancybox"] = False # 丸角
plt.rcParams["legend.framealpha"] = 1 # 透明度の指定、0で塗りつぶしなし
plt.rcParams["legend.edgecolor"] = 'black' # edgeの色を変更
plt.rcParams["legend.handlelength"] = 1.3 # 凡例の線の長さを調節
plt.rcParams["legend.labelspacing"] = 0.05 # 垂直方向(縦)の距離の各凡例の距離
plt.rcParams["legend.handletextpad"] = 0.3 # 凡例の線と文字の距離の長さ
plt.rcParams["legend.markerscale"] = 1 # 点がある場合のmarker scale


def default_ax(ax):
    spines = 2
    #軸の太さの調整。方向を辞書のキーとして渡し、set_linewidthで大きさを微調整できる
    ax.spines["top"].set_linewidth(spines)
    ax.spines["left"].set_linewidth(spines)
    ax.spines["bottom"].set_linewidth(spines)
    ax.spines["right"].set_linewidth(spines)

    ax.tick_params(axis='x', direction="in", length=7, width=2, top=True, right=True, left=True, bottom=True, pad=7, labelsize=30, which='major')
    ax.tick_params(axis='y', direction="in", length=7, width=2, top=True, right=True, left=True, bottom=True, pad=4, labelsize=30, which='major')
    ax.tick_params(axis='y', direction="in", length=5, width=2, top=True, right=True, left=True, bottom=True, which='minor')
    ax.tick_params(axis='x', direction="in", length=5, width=2, top=True, right=True, left=True, bottom=True, which='minor')
    return ax

使い方

import numpy as np

x = np.linspace(-7, 7, 100)
y = np.sinc(x)

# axを渡す
ax = default_ax(plt.figure().add_subplot())

ax.set_xlabel("x", fontsize=30, labelpad=-3)
ax.set_ylabel("y", fontsize=30)

ax.plot(x, y, c="blue", lw=3, label="sinc(x)")
ax.legend(fontsize=20)    # legendの枠の太さは,.get_frame().set_linewidth(太さ)を使って調節できる

after.png

補足

Matplotlibのデフォルト設定の図は次の様になります.
before.png
悪くはないのですが,論文に載せるにはメモリの数字が小さかったり,解像度が物足りません.

default_axの中で設定されている数値はお好みの様に変えてください!

ごめんなさい

このコードを作成するにあたって様々な記事をこれまで参考にさせていただいております.
コピペした部分もあると思うのですが,どなたの記事をどの部分で参考にしたか覚えていないため,参考文献に引用することが出来ません.
みんなありがとう!

2
1
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
2
1