| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Python matplotlib

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

Python matplotlib

Python | Python NumPy |

Install

ubuntu で apt-get を利用してインストール
  1. sudo apt-get install python-matplotlib
CentOS で PIPを利用してインストール
  1. # pip install matplotlib
  2. # pip install python-dateutil
  3. # pip install pyparsing
Windows で パッケージをダウンロードしてインストール
Windows で PIPを利用してインストール
  1. C:\Python27\Scripts>pip install matplotlib
  2. C:\Python27\Scripts>pip install python-dateutil
  3. C:\Python27\Scripts>pip install pyparsing

グラフウィンドウ

表示確認

  • グラフ描画のバックエンドが何に設定されているのかとウィンドウの表示確認
  1. >>> import matplotlib
  2. >>> matplotlib.get_backend()
  3. 'TkAgg'
  4. >>> import pylab
  5. >>> pylab.show()
  6. >>> pylab.figure()
  7. <matplotlib.figure.Figure object at 0x00000000043979E8>
  8. >>> pylab.show()

1053 matplotlib graph window01.jpg

表示方法を変える

以下の中から、matplotlibのバックエンドを設定する
  • ['pdf', 'pgf', 'Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX']
WebAggの例

  1. import matplotlib
  2. matplotlib.use("WebAgg")
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import random
  6. fig,ax = plt.subplots()
  7. ax.bar(np.arange(10),[random.randrange(0,10) for x in range(10)],1)
  8. plt.show()

1054 matplotlib graph window02.png

終了

  • plt.cla(): Axesをクリア
  • plt.clf(): figureをクリア
  • plt.close(): プロットを表示するためにポップアップしたウィンドウをクローズ

例1
  1. >>> from pylab import *
  2. >>> x = range(10)
  3. >>> y = [y1**2 for y1 in x]
  4. >>> plot(x, y, 'ro')
  5. [<matplotlib.lines.Line2D object at 0x0278D170>]
  6. >>> savefig('test.png')
  7. >>> show()

1055 matplotlib01.jpg

例2
  1. >>> from pylab import *
  2. >>> from numpy import *
  3. >>> x = arange(-3,3,.1)
  4. >>> y = [y1**3 for y1 in x]
  5. >>> plot(x,y)
  6. [<matplotlib.lines.Line2D object at 0x02C1C270>]
  7. >>> show()

1056 matplotlib02.jpg

コマンド

http://matplotlib.org/api/pyplot_summary.html

Examples

サンプル

棒グラフ

  1. >>> import numpy as np
  2. >>> import matplotlib.pyplot as plt
  3. >>> import random
  4. >>> random_data = [random.randrange(0,10) for x in range(10)]
  5. >>> random_data
  6. [0, 1, 8, 4, 5, 9, 8, 0, 4, 7]
  7. >>> fig,ax = plt.subplots()
  8. >>> ax.bar(np.arange(10),random_data,1)
  9. <Container object of 10 artists>
  10. >>> plt.show()

1057 matplotlib03.jpg