0
1

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-01-25

はじめに

都内の大学で大学院生をしています.研究室に配属された方は,発表や論文でグラフを掲載することがあると思います.特に,PythonユーザはMatplotlibを用いて作成することがあると思います.デフォルトの設定でもいいのですが,できれば簡素で綺麗な図を作成したいものです.そこで,今回は私が使っている設定を共有しようと思います.

環境

  • Ubuntu 24.04.1 LTS
  • Python 3.11.11
  • matplotlib 3.7.5

matplotlibは以下のようにimportしているとします.

import matplotlib.pyplot as plt

フォントのインスール・設定

図で使用するフォントはArialかHelveticaが良いとされています.
私はArialを使用していますが,UbuntuにはデフォルトでArialは入っていないのでインストールします.

sudo apt install ttf-mscorefonts-installer
sudo fc-cache -f
fc-match Arial

以下のような出力が出れば成功です

Arial.ttf: "Arial" "Regular"

このあと,matplotlibのキャッシュを削除します

rm ~/.cache/matplotlib -rf

これで,Arialが使用可能になったのでコード内に以下のように設定します.

# Arialフォントを使用するように設定
plt.rcParams['font.family']= 'Arial'

# フォント設定の確認
print("Current font family:", plt.rcParams['font.family'])

各種設定

以下のように設定しています.補助目盛りやグリッドの有無は指導教員の指示に従ってください.

#フォント設定
plt.rcParams["font.size"] = 18 # 全体のフォントサイズ
plt.rcParams['xtick.labelsize'] = 12 # x軸のフォントサイズ
plt.rcParams['ytick.labelsize'] = 12 # y軸のフォントサイズ

#軸設定
plt.rcParams['axes.linewidth'] = 1.2 #軸の太さ
#plt.rcParams['axes.grid']=True #グリッドの表示
#plt.rcParams['grid.linestyle']='--' #グリッドの線のスタイル
#plt.rcParams['grid.linewidth'] = 0.3 #グリッドの線の太さ

#目盛り設定
plt.rcParams['xtick.direction'] = 'in' #x軸の目盛りの向き
plt.rcParams['ytick.direction'] = 'in' #y軸の目盛りの向き
plt.rcParams['xtick.top'] = True  #x軸の上部目盛り
plt.rcParams['ytick.right'] = True  #y軸の右部目盛り

plt.rcParams['xtick.major.size'] = 5 #x軸の目盛りのサイズ
plt.rcParams['ytick.major.size'] = 5 #y軸の目盛りのサイズ
plt.rcParams['xtick.major.width'] = 1.2 #x軸の目盛りの太さ
plt.rcParams['ytick.major.width'] = 1.2 #y軸の目盛りの太さ

#plt.rcParams["xtick.minor.visible"] = True  #x軸補助目盛りの追加
#plt.rcParams["ytick.minor.visible"] = True  #y軸補助目盛りの追加
#plt.rcParams['xtick.minor.size'] = 3 #x軸の補助目盛りのサイズ
#plt.rcParams['ytick.minor.size'] = 3 #y軸の補助目盛りのサイズ
#plt.rcParams["xtick.minor.width"] = 1.0 #x軸補助目盛りの太さ
#plt.rcParams["ytick.minor.width"] = 1.0 #y軸補助目盛りの太さ

#凡例設定
plt.rcParams["legend.fontsize"] = 14 #凡例のフォントサイズ
plt.rcParams["legend.fancybox"] = False  # 丸角OFF
plt.rcParams["legend.framealpha"] = 1  # 透明度の指定、0で塗りつぶしなし
plt.rcParams["legend.edgecolor"] = 'black'  # edgeの色を変更

#画質
plt.rcParams['figure.dpi'] = 600

描画例

# 例として簡単なグラフを生成する
plt.figure()
plt.plot([0.1,0.9], [0.1,0.9], label="line")
plt.xlabel("X label")
plt.ylabel("Y label")
plt.legend()
plt.show()

image.png

なお,保存する場合は以下のようにします.Matplotlibではpdf出力が可能なのでpdf形式にしても構いません

plt.savefig('test.png')
plt.savefig('test.pdf')

おわりに

いかがだったでしょうか?今までデフォルト設定で使っていた方は是非試して頂けたらと思います.また,この他にも良い設定を知っている方がいましたら共有して頂けると嬉しいです.

参考文献・記事

以下の文献・記事を参考にしました.ありがとうございました.

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?