「Matplotlib tips」の版間の差分
提供: Eospedia
(→Axes3D) |
(→画像ファイルを開いて表示) |
||
(同じ利用者による、間の7版が非表示) | |||
行1: | 行1: | ||
+ | == 参考文献(全般) == | ||
+ | * [https://matplotlib.org/tutorials/index.html 公式チュートリアル] | ||
+ | * [https://qiita.com/skotaro/items/08dc0b8c5704c94eafb9 早く知っておきたかったmatplotlibの基礎知識、あるいは見た目の調整が捗るArtistの話] | ||
+ | |||
== JupyterLabでインタラクティブプロット == | == JupyterLabでインタラクティブプロット == | ||
* 下準備 | * 下準備 | ||
行10: | 行14: | ||
始めにinlineにしておいて途中からwidgetに変えるとかは無理。最初から冒頭でwidgetにしておく必要あり。 | 始めにinlineにしておいて途中からwidgetに変えるとかは無理。最初から冒頭でwidgetにしておく必要あり。 | ||
+ | |||
+ | == 複数プロット == | ||
+ | === 参考文献 === | ||
+ | * https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/subplots_demo.html | ||
+ | |||
+ | === 具体例1 === | ||
+ | * 縦に3つのグラフを並べて、x軸の目盛りとラベルは一番下のみに表示 | ||
+ | <pre> | ||
+ | fig, ax = plt.subplots(nrows=3, ncols=1, figsize=figsize, dpi=dpi, sharex=True) | ||
+ | ax[0].plot(epoch, gen_loss) | ||
+ | ax[0].set_ylabel('Generator loss') | ||
+ | ax[1].plot(epoch, kld) | ||
+ | ax[1].set_ylabel('KLD') | ||
+ | ax[2].plot(epoch, total_loss) | ||
+ | ax[2].set_ylabel('Total loss') | ||
+ | ax[2].set_xlabel('Epoch') | ||
+ | </pre> | ||
== 3Dプロット == | == 3Dプロット == | ||
行25: | 行46: | ||
</pre> | </pre> | ||
− | * | + | * axはAxes3Dというクラスのインスタンスになっていて、3Dプロット用のメソッドを持っている。 |
+ | ** APIは https://matplotlib.org/3.3.2/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.html#mpl-toolkits-mplot3d-axes3d-axes3d を参照 | ||
* %matplotlib widget と一緒に使うと、3Dプロットの回転とかがマウスドラッグで可能。 | * %matplotlib widget と一緒に使うと、3Dプロットの回転とかがマウスドラッグで可能。 | ||
+ | |||
+ | == プロットの保存 == | ||
+ | === オブジェクト指向インターフェイス === | ||
+ | * ドキュメント | ||
+ | ** https://matplotlib.org/3.3.2/api/_as_gen/matplotlib.figure.Figure.html?highlight=savefig#matplotlib.figure.Figure.savefig | ||
+ | matplotlib.figure.Figureオブジェクト (fig) に対し、 | ||
+ | <pre> | ||
+ | fig.savefig('hoge.png', bbox_inches='tight') | ||
+ | </pre> | ||
+ | * bbox_inches='tight' を付けない場合、例えばfigが 10x7inch@100dpiであれば 1000x700 pixelで画像が保存されるが、図によってはかなりの余白ができてしまう。 | ||
+ | * bbox_inches='tight' を付けると、図の中のコンテンツをできるだけタイトに囲むバウンディングボックスでもって図が保存される。画素数はその分減少するけど無駄な余白が無い。 | ||
+ | |||
+ | windowsのダークテーマとか使っていて、標準のフォトアプリとかで保存画像を開くと、軸やラベルの部分が背景が透明化されていて見づらいかもしれない。その場合は | ||
+ | <pre> | ||
+ | fig.savefig('hoge.png', bbox_inches='tight', facecolor='w') | ||
+ | </pre> | ||
+ | としてfigを白くすればよい。 | ||
+ | |||
+ | ループ回して大量に保存するとかならfigはcloseして消すべし? | ||
+ | <pre> | ||
+ | plt.close(fig) | ||
+ | </pre> | ||
+ | |||
+ | == 画像ファイルを開いて表示 == | ||
+ | === Jupyter, IPythonの場合 === | ||
+ | ==== Pillow ==== | ||
+ | * ドキュメント | ||
+ | ** https://pillow.readthedocs.io/en/stable/reference/Image.html | ||
+ | <pre> | ||
+ | from PIL import Image | ||
+ | from IPython.display import display | ||
+ | |||
+ | display(Image.open('hoge.png')) | ||
+ | </pre> |
2020年11月4日 (水) 12:58時点における最新版
目次
参考文献(全般)
JupyterLabでインタラクティブプロット
- 下準備
- ipympl( https://github.com/matplotlib/ipympl )をインストール
- 使い方
- ノートブックの冒頭で以下のマジックコマンドをたたく
%matplotlib widget
始めにinlineにしておいて途中からwidgetに変えるとかは無理。最初から冒頭でwidgetにしておく必要あり。
複数プロット
参考文献
具体例1
- 縦に3つのグラフを並べて、x軸の目盛りとラベルは一番下のみに表示
fig, ax = plt.subplots(nrows=3, ncols=1, figsize=figsize, dpi=dpi, sharex=True) ax[0].plot(epoch, gen_loss) ax[0].set_ylabel('Generator loss') ax[1].plot(epoch, kld) ax[1].set_ylabel('KLD') ax[2].plot(epoch, total_loss) ax[2].set_ylabel('Total loss') ax[2].set_xlabel('Epoch')
3Dプロット
Axes3D
- インポート
from mpl_toolkits.mplot3d import Axed3D import matplotlib.pyplot as plt
- 使い方
fig = plt.figure() ax = fig.add_subplot(111, projection='3d')
- axはAxes3Dというクラスのインスタンスになっていて、3Dプロット用のメソッドを持っている。
- %matplotlib widget と一緒に使うと、3Dプロットの回転とかがマウスドラッグで可能。
プロットの保存
オブジェクト指向インターフェイス
matplotlib.figure.Figureオブジェクト (fig) に対し、
fig.savefig('hoge.png', bbox_inches='tight')
- bbox_inches='tight' を付けない場合、例えばfigが 10x7inch@100dpiであれば 1000x700 pixelで画像が保存されるが、図によってはかなりの余白ができてしまう。
- bbox_inches='tight' を付けると、図の中のコンテンツをできるだけタイトに囲むバウンディングボックスでもって図が保存される。画素数はその分減少するけど無駄な余白が無い。
windowsのダークテーマとか使っていて、標準のフォトアプリとかで保存画像を開くと、軸やラベルの部分が背景が透明化されていて見づらいかもしれない。その場合は
fig.savefig('hoge.png', bbox_inches='tight', facecolor='w')
としてfigを白くすればよい。
ループ回して大量に保存するとかならfigはcloseして消すべし?
plt.close(fig)
画像ファイルを開いて表示
Jupyter, IPythonの場合
Pillow
from PIL import Image from IPython.display import display display(Image.open('hoge.png'))