!!!Python matplotlib [Python][Python NumPy] {{category グラフ}} *http://matplotlib.sourceforge.net/ !Install ::ubuntu で apt-get を利用してインストール sudo apt-get install python-matplotlib ::CentOS で PIPを利用してインストール # pip install matplotlib # pip install python-dateutil # pip install pyparsing ::Windows で パッケージをダウンロードしてインストール *http://matplotlib.org/downloads.html からダウンロード ::Windows で PIPを利用してインストール *Microsoft Visual C++ Compiler for Python 2.7 **http://www.microsoft.com/en-us/download/confirmation.aspx?id=44266 *tcl ライブラリが見つからないエラー **http://typea.info/blg/glob/2015/07/python-windows-virtualenv-tcl.html **activate.bat に set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5" を追記 *[[PIP|Python WindowsにPIPをインストール]] C:\Python27\Scripts>pip install matplotlib C:\Python27\Scripts>pip install python-dateutil C:\Python27\Scripts>pip install pyparsing !!グラフウィンドウ !表示確認 *グラフ描画のバックエンドが何に設定されているのかとウィンドウの表示確認 >>> import matplotlib >>> matplotlib.get_backend() 'TkAgg' >>> import pylab >>> pylab.show() >>> pylab.figure() >>> pylab.show() {{ref_image 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の例 import matplotlib matplotlib.use("WebAgg") import numpy as np import matplotlib.pyplot as plt import random fig,ax = plt.subplots() ax.bar(np.arange(10),[random.randrange(0,10) for x in range(10)],1) plt.show() {{ref_image matplotlib_graph_window02.png}} !!例 ::例1 >>> from pylab import * >>> x = range(10) >>> y = [y1**2 for y1 in x] >>> plot(x, y, 'ro') [] >>> savefig('test.png') >>> show() {{ref_image matplotlib01.jpg}} ::例2 >>> from pylab import * >>> from numpy import * >>> x = arange(-3,3,.1) >>> y = [y1**3 for y1 in x] >>> plot(x,y) [] >>> show() {{ref_image matplotlib02.jpg}} !!コマンド http://matplotlib.org/api/pyplot_summary.html !!Examples *http://matplotlib.org/examples/index.html !!!サンプル !!棒グラフ *http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplots >>> import numpy as np >>> import matplotlib.pyplot as plt >>> import random >>> random_data = [random.randrange(0,10) for x in range(10)] >>> random_data [0, 1, 8, 4, 5, 9, 8, 0, 4, 7] >>> fig,ax = plt.subplots() >>> ax.bar(np.arange(10),random_data,1) >>> plt.show() {{ref_image matplotlib03.jpg}}