両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン |
python:matplotlib [2018/04/06 14:13] – [ipython でグラフを閉じた後に操作を受け付けない] mumeiyamibito | python:matplotlib [2022/11/29 10:24] (現在) – [操作方法] mumeiyamibito |
---|
* Python でグラフを描画するためのモジュール | * Python でグラフを描画するためのモジュール |
| |
===== 使い方 ===== | ===== モジュールの読み込み ===== |
==== モジュールの読み込み ==== | |
<code python>import matplotlib.pyplot as plt</code> | <code python>import matplotlib.pyplot as plt</code> |
* 同時に ''numpy'' も読み込んでおくと良い。 | * 同時に ''numpy'' も読み込んでおくと良い。 |
* Ubuntu の場合は、''python-tk'' や ''python3-tk'' が必要なので、インストールしておく。\\ <code bash>$ sudo apt install python-tk python3-tk</code> | * Ubuntu の場合は、''python-tk'' や ''python3-tk'' が必要なので、インストールしておく。\\ <code bash>$ sudo apt install python-tk python3-tk</code> |
| * jupyter notebook の場合は、''python-tk'' や ''python3-tk'' は不要。 |
| |
==== グラフの描画 ==== | ===== 記法 ===== |
- データを定義する。 | * matplotlib では以下の 2 つのインターフェース (記法) が存在する。 |
* x, y をリスト形式で定義する。\\ 例: <code python>x = [1,2,3,4] | * オブジェクト指向: ''ax.plot'' など ax で始まるコマンドで記述する方法 (厳密に設定する場合) |
y = [5,6,7,8]</code> | * オブジェクトの構造:\\ <code> |
- グラフを描画する。\\ <code python>plt.plot(x, y, PLOTSTYLE [, x2, y2, PLOTSTYLE2, ...], label = LABEL)</code> | Figure: (ウィンドウ; 描画領域) |
* ''x'', ''y'': 描画するデータ | `- Axes: グラフ描画部分 |
* ''PLOTSTYLE'': プロットの形式を指定 (以下の記号を文字列として組み合わせる) | `- Axis: 軸 |
* ''<nowiki>-</nowiki>'': 実線 (solid line)| | |- fig.axes |
* ''<nowiki>--</nowiki>'': 破線 (dash line)| | |- fig.patch (長方形オブジェクト; 背景色用の長方形オブジェクト) |
* ''<nowiki>-.</nowiki>'': 点線と破線 (dash-dot line)| | |- fig.image |
* ''<nowiki>:</nowiki>'': 点線 (dotted line) | |- fig.legends (凡例) |
* ''<nowiki>.</nowiki>'': 小さめの丸 (point marker) | |- fig.lines |
* ''<nowiki>,</nowiki>'': 点 (pixel marker) | |- fig.patches (その他の図形) |
* ''<nowiki>o</nowiki>'': 丸 (circle marker) | `- fig.texts |
* ''<nowiki>v</nowiki>'': 下三角 (triangle_down marker) | |
* ''<nowiki>^</nowiki>'': 上三角 (triangle_up marker) | |
* ''<nowiki><</nowiki>'': 左三角 (triangle_left marker) | |
* ''<nowiki>></nowiki>'': 右三角 (triangle_right marker) | |
* ''<nowiki>1</nowiki>'': (tri_down marker) | |
* ''<nowiki>2</nowiki>'': (tri_up marker) | |
* ''<nowiki>3</nowiki>'': (tri_left marker) | |
* ''<nowiki>4</nowiki>'': (tri_right marker) | |
* ''<nowiki>s</nowiki>'': 四角形 (square marker) | |
* ''<nowiki>p</nowiki>'': 五角形 (pentagon marker) | |
* ''<nowiki>*</nowiki>'': アスタリスク (star marker) | |
* ''<nowiki>h</nowiki>'': 六角形 (縦方向) (hexagon1 marker) | |
* ''<nowiki>H</nowiki>'': 六角形 (横方向) (hexagon2 marker) | |
* ''<nowiki>+</nowiki>'': + (plus marker) | |
* ''<nowiki>x</nowiki>'': ×(x marker) | |
* ''<nowiki>D</nowiki>'': ダイア (四角形が 45 度傾いたマーク) (diamond marker) | |
* ''<nowiki>d</nowiki>'': ダイア (細長いダイア) (thin_diamond marker) | |
* ''<nowiki>|</nowiki>'': 縦棒 (vline marker) | |
* ''<nowiki>_</nowiki>'': 横棒 (hline marker) | |
* ''<nowiki>b</nowiki>'': 青 | |
* ''<nowiki>g</nowiki>'': 緑 | |
* ''<nowiki>r</nowiki>'': 赤 | |
* ''<nowiki>c</nowiki>'': シアン | |
* ''<nowiki>m</nowiki>'': マゼンタ | |
* ''<nowiki>y</nowiki>'': 黄色 | |
* ''<nowiki>k</nowiki>'': 黒 | |
* ''<nowiki>w</nowiki>'': 白 | |
* ''LABEL'': 描画する線の名前 | |
* その他のキーワード: [[https://matplotlib.org/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D | matplotlib.lines.Line2D — Matplotlib 2.2.2 documentation]] | |
* 複数のデータをプロットする場合は、それぞれのデータ (線、プロット) について、plot() を実行する。 | |
- グラフの設定をする。 | |
* ''plt.xlabel(LABEL)'': x 軸の名前を ''LABEL'' で指定する。 | |
* ''plt.ylabel(LABEL)'': y 軸の名前を ''LABEL'' で指定する。 | |
* ''plt.legend(loc = "best")'': 凡例の位置を最適 (''best'') な位置に配置する。 | |
* その他の位置\\ <code> | |
+-----------+ | |
|2 9 1| | |
| | | |
|6 10 5,7| | |
| | | |
|3 8 4| | |
+-----------+ | |
</code> | </code> |
* ''0'' or ''best'' | * Pyplot インターフェース: ''plt.plot'' など、plt で始まるコマンドで記述する方法 (MATLAB に類似した記法で、細かい設定はある程度、自動で設定してくれる) |
* ''1'' or ''upper right'' | * 両方のインターフェースを混在して記述することも可能だが、見栄えは悪くなる…。 |
* ''2'' or ''upper left'' | * 参考サイト: [[https://qiita.com/skotaro/items/08dc0b8c5704c94eafb9 | 早く知っておきたかったmatplotlibの基礎知識、あるいは見た目の調整が捗るArtistの話 - Qiita]] |
* ''3'' or ''lower left'' | |
* ''4'' or ''lower right'' | |
* ''5'' or ''right'' | |
* ''6'' or ''center left'' | |
* ''7'' or ''center right'' | |
* ''8'' or ''lower center'' | |
* ''9'' or ''upper center'' | |
* ''10'' or ''center'' | |
- グラフを表示する。\\ <code python>plt.show()</code> | |
- グラフを閉じる。\\ <code python>plt.close()</code> | |
* グラフを消す場合は ''plt.cla()'' や ''plt.clf()'' を使う。 | |
| |
==== その他のグラフの設定 ==== | ===== 操作方法 ===== |
* ''plt.grid(b = BOOL, which = TARGET, axis = AXIS)'': グリッド線の設定 | * [[python/matplotlib/オブジェクト指向インターフェースでのグラフ作成 | オブジェクト指向インターフェースでのグラフ作成]] |
* ''BOOL'': グリッドの表示の有無 (''True'': 表示 / ''False'' or ''None'': 非表示) | * [[python/matplotlib/Pyplot インターフェースでのグラフ作成 | Pyplot インターフェースでのグラフ作成]] |
* ''TARGET'': 設定する対象 (''major'': 主目盛線 / ''minor'': 補助目盛線) | * [[python/matplotlib/共通操作 | 共通操作]] |
* ''AXIS'': グリッド線の方向 (''both'': 両方 / ''x'': X 軸 / ''y'': Y 軸) | |
* その他のオプション | ===== その他 ===== |
* ''linestyle'' | * [[python/matplotlib/デフォルトテーマ | デフォルトテーマ]] |
* ''color'' | |
* [[https://matplotlib.org/api/_as_gen/matplotlib.pyplot.grid.html | matplotlib.pyplot.grid — Matplotlib 2.2.2 documentation]] | |
* 設定ファイル: [[https://note.nkmk.me/python-matplotlib-matplotlibrc-stylesheet/ | Matplotlibのグラフのスタイルを設定ファイル・スタイルシートで変更 | Python / note.nkmk.me]] | |
* フォントの調整: [[https://qiita.com/yniji/items/3fac25c2ffa316990d0c | matplotlibで日本語 - Qiita]] | |
* グラフ描画領域の位置: [[https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots_adjust.html | matplotlib.pyplot.subplots_adjust — Matplotlib 2.2.2 documentation]] | |
| |
===== Tips ===== | ===== Tips ===== |
==== ipython でグラフを閉じた後に操作を受け付けない ==== | ==== バッチプログラムで利用する場合 ==== |
* 対話操作が可能なようにする。\\ <code python>plt.interactive(True)</code> or <code python>plt.ion()</code> | * バッチプログラムで matplotlib を利用すると以下のエラーメッセージが出てくる。\\ <code python> |
* [[https://stackoverflow.com/questions/13557260/plt-show-making-terminal-hang | python - plt.show() making terminal hang - Stack Overflow]] | _tkinter.TclError: no display name and no $DISPLAY environment variable |
| </code> |
| * 解決するには、以下の順で ''import'' する。\\ <code python> |
| import matplotlib |
| matplotlib.use('Agg') |
| from matplotlib import pyplot |
| </code> |
| * 参考サイト: [[https://qiita.com/itkr/items/4172c5aa6ccaa9e01b88 | [小ネタ] Pythonでimportの前に関数を実行しなければならないときの苦肉の策 - Qiita]] |
| |
==== Qt に関する ImportError ==== | ==== jupyternote で利用する場合のおまじない ==== |
* 以下のエラーが出る。\\ <code>ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, | * jupyternote で使う場合は、以下のおまじないをグラフをプロットする前に記述する。<code python> |
PySide or PySide2 package to be installed, but it was not found.</code> | %matplotlib inline |
* Qt を使わないようにする。 | %config InlineBackend.print_figure_kwargs = {'bbox_inches': None} |
* 直接設定する場合\\ <code python>import matplotlib | </code> |
matplotlib.use('TkAgg')</code> | * ''%matplotlib inline'': グラフをブラウザ内に描画する場合に必要 |
* 設定ファイルで設定する場合\\ <code python>backend : qt5agg</code>↓<code python>backend : Tkagg</code> | |
* 参考サイト: | |
* [[https://stackoverflow.com/questions/46993820/matplotlib-does-not-import-pyqt4-pyqt5-or-pyside | python - matplotlib does not import PyQt4, PyQt5 or PySide - Stack Overflow]] | |
* [[http://pppurple.hatenablog.com/entry/2016/02/28/213152 | matplotlibを使ってみる - abcdefg.....]] | |
| |
===== 参考サイト ===== | |
* [[https://qiita.com/yohm/items/1daa5aabbdb1e8edbf26 | matplotlib入門 - Qiita]] | |
* [[https://qiita.com/irs/items/cd1556c568887ff2bdd7 | matplotlib ログスケール表示とグリッド表示 - Qiita]] | |
| |
| |
{{tag>プログラミング}} | {{tag>プログラミング}} |
| |