python:matplotlib

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
python:matplotlib [2018/04/06 15:21] – [参考サイト] mumeiyamibitopython:matplotlib [2022/11/29 10:24] (現在) – [操作方法] mumeiyamibito
行 3: 行 3:
   * 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 は以下の つのインターェース (記法が存在する 
-    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]] +
-  * 描画範囲の設定\\ <code python>plt.xlim(X0, X1) +
-plt.ylim(Y0, Y1)</code> +
-    * ''X0'', ''Y0'': X および Y の描画開始位置 +
-    * ''X1'', ''Y1'': X および Y の描画終了位置 +
-  * フォントの調整: [[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]] 
-  * [[http://d.hatena.ne.jp/y_n_c/20091122/1258842406 | matplotlibのグラフの体裁を整える - たこ焼き食べた.net]] 
  
 {{tag>プログラミング}} {{tag>プログラミング}}
  
  • python/matplotlib.1522995696.txt.gz
  • 最終更新: 2018/04/06 15:21
  • by mumeiyamibito