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

More than 1 year has passed since last update.

matplotlibの各種設定をmatplotlibrcで管理する

2
Posted at

この記事はChatGPT-4oにより生成されました。

参考記事:科学技術論文に用いる図表のための matplotlib 設定

1. matplotlibrc とは?

matplotlibrc は Matplotlib の 全体設定(グローバル設定)を管理するファイルです。
例えば、毎回以下のような設定を書くのは面倒です。

plt.rcParams["font.size"] = 24
plt.rcParams["figure.figsize"] = (6, 4)
plt.rcParams["lines.linewidth"] = 2

matplotlibrc に設定をまとめておけば、すべてのスクリプトに自動適用されます


2. 設定ファイルの場所を確認する

ユーザー用の設定ファイルは、以下で確認できます。

import matplotlib
print(matplotlib.get_configdir())

例:

~/.matplotlib

このディレクトリに matplotlibrc を置けば、仮想環境やプロジェクトを超えて共通設定が有効になります。


3. matplotlibrc の基本書式

  • 設定は key : value 形式で記述。
  • 行頭の # はコメント。

例:

# フォント
font.family : IPAexGothic, Noto Sans CJK JP, DejaVu Sans, sans-serif
font.size : 24

# 図のサイズ
figure.figsize : 6, 4
figure.dpi : 300

# 線の設定
lines.linewidth : 2.5
lines.markersize : 6

# 凡例
legend.fontsize : 20
legend.frameon : False

4. フォントのフォールバック

複数のフォントをリストとして指定すると、先頭から順に利用可能なフォントが使われます。

font.family : IPAexGothic, Noto Sans CJK JP, Yu Gothic, DejaVu Sans, sans-serif

5. スクリプト内で設定を上書き

一時的に設定を変えたい場合は、rcParams を直接変更します。

import matplotlib.pyplot as plt
plt.rcParams['font.size'] = 18

または mpl.rc_context() で一時的な適用が可能です。

import matplotlib as mpl
with mpl.rc_context({'lines.linewidth': 3}):
    plt.plot([1, 2, 3])

6. スライド用(デフォルト設定)サンプル

~/.matplotlib/matplotlibrc に以下を保存すると、スライドに適した大きなフォントと太い線がデフォルトになります。

# ===============================
#  スライド用 matplotlibrc
# ===============================

# --- フォント ---
font.family : IPAexGothic, Noto Sans CJK JP, Yu Gothic, DejaVu Sans, sans-serif
font.size : 24
mathtext.fontset : stix

# --- 図のサイズ ---
figure.figsize : 6, 4  # inch
figure.dpi : 300

# --- 線・マーカー ---
lines.linewidth : 2.5
lines.markersize : 6

# --- 軸 ---
axes.linewidth : 1.2
axes.labelsize : 24
xtick.labelsize : 22
ytick.labelsize : 22

# --- 凡例 ---
legend.fontsize : 20
legend.frameon : False

# --- 出力 ---
savefig.bbox : tight
savefig.dpi : 300

7. 論文用設定サンプル

論文用の図は 小さいサイズ(2段組み1カラム幅 85mm 程度) に調整します。
以下を ~/.matplotlib/styles/matplotlibrc_paper として保存します。

# ===============================
#  論文用 matplotlibrc
# ===============================

# --- フォント ---
font.family : IPAexGothic, Noto Sans CJK JP, Yu Gothic, DejaVu Sans, sans-serif
font.size : 10
mathtext.fontset : stix

# --- 図のサイズ ---
figure.figsize : 3.4, 2.5  # inch (約85mm x 62mm)
figure.dpi : 300

# --- 線・マーカー ---
lines.linewidth : 1.2
lines.markersize : 4

# --- 軸 ---
axes.linewidth : 0.8
axes.labelsize : 10
xtick.labelsize : 9
ytick.labelsize : 9

# --- 凡例 ---
legend.fontsize : 9
legend.frameon : False

# --- 出力 ---
savefig.bbox : tight
savefig.dpi : 300

スクリプトから切り替えるには:

import matplotlib.pyplot as plt
plt.style.use('~/.matplotlib/styles/matplotlibrc_paper')

8. おすすめの運用フロー

  1. print(matplotlib.get_configdir()) でユーザー設定ディレクトリを確認
  2. スライド用設定を ~/.matplotlib/matplotlibrc に配置
  3. 論文用は ~/.matplotlib/styles/matplotlibrc_paper として保存
  4. 必要に応じて plt.style.use() で切り替える

9. まとめ

  • matplotlibrc を活用すると 毎回の設定が不要になり、見やすい図を一貫して作成できる。
  • スライド用をデフォルトにし、論文用は別ファイルで管理すると効率的。
  • フォントのフォールバックを設定すると環境依存の問題が減る。
2
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
2
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?