matplotlibでよく使う構文まとめ
はじめに
よく使う構文を自分用にまとめる。随時更新予定ではある。
インポート系
from mpl_toolkits.mplot3d import Axes3D
が絶対忘れる。
import matplotlib.pyplot as plt # これがなきゃ始まらない from mpl_toolkits.mplot3d import Axes3D # 3D表示には必要 from matplotlib.animation import ArtistAnimation # アニメーション作成に必要 from matplotlib.animation import PillowWriter # gifへ出力するために必要 from matplotlib import rcParams # フォント設定等に必要 import matplotlib.patches as patches # 図形とか描画用
日本語使用可能にする
rcParams['font.family'] = 'Meiryo' #フォントをメイリオに設定することで日本語使用可能にする。
fig, axの作成
# まとめて生成 fig, ax = plt.subplots() # 縦:横=4:5のaxリスト生成 fig, ax = plt.subplots(4, 5) # 3D用axの生成 fig = plt.figure() ax = fig.add_subplot(111, projection='3d')
fig.add_subplot(111)の意味
書き方を変えるとfig.add_subplot(1, 1, 1)
となる。
それぞれの意味は(行数, 列数, 何番目か)
である。
fig = plt.figure() axes = [] for i in range(1, 4*5+1): ax = fig.add_subplot(4, 5, i) ax.set_title(f'{i}番目') axes.append(ax) fig.tight_layout() fig.show()
頭痛いから今日はこれにて