python:matplotlib:プロットの保存
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python:matplotlib:プロットの保存 [2018/01/21 03:13] – [CUI環境] koudai | python:matplotlib:プロットの保存 [2021/06/27 22:04] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| plt.savefigを使うとファイルに直接出力できます。 | plt.savefigを使うとファイルに直接出力できます。 | ||
| - | <file python> | + | <file python |
| import numpy as np | import numpy as np | ||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
| Line 43: | Line 43: | ||
| 例えばpng形式で解像度をDPI=300としたい場合< | 例えばpng形式で解像度をDPI=300としたい場合< | ||
| - | |||
| - | |||
| - | |||
| - | ====== CUI環境 ====== | ||
| - | |||
| - | グラフをパソコンの画面に表示するには、(1)ウィンドウを開いて(2)グラフの画像を生成する、という手順が必要です。 | ||
| - | |||
| - | (1)と(2)の両方を行うソフトを対話型バックエンド(interactive backend)、(2)のみの機能を持つソフトを非対話型バックエンド(non-interactive backend)いいます。 | ||
| - | |||
| - | GUIでLinuxのターミナルを開いてmatplotlibを使っている場合は問題ありませんが、CUI環境で使っているときやSSHでサーバに接続して作業しているときなどに対話型バックエンドが使えずエラーがでます。 | ||
| - | |||
| - | このような場合、png形式でグラフをプロットする際は、matplotlib.pyplotをインポートする前にnon-interactiveバックエンドとしてAGGを指定します。 | ||
| - | |||
| - | <file python> | ||
| - | import matplotlib | ||
| - | matplotlib.use(" | ||
| - | |||
| - | import numpy as np | ||
| - | import matplotlib.pyplot as plt | ||
| - | |||
| - | x = np.arange(-5, | ||
| - | y = np.sin(x) | ||
| - | |||
| - | plt.plot(x, y) | ||
| - | |||
| - | plt.savefig(" | ||
| - | </ | ||
| - | |||
| - | 非対話型バックエンドでは当然plt.showが使えません。 | ||
| - | |||
| - | 他の非対話型バックエンドについては、以下を参照のこと(大文字、小文字は区別されません)。 | ||
| - | |||
| - | ^Renderer^Filetypes^Description^ | ||
| - | |AGG|png|raster graphics – high quality images using the Anti-Grain Geometry engine| | ||
| - | |PS|ps eps|vector graphics – Postscript output| | ||
| - | |PDF|pdf|vector graphics – Portable Document Format| | ||
| - | |SVG|svg|vector graphics – Scalable Vector Graphics| | ||
| - | |Cairo|png ps pdf svg ...|vector graphics – Cairo graphics| | ||
| - | |GDK|png jpg tiff ...|raster graphics – the Gimp Drawing Kit Deprecated in 2.0| | ||
| - | |||
| - | https:// | ||
| - | |||
| - | なお、デフォルトのバックエンドは matplotlibrc で確認できます。 | ||
| - | |||
| - | * Anaconda3でインストールした場合は / | ||
| - | |||
python/matplotlib/プロットの保存.1516472029.txt.gz · Last modified: 2021/06/27 21:59 (external edit)
