matplotlib メモ書き

はじめに

インポートとエイリアスを下記のように設定.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

根本的な仕組みの理解については,

を参考にさせていただきました.
ここでは,下記のように Figure, Axes のインスタンスを生成させるようにし,オブジェクト指向的な書き方と MATLAB 的な書き方を両方記しているものもある.

fig = plt.figure()
ax = fig.add_subplot(111)

Figureのサイズ(大きさ)調整

fig = plt.figure(figsize=(hor,ver))

デフォルトはfigsize=(8, 6).800x600 ピクセルという単位(横x縦).

目盛ラベル位置

現在の目盛位置を取得するには(x軸の場合)

ax.get_xticks()

目盛位置と目盛ラベルを指定するには,float 型のリスト ticks,文字列のリスト labels という変数があるとして

plt.xticks(ticks, labels) # MATLAB的な書き方
ax.xaxis.set_ticks(ticks) # メソッドを使用, オブジェクト指向っぽい書き方
ax.xaxis.set_ticklabels(labels)

目盛長さ

下記は x, y 軸の目盛を消す処理.ラベルはそのまま.

ax.tick_params(axis='both', length=0.0)

軸ラベル

fontdict と fontname という指定方法がある.

plt.xlabel('x-axis',fontsize=12,fontdict={"name":"serif"}) # MATLAB的な書き方&辞書型
plt.ylabel('y-axis',fontsize=12,fontname='serif')
ax.set_xlabel(...) # オブジェクト指向っぽい書き方
ax.set_ylabel(...)

軸の向きを逆に

y軸の向きを逆にする例.

ax.invert_yaxis()

colorbar

fig = plt.figure()
ax = fig.add_subplot(111)
pc = ax.pcolor(X, Y, Z)
pc.set_clim(0.,15.)
ax.axis('scaled')
cbar = fig.colorbar(pc, ticks=np.linspace(0.,15.,7))
cbar.ax.set_ylabel('colorbar hogehoge')

Front page   Edit Diff Attach Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes
Last-modified: 2021-02-05 (Fri) 09:57:19 (1169d)