User Tools

Site Tools


python:matplotlib:プロットの保存

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
python:matplotlib:プロットの保存 [2018/01/21 05:35]
koudai [CUI環境]
python:matplotlib:プロットの保存 [2021/06/27 22:04] (current)
Line 43: Line 43:
  
 例えばpng形式で解像度をDPI=300としたい場合<file python>plt.savefig("sin.png", format="png", dpi=300)</file>とします。 例えばpng形式で解像度をDPI=300としたい場合<file python>plt.savefig("sin.png", format="png", dpi=300)</file>とします。
- 
- 
- 
-====== CUI環境 ====== 
- 
-matplotlibでは、図形を出力するとき、画面にウィンドウを開いて図形や文字を表示したり編集したりするソフトを対話型バックエンド(interactive backend)、与えられた情報を元にPNGやEPSといった画像ファイルを生成するソフトを非対話型バックエンド(non-interactive backend)と呼びます。 
- 
-matplotlibのデフォルトで対話型バックエンドを呼び出す設定になっている場合(これが普通です)、CUI環境で使っているときやSSHでサーバに接続して作業しているときなどに対話型バックエンドが起動できずエラーがでます。 
- 
-対話型バックエンドが使えない環境では、matplotlib.pyplotをインポートする前に<file python> 
-import matplotlib 
-matplotlib.use("Agg") 
-</file>を挿入することで非対話型バックエンドのみが使用されます。 
-plt.showが使えなくなることに注意しましょう。 
-なお、AGG=Anti-Grain Geometryはベクター画像からラスター画像を生成するのに使われるC++のライブラリです。 
- 
-<file python sin_agg.py> 
-import matplotlib 
-matplotlib.use("Agg") 
- 
-import numpy as np 
-import matplotlib.pyplot as plt 
- 
-x = np.arange(-5, 5, 0.1) 
-y = np.sin(x) 
- 
-plt.plot(x, y) 
- 
-plt.savefig("sin.png") 
-</file> 
- 
-デフォルトのバックエンドは matplotlibrc で確認できます。 
- 
-  * Anaconda3でインストールした場合は /path/to/anaconda3/lib/python3.6/site-package/matplotlib/mpl-data/ にあります。対話型バックエンド qt5agg がデフォルトになっているので、CUI環境だとAggに変更しといた方が便利です。 
- 
-===== 参考 ===== 
- 
-  * https://matplotlib.org/faq/usage_faq.html#what-is-a-backend 
- 
- 
python/matplotlib/プロットの保存.1516480544.txt.gz · Last modified: 2021/06/27 21:59 (external edit)