[[ノート/ノート]]~
訪問者数 &counter(); 最終更新 &lastmod();
*Oregano上のPythonで、Webに描画させたい [#s396e644]
**matplotlibを使う方法 [#hb7902b1]
***matplotlibは [#re0ead6c]
[[matplotlibとは:http://matplotlib.org/]]
[[matplotlibの例題(Examples):http://matplotlib.org/examples/index.html]]
[[matplotlibのチュートリアル:http://www.loria.fr/~rougier/teaching/matplotlib/]]
[[matplotlibのHowTo:http://matplotlib.org/faq/howto_faq.html]] と
[[HowToの中のWebアプリサーバーを作る方法:http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
[[Cookbook - MatPlotLibをCGI Scriptで使う:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]
***matplotlibのインストール(Linux) [#s9b056a5]
Numpyが必要
-[[Numpy:http://numpy.scipy.org/]]
-[[LinuxにNumpyをインストール:http://empty-garden.com/web/others/numpy_install.html]]
には python setup.py build して puthon setup.py install しろと書いてあるが、
NumpyのINSTALL.txtを見るとFortranの環境に合わせて python setup.py build --fcompiler=gnu95 のようにfcompilerを指定せよと書いてある。なので、このマシンの環境であるgfortranに合わせて指定してみた。
-Numpyのテストをするには、パッケージ nose が必要だが、それをインストールするツールとしてSetupToolsパッケージが必要。
--[[Setuptoolsのダウンロード:http://pypi.python.org/pypi/setuptools#files]] oregano上のpythonは2.6なので、py2.6.eggをダウンロード
--インストールは、sh setuptools-0.6c11-py2.6.egg
--次に [[nose:http://somethingaboutorange.com/mrl/projects/nose/]] 本体をインストール(setuptoolsで自動ダウンロード)するには、easy_install nose
-[[簡単な使い方:http://gihyo.jp/dev/serial/01/machine-learning/0006]]~
[[numpy:http://ibisforest.org/index.php?python%2Fnumpy]]
次にmatplotlibのインストール
-[[matplotlib:http://matplotlib.org]] の [[インストールのページ:http://matplotlib.org/users/installing.html]]
-ソースから作ることにして、[[ダウンロード先:http://sourceforge.net/project/showfiles.php?group_id=80706]]
-matplotlib-1.1.1を展開して、その下で python setup.py build して python setup.py install
で、matplotlibのテスト
>>> import matplotlib
>>> matplotlib.use('Agg')
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.plot([1,2,3])
>>> fig.savefig('test.png')
を実行すると、直線グラフが描けた。
次に、CGIとして動作させたいときのやり方。
-[[Cookbook / Matplotlib / Using MatPlotLib in a CGI script:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]~
サンプルプログラムをテスト
#!/usr/bin/python
import os,sys
import cgi
import cgitb; cgitb.enable()
# set HOME environment variable to a directory the httpd server can write to
os.environ[ 'HOME' ] = '/tmp/'
import matplotlib
# chose a non-GUI backend
matplotlib.use( 'Agg' )
import pylab
#Deals with inputing data into python from the html form
form = cgi.FieldStorage()
# construct your plot
pylab.plot([1,2,3])
print "Content-Type: image/png\n"
# save the plot as a png and output directly to webserver
pylab.savefig( sys.stdout, format='png' )
**[[matplotlibで日本語を表示する方法:http://www.sakito.com/2012/01/matplotlib.html]] [#zf238330]
*GnuPlotへリンクする方法 [#i4375e98]
[[Gnuplot.py:http://gnuplot-py.sourceforge.net/]]
[[ノート/ノート]]~
訪問者数 &counter(); 最終更新 &lastmod();
*Oregano上のPythonで、Webに描画させたい [#s396e644]
**matplotlibを使う方法 [#hb7902b1]
***matplotlibは [#re0ead6c]
[[matplotlibとは:http://matplotlib.org/]]
[[matplotlibの例題(Examples):http://matplotlib.org/examples/index.html]]
[[matplotlibのチュートリアル:http://www.loria.fr/~rougier/teaching/matplotlib/]]
[[matplotlibのHowTo:http://matplotlib.org/faq/howto_faq.html]] と
[[HowToの中のWebアプリサーバーを作る方法:http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
[[Cookbook - MatPlotLibをCGI Scriptで使う:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]
***matplotlibのインストール(Linux) [#s9b056a5]
Numpyが必要
-[[Numpy:http://numpy.scipy.org/]]
-[[LinuxにNumpyをインストール:http://empty-garden.com/web/others/numpy_install.html]]
には python setup.py build して puthon setup.py install しろと書いてあるが、
NumpyのINSTALL.txtを見るとFortranの環境に合わせて python setup.py build --fcompiler=gnu95 のようにfcompilerを指定せよと書いてある。なので、このマシンの環境であるgfortranに合わせて指定してみた。
-Numpyのテストをするには、パッケージ nose が必要だが、それをインストールするツールとしてSetupToolsパッケージが必要。
--[[Setuptoolsのダウンロード:http://pypi.python.org/pypi/setuptools#files]] oregano上のpythonは2.6なので、py2.6.eggをダウンロード
--インストールは、sh setuptools-0.6c11-py2.6.egg
--次に [[nose:http://somethingaboutorange.com/mrl/projects/nose/]] 本体をインストール(setuptoolsで自動ダウンロード)するには、easy_install nose
-[[簡単な使い方:http://gihyo.jp/dev/serial/01/machine-learning/0006]]~
[[numpy:http://ibisforest.org/index.php?python%2Fnumpy]]
次にmatplotlibのインストール
-[[matplotlib:http://matplotlib.org]] の [[インストールのページ:http://matplotlib.org/users/installing.html]]
-ソースから作ることにして、[[ダウンロード先:http://sourceforge.net/project/showfiles.php?group_id=80706]]
-matplotlib-1.1.1を展開して、その下で python setup.py build して python setup.py install
で、matplotlibのテスト
>>> import matplotlib
>>> matplotlib.use('Agg')
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.plot([1,2,3])
>>> fig.savefig('test.png')
を実行すると、直線グラフが描けた。
次に、CGIとして動作させたいときのやり方。
-[[Cookbook / Matplotlib / Using MatPlotLib in a CGI script:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]~
サンプルプログラムをテスト
#!/usr/bin/python
import os,sys
import cgi
import cgitb; cgitb.enable()
# set HOME environment variable to a directory the httpd server can write to
os.environ[ 'HOME' ] = '/tmp/'
import matplotlib
# chose a non-GUI backend
matplotlib.use( 'Agg' )
import pylab
#Deals with inputing data into python from the html form
form = cgi.FieldStorage()
# construct your plot
pylab.plot([1,2,3])
print "Content-Type: image/png\n"
# save the plot as a png and output directly to webserver
pylab.savefig( sys.stdout, format='png' )
**[[matplotlibで日本語を表示する方法:http://www.sakito.com/2012/01/matplotlib.html]] [#zf238330]
>参照
-[[matplotlib で日本語を表示する設定:http://www.sakito.com/2012/01/matplotlib.html]]
-[[matplotlibで日本語入りのグラフを書く:http://sucrose.hatenablog.com/entry/2013/06/10/001250]]
-[[matplotlibで、日本語を表示させるための3ステップ設定:http://plog.web-hack.org/2011/01/matplotlib.html]]
-[[Pythonのmatplotlibの日本語表示:http://blog.godo-tys.jp/2013/09/16/4381/]]
いろいろあるが、基本は、
1)フォントそのものを入れる必要がある。黙っていてVL Gothicフォントが入っているはず。~
ここ ...\Python26\Lib\site-packages\matplotlib\mpl-data\fonts\ttf にもコピーがいるという説あり。Linuxではそうでもなさそう。~
2)matplotlibのconfigファイルにフォント指定。基本は自前環境 ($HOME)/.matplotlib/matplotlibrc を修正。下記2行をFONTセクションに追加。
font.serif : VL Gothic
font.sans-serif : VL Gothic
3)漢字を表示したいプログラム上で、
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
... 好きなことをやって
x.hist() " ヒストグラムを生成
plt.title(u"図のタイトル", fontname="VL Gothic", size=10) #ここで漢字用のフォント指定
plt.show()
plt.savefig("filename.png")
plt.clf()
とすればよい。fontname指定を忘れると出ないようだ。デフォルトのフォント指定ができるのかも知れない。
*GnuPlotへリンクする方法 [#i4375e98]
[[Gnuplot.py:http://gnuplot-py.sourceforge.net/]]
[[ノート/ノート]]~
訪問者数 &counter(); 最終更新 &lastmod();
*Oregano上のPythonで、Webに描画させたい [#s396e644]
**matplotlibを使う方法 [#hb7902b1]
***matplotlibは [#re0ead6c]
[[matplotlibとは:http://matplotlib.org/]]
[[matplotlibの例題(Examples):http://matplotlib.org/examples/index.html]]
[[matplotlibのチュートリアル:http://www.loria.fr/~rougier/teaching/matplotlib/]]
[[matplotlibのHowTo:http://matplotlib.org/faq/howto_faq.html]] と
[[HowToの中のWebアプリサーバーを作る方法:http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
[[Cookbook - MatPlotLibをCGI Scriptで使う:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]
***matplotlibのインストール(Linux) [#s9b056a5]
Numpyが必要
-[[Numpy:http://numpy.scipy.org/]]
-[[LinuxにNumpyをインストール:http://empty-garden.com/web/others/numpy_install.html]]
には python setup.py build して puthon setup.py install しろと書いてあるが、
NumpyのINSTALL.txtを見るとFortranの環境に合わせて python setup.py build --fcompiler=gnu95 のようにfcompilerを指定せよと書いてある。なので、このマシンの環境であるgfortranに合わせて指定してみた。
-Numpyのテストをするには、パッケージ nose が必要だが、それをインストールするツールとしてSetupToolsパッケージが必要。
--[[Setuptoolsのダウンロード:http://pypi.python.org/pypi/setuptools#files]] oregano上のpythonは2.6なので、py2.6.eggをダウンロード
--インストールは、sh setuptools-0.6c11-py2.6.egg
--次に [[nose:http://somethingaboutorange.com/mrl/projects/nose/]] 本体をインストール(setuptoolsで自動ダウンロード)するには、easy_install nose
-[[簡単な使い方:http://gihyo.jp/dev/serial/01/machine-learning/0006]]~
[[numpy:http://ibisforest.org/index.php?python%2Fnumpy]]
次にmatplotlibのインストール
-[[matplotlib:http://matplotlib.org]] の [[インストールのページ:http://matplotlib.org/users/installing.html]]
-ソースから作ることにして、[[ダウンロード先:http://sourceforge.net/project/showfiles.php?group_id=80706]]
-matplotlib-1.1.1を展開して、その下で python setup.py build して python setup.py install
で、matplotlibのテスト
>>> import matplotlib
>>> matplotlib.use('Agg')
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.plot([1,2,3])
>>> fig.savefig('test.png')
を実行すると、直線グラフが描けた。
次に、CGIとして動作させたいときのやり方。
-[[Cookbook / Matplotlib / Using MatPlotLib in a CGI script:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]~
サンプルプログラムをテスト
#!/usr/bin/python
import os,sys
import cgi
import cgitb; cgitb.enable()
# set HOME environment variable to a directory the httpd server can write to
os.environ[ 'HOME' ] = '/tmp/'
import matplotlib
# chose a non-GUI backend
matplotlib.use( 'Agg' )
import pylab
#Deals with inputing data into python from the html form
form = cgi.FieldStorage()
# construct your plot
pylab.plot([1,2,3])
print "Content-Type: image/png\n"
# save the plot as a png and output directly to webserver
pylab.savefig( sys.stdout, format='png' )
**[[matplotlibで日本語を表示する方法:http://www.sakito.com/2012/01/matplotlib.html]] [#zf238330]
>参照
-[[matplotlib で日本語を表示する設定:http://www.sakito.com/2012/01/matplotlib.html]]
-[[matplotlibで日本語入りのグラフを書く:http://sucrose.hatenablog.com/entry/2013/06/10/001250]]
-[[matplotlibで、日本語を表示させるための3ステップ設定:http://plog.web-hack.org/2011/01/matplotlib.html]]
-[[Pythonのmatplotlibの日本語表示:http://blog.godo-tys.jp/2013/09/16/4381/]]
-[[matplotlib:http://space.geocities.jp/hamesspam/hes2013/130630.html]]
いろいろあるが、基本は、
1)フォントそのものを入れる必要がある。黙っていてVL Gothicフォントが入っているはず。~
ここ ...\Python26\Lib\site-packages\matplotlib\mpl-data\fonts\ttf にもコピーがいるという説あり。Linuxではそうでもなさそう。~
2)matplotlibのconfigファイルにフォント指定。基本は自前環境 ($HOME)/.matplotlib/matplotlibrc を修正。下記2行をFONTセクションに追加。
font.serif : VL Gothic
font.sans-serif : VL Gothic
3)漢字を表示したいプログラム上で、
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
... 好きなことをやって
x.hist() " ヒストグラムを生成
plt.title(u"図のタイトル", fontname="VL Gothic", size=10) #ここで漢字用のフォント指定
plt.show()
plt.savefig("filename.png")
plt.clf()
とすればよい。fontname指定を忘れると出ないようだ。デフォルトのフォント指定ができるのかも知れない。
***matplotlibで複数の図を1ページに書く方法 [#s8aa09e9]
>参照
-[[Matplotlibサンプル集:http://kaiseki-web.lhd.nifs.ac.jp/wiki/index.php/Matplotlib_%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E9%9B%86]]
-[[matplotlibのグラフの体裁を整える:http://d.hatena.ne.jp/y_n_c/20091122/1258842406]]
-[[matplotlib によるデータ可視化の方法:http://qiita.com/ynakayama/items/8d3b1f7356da5bcbe9bc]]
で、簡単には、subplotを使うのがよさそうとのこと。具体的には、
list = ["意欲_偏差値", "学びへの意識(%)", "進路総合(%)", "自己理解(%)"
, "6_復習をした", "5_宿題・課題はきちんとやった", "Q35 機会があれば、外国で勉強
や仕事をしてみたい"]
for u in list:
subplot(3, 3, i) # 画面を3×3に分割、その中のi番目の図をこれから描くぞ
plt.title(u.decode("utf-8"), fontname="VL Gothic", size=10)
x[u].hist()
plt.show() # 全部まとめて表示
plt.savefig("hist.png")
plt.clf()
だと、listが7要素なので3×3のうち7つだけ埋まる。
たくさん図を入れると、図と図の間隔を調整しなければならないことがある。予め
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.4)
などとすると、横方向の図の間隔を0.2に、縦方向を0.4にしてくれる。
*GnuPlotへリンクする方法 [#i4375e98]
[[Gnuplot.py:http://gnuplot-py.sourceforge.net/]]
[[ノート/ノート]]~
訪問者数 &counter(); 最終更新 &lastmod();
*Oregano上のPythonで、Webに描画させたい [#s396e644]
**matplotlibを使う方法 [#hb7902b1]
***matplotlibは [#re0ead6c]
[[matplotlibとは:http://matplotlib.org/]]
[[matplotlibの例題(Examples):http://matplotlib.org/examples/index.html]]
[[matplotlibのチュートリアル:http://www.loria.fr/~rougier/teaching/matplotlib/]]
[[matplotlibのHowTo:http://matplotlib.org/faq/howto_faq.html]] と
[[HowToの中のWebアプリサーバーを作る方法:http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
[[Cookbook - MatPlotLibをCGI Scriptで使う:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]
***matplotlibのインストール(Linux) [#s9b056a5]
Numpyが必要
-[[Numpy:http://numpy.scipy.org/]]
-[[LinuxにNumpyをインストール:http://empty-garden.com/web/others/numpy_install.html]]
には python setup.py build して puthon setup.py install しろと書いてあるが、
NumpyのINSTALL.txtを見るとFortranの環境に合わせて python setup.py build --fcompiler=gnu95 のようにfcompilerを指定せよと書いてある。なので、このマシンの環境であるgfortranに合わせて指定してみた。
-Numpyのテストをするには、パッケージ nose が必要だが、それをインストールするツールとしてSetupToolsパッケージが必要。
--[[Setuptoolsのダウンロード:http://pypi.python.org/pypi/setuptools#files]] oregano上のpythonは2.6なので、py2.6.eggをダウンロード
--インストールは、sh setuptools-0.6c11-py2.6.egg
--次に [[nose:http://somethingaboutorange.com/mrl/projects/nose/]] 本体をインストール(setuptoolsで自動ダウンロード)するには、easy_install nose
-[[簡単な使い方:http://gihyo.jp/dev/serial/01/machine-learning/0006]]~
[[numpy:http://ibisforest.org/index.php?python%2Fnumpy]]
次にmatplotlibのインストール
-[[matplotlib:http://matplotlib.org]] の [[インストールのページ:http://matplotlib.org/users/installing.html]]
-ソースから作ることにして、[[ダウンロード先:http://sourceforge.net/project/showfiles.php?group_id=80706]]
-matplotlib-1.1.1を展開して、その下で python setup.py build して python setup.py install
で、matplotlibのテスト
>>> import matplotlib
>>> matplotlib.use('Agg')
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.plot([1,2,3])
>>> fig.savefig('test.png')
を実行すると、直線グラフが描けた。
次に、CGIとして動作させたいときのやり方。
-[[Cookbook / Matplotlib / Using MatPlotLib in a CGI script:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]~
サンプルプログラムをテスト
#!/usr/bin/python
import os,sys
import cgi
import cgitb; cgitb.enable()
# set HOME environment variable to a directory the httpd server can write to
os.environ[ 'HOME' ] = '/tmp/'
import matplotlib
# chose a non-GUI backend
matplotlib.use( 'Agg' )
import pylab
#Deals with inputing data into python from the html form
form = cgi.FieldStorage()
# construct your plot
pylab.plot([1,2,3])
print "Content-Type: image/png\n"
# save the plot as a png and output directly to webserver
pylab.savefig( sys.stdout, format='png' )
**[[matplotlibで日本語を表示する方法:http://www.sakito.com/2012/01/matplotlib.html]] [#zf238330]
>参照
-[[matplotlib で日本語を表示する設定:http://www.sakito.com/2012/01/matplotlib.html]]
-[[matplotlibで日本語入りのグラフを書く:http://sucrose.hatenablog.com/entry/2013/06/10/001250]]
-[[matplotlibで、日本語を表示させるための3ステップ設定:http://plog.web-hack.org/2011/01/matplotlib.html]]
-[[Pythonのmatplotlibの日本語表示:http://blog.godo-tys.jp/2013/09/16/4381/]]
-[[matplotlib:http://space.geocities.jp/hamesspam/hes2013/130630.html]]
いろいろあるが、基本は、
1)フォントそのものを入れる必要がある。黙っていてVL Gothicフォントが入っているはず。~
ここ ...\Python26\Lib\site-packages\matplotlib\mpl-data\fonts\ttf にもコピーがいるという説あり。Linuxではそうでもなさそう。~
2)matplotlibのconfigファイルにフォント指定。基本は自前環境 ($HOME)/.matplotlib/matplotlibrc を修正。下記2行をFONTセクションに追加。
font.serif : VL Gothic
font.sans-serif : VL Gothic
3)漢字を表示したいプログラム上で、
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
... 好きなことをやって
x.hist() " ヒストグラムを生成
plt.title(u"図のタイトル", fontname="VL Gothic", size=10) #ここで漢字用のフォント指定
plt.show()
plt.savefig("filename.png")
plt.clf()
とすればよい。fontname指定を忘れると出ないようだ。デフォルトのフォント指定ができるのかも知れない。
***matplotlibで複数の図を1ページに書く方法 [#s8aa09e9]
>参照
-[[Matplotlibサンプル集:http://kaiseki-web.lhd.nifs.ac.jp/wiki/index.php/Matplotlib_%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E9%9B%86]]
-[[matplotlibのグラフの体裁を整える:http://d.hatena.ne.jp/y_n_c/20091122/1258842406]]
-[[matplotlib によるデータ可視化の方法:http://qiita.com/ynakayama/items/8d3b1f7356da5bcbe9bc]]
-[[matplotlib:http://seesaawiki.jp/met-python/d/matplotlib#content_9_32]]
で、簡単には、subplotを使うのがよさそうとのこと。具体的には、
list = ["意欲_偏差値", "学びへの意識(%)", "進路総合(%)", "自己理解(%)"
, "6_復習をした", "5_宿題・課題はきちんとやった", "Q35 機会があれば、外国で勉強
や仕事をしてみたい"]
for u in list:
subplot(3, 3, i) # 画面を3×3に分割、その中のi番目の図をこれから描くぞ
plt.title(u.decode("utf-8"), fontname="VL Gothic", size=10)
x[u].hist()
plt.show() # 全部まとめて表示
plt.savefig("hist.png")
plt.clf()
だと、listが7要素なので3×3のうち7つだけ埋まる。
たくさん図を入れると、図と図の間隔を調整しなければならないことがある。予め
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.4)
などとすると、横方向の図の間隔を0.2に、縦方向を0.4にしてくれる。
たくさん図を入れると、軸の目盛の文字のフォントが大きすぎることになる。これを修正するのに1つ1つ面倒だという話になるが、デフォルトを変えてしまうことができる。
> 参照 ⇒ [[matplotlib:http://seesaawiki.jp/met-python/d/matplotlib#content_9_32]] の下の方
一時的にデフォルト値を変更する。
一時的に値を変更することもできる。繰り返し操作を行う際などで便利。スクリプトか対話モードが終了するかデフォルトファイルの再読み込みをするまで有効。
import matplotlib as mpl
mpl.rc('lines', linewidth=2, color='r')
font = {'family' : 'monospace',
'weight' : 'bold',
'size' : 'larger'}
mpl.rc('font', **font)
実際に使ってみたのは、
font = {'family': 'VL Gothic',
'size': '9' }
matplotlib.rc('font', **font)
という感じ
*GnuPlotへリンクする方法 [#i4375e98]
[[Gnuplot.py:http://gnuplot-py.sourceforge.net/]]
[[ノート/ノート]]~
訪問者数 &counter(); 最終更新 &lastmod();
*Oregano上のPythonで、Webに描画させたい [#s396e644]
**matplotlibを使う方法 [#hb7902b1]
***matplotlibは [#re0ead6c]
[[matplotlibとは:http://matplotlib.org/]]
[[matplotlibの例題(Examples):http://matplotlib.org/examples/index.html]]
[[matplotlibのチュートリアル:http://www.loria.fr/~rougier/teaching/matplotlib/]]
[[matplotlibのHowTo:http://matplotlib.org/faq/howto_faq.html]] と
[[HowToの中のWebアプリサーバーを作る方法:http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
[[Cookbook - MatPlotLibをCGI Scriptで使う:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]
***matplotlibのインストール(Linux) [#s9b056a5]
Numpyが必要
-[[Numpy:http://numpy.scipy.org/]]
-[[LinuxにNumpyをインストール:http://empty-garden.com/web/others/numpy_install.html]]
には python setup.py build して puthon setup.py install しろと書いてあるが、
NumpyのINSTALL.txtを見るとFortranの環境に合わせて python setup.py build --fcompiler=gnu95 のようにfcompilerを指定せよと書いてある。なので、このマシンの環境であるgfortranに合わせて指定してみた。
-Numpyのテストをするには、パッケージ nose が必要だが、それをインストールするツールとしてSetupToolsパッケージが必要。
--[[Setuptoolsのダウンロード:http://pypi.python.org/pypi/setuptools#files]] oregano上のpythonは2.6なので、py2.6.eggをダウンロード
--インストールは、sh setuptools-0.6c11-py2.6.egg
--次に [[nose:http://somethingaboutorange.com/mrl/projects/nose/]] 本体をインストール(setuptoolsで自動ダウンロード)するには、easy_install nose
-[[簡単な使い方:http://gihyo.jp/dev/serial/01/machine-learning/0006]]~
[[numpy:http://ibisforest.org/index.php?python%2Fnumpy]]
次にmatplotlibのインストール
-[[matplotlib:http://matplotlib.org]] の [[インストールのページ:http://matplotlib.org/users/installing.html]]
-ソースから作ることにして、[[ダウンロード先:http://sourceforge.net/project/showfiles.php?group_id=80706]]
-matplotlib-1.1.1を展開して、その下で python setup.py build して python setup.py install
で、matplotlibのテスト
>>> import matplotlib
>>> matplotlib.use('Agg')
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.plot([1,2,3])
>>> fig.savefig('test.png')
を実行すると、直線グラフが描けた。
次に、CGIとして動作させたいときのやり方。
-[[Cookbook / Matplotlib / Using MatPlotLib in a CGI script:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]~
サンプルプログラムをテスト
#!/usr/bin/python
import os,sys
import cgi
import cgitb; cgitb.enable()
# set HOME environment variable to a directory the httpd server can write to
os.environ[ 'HOME' ] = '/tmp/'
import matplotlib
# chose a non-GUI backend
matplotlib.use( 'Agg' )
import pylab
#Deals with inputing data into python from the html form
form = cgi.FieldStorage()
# construct your plot
pylab.plot([1,2,3])
print "Content-Type: image/png\n"
# save the plot as a png and output directly to webserver
pylab.savefig( sys.stdout, format='png' )
**[[matplotlibで日本語を表示する方法:http://www.sakito.com/2012/01/matplotlib.html]] [#zf238330]
>参照
-[[matplotlib で日本語を表示する設定:http://www.sakito.com/2012/01/matplotlib.html]]
-[[matplotlibで日本語入りのグラフを書く:http://sucrose.hatenablog.com/entry/2013/06/10/001250]]
-[[matplotlibで、日本語を表示させるための3ステップ設定:http://plog.web-hack.org/2011/01/matplotlib.html]]
-[[Pythonのmatplotlibの日本語表示:http://blog.godo-tys.jp/2013/09/16/4381/]]
-[[matplotlib:http://space.geocities.jp/hamesspam/hes2013/130630.html]]
いろいろあるが、基本は、
1)フォントそのものを入れる必要がある。黙っていてVL Gothicフォントが入っているはず。~
ここ ...\Python26\Lib\site-packages\matplotlib\mpl-data\fonts\ttf にもコピーがいるという説あり。Linuxではそうでもなさそう。~
2)matplotlibのconfigファイルにフォント指定。基本は自前環境 ($HOME)/.matplotlib/matplotlibrc を修正。下記2行をFONTセクションに追加。
font.serif : VL Gothic
font.sans-serif : VL Gothic
3)漢字を表示したいプログラム上で、
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
... 好きなことをやって
x.hist() " ヒストグラムを生成
plt.title(u"図のタイトル", fontname="VL Gothic", size=10) #ここで漢字用のフォント指定
plt.show()
plt.savefig("filename.png")
plt.clf()
とすればよい。fontname指定を忘れると出ないようだ。デフォルトのフォント指定ができるのかも知れない。
***matplotlibで複数の図を1ページに書く方法 [#s8aa09e9]
>参照
-[[Matplotlibサンプル集:http://kaiseki-web.lhd.nifs.ac.jp/wiki/index.php/Matplotlib_%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E9%9B%86]]
-[[matplotlibのグラフの体裁を整える:http://d.hatena.ne.jp/y_n_c/20091122/1258842406]]
-[[matplotlib によるデータ可視化の方法:http://qiita.com/ynakayama/items/8d3b1f7356da5bcbe9bc]]
-[[matplotlib:http://seesaawiki.jp/met-python/d/matplotlib#content_9_32]]
で、簡単には、subplotを使うのがよさそうとのこと。具体的には、
list = ["意欲_偏差値", "学びへの意識(%)", "進路総合(%)", "自己理解(%)"
, "6_復習をした", "5_宿題・課題はきちんとやった", "Q35 機会があれば、外国で勉強
や仕事をしてみたい"]
for u in list:
subplot(3, 3, i) # 画面を3×3に分割、その中のi番目の図をこれから描くぞ
plt.title(u.decode("utf-8"), fontname="VL Gothic", size=10)
x[u].hist()
plt.show() # 全部まとめて表示
plt.savefig("hist.png")
plt.clf()
だと、listが7要素なので3×3のうち7つだけ埋まる。
たくさん図を入れると、図と図の間隔を調整しなければならないことがある。予め
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.4)
などとすると、横方向の図の間隔を0.2に、縦方向を0.4にしてくれる。
たくさん図を入れると、軸の目盛の文字のフォントが大きすぎることになる。これを修正するのに1つ1つ面倒だという話になるが、デフォルトを変えてしまうことができる。
> 参照 ⇒ [[matplotlib:http://seesaawiki.jp/met-python/d/matplotlib#content_9_32]] の下の方
一時的にデフォルト値を変更する。
一時的に値を変更することもできる。繰り返し操作を行う際などで便利。スクリプトか対話モードが終了するかデフォルトファイルの再読み込みをするまで有効。
一時的に値を変更することもできる。繰り返し操作を行う際などで便利。
スクリプトか対話モードが終了するかデフォルトファイルの再読み込みをするまで有効。
import matplotlib as mpl
mpl.rc('lines', linewidth=2, color='r')
font = {'family' : 'monospace',
'weight' : 'bold',
'size' : 'larger'}
mpl.rc('font', **font)
実際に使ってみたのは、
font = {'family': 'VL Gothic',
'size': '9' }
matplotlib.rc('font', **font)
という感じ
*GnuPlotへリンクする方法 [#i4375e98]
[[Gnuplot.py:http://gnuplot-py.sourceforge.net/]]
[[ノート/ノート]]~
訪問者数 &counter(); 最終更新 &lastmod();
*Oregano上のPythonで、Webに描画させたい [#s396e644]
[[なお、別のデータ処理用途で、mint上でヒストグラム描画にも使った>ノート/Python/Pandas〜Pythonで統計処理]]
**matplotlibを使う方法 [#hb7902b1]
***matplotlibは [#re0ead6c]
[[matplotlibとは:http://matplotlib.org/]]
[[matplotlibの例題(Examples):http://matplotlib.org/examples/index.html]]
[[matplotlibのチュートリアル:http://www.loria.fr/~rougier/teaching/matplotlib/]]
[[matplotlibのHowTo:http://matplotlib.org/faq/howto_faq.html]] と
[[HowToの中のWebアプリサーバーを作る方法:http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
[[Cookbook - MatPlotLibをCGI Scriptで使う:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]
***matplotlibのインストール(Linux) [#s9b056a5]
Numpyが必要
-[[Numpy:http://numpy.scipy.org/]]
-[[LinuxにNumpyをインストール:http://empty-garden.com/web/others/numpy_install.html]]
には python setup.py build して puthon setup.py install しろと書いてあるが、
NumpyのINSTALL.txtを見るとFortranの環境に合わせて python setup.py build --fcompiler=gnu95 のようにfcompilerを指定せよと書いてある。なので、このマシンの環境であるgfortranに合わせて指定してみた。
-Numpyのテストをするには、パッケージ nose が必要だが、それをインストールするツールとしてSetupToolsパッケージが必要。
--[[Setuptoolsのダウンロード:http://pypi.python.org/pypi/setuptools#files]] oregano上のpythonは2.6なので、py2.6.eggをダウンロード
--インストールは、sh setuptools-0.6c11-py2.6.egg
--次に [[nose:http://somethingaboutorange.com/mrl/projects/nose/]] 本体をインストール(setuptoolsで自動ダウンロード)するには、easy_install nose
-[[簡単な使い方:http://gihyo.jp/dev/serial/01/machine-learning/0006]]~
[[numpy:http://ibisforest.org/index.php?python%2Fnumpy]]
次にmatplotlibのインストール
-[[matplotlib:http://matplotlib.org]] の [[インストールのページ:http://matplotlib.org/users/installing.html]]
-ソースから作ることにして、[[ダウンロード先:http://sourceforge.net/project/showfiles.php?group_id=80706]]
-matplotlib-1.1.1を展開して、その下で python setup.py build して python setup.py install
で、matplotlibのテスト
>>> import matplotlib
>>> matplotlib.use('Agg')
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.plot([1,2,3])
>>> fig.savefig('test.png')
を実行すると、直線グラフが描けた。
次に、CGIとして動作させたいときのやり方。
-[[Cookbook / Matplotlib / Using MatPlotLib in a CGI script:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]~
サンプルプログラムをテスト
#!/usr/bin/python
import os,sys
import cgi
import cgitb; cgitb.enable()
# set HOME environment variable to a directory the httpd server can write to
os.environ[ 'HOME' ] = '/tmp/'
import matplotlib
# chose a non-GUI backend
matplotlib.use( 'Agg' )
import pylab
#Deals with inputing data into python from the html form
form = cgi.FieldStorage()
# construct your plot
pylab.plot([1,2,3])
print "Content-Type: image/png\n"
# save the plot as a png and output directly to webserver
pylab.savefig( sys.stdout, format='png' )
**[[matplotlibで日本語を表示する方法:http://www.sakito.com/2012/01/matplotlib.html]] [#zf238330]
>参照
-[[matplotlib で日本語を表示する設定:http://www.sakito.com/2012/01/matplotlib.html]]
-[[matplotlibで日本語入りのグラフを書く:http://sucrose.hatenablog.com/entry/2013/06/10/001250]]
-[[matplotlibで、日本語を表示させるための3ステップ設定:http://plog.web-hack.org/2011/01/matplotlib.html]]
-[[Pythonのmatplotlibの日本語表示:http://blog.godo-tys.jp/2013/09/16/4381/]]
-[[matplotlib:http://space.geocities.jp/hamesspam/hes2013/130630.html]]
いろいろあるが、基本は、
1)フォントそのものを入れる必要がある。黙っていてVL Gothicフォントが入っているはず。~
ここ ...\Python26\Lib\site-packages\matplotlib\mpl-data\fonts\ttf にもコピーがいるという説あり。Linuxではそうでもなさそう。~
2)matplotlibのconfigファイルにフォント指定。基本は自前環境 ($HOME)/.matplotlib/matplotlibrc を修正。下記2行をFONTセクションに追加。
font.serif : VL Gothic
font.sans-serif : VL Gothic
3)漢字を表示したいプログラム上で、
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
... 好きなことをやって
x.hist() " ヒストグラムを生成
plt.title(u"図のタイトル", fontname="VL Gothic", size=10) #ここで漢字用のフォント指定
plt.show()
plt.savefig("filename.png")
plt.clf()
とすればよい。fontname指定を忘れると出ないようだ。デフォルトのフォント指定ができるのかも知れない。
***matplotlibで複数の図を1ページに書く方法 [#s8aa09e9]
>参照
-[[Matplotlibサンプル集:http://kaiseki-web.lhd.nifs.ac.jp/wiki/index.php/Matplotlib_%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E9%9B%86]]
-[[matplotlibのグラフの体裁を整える:http://d.hatena.ne.jp/y_n_c/20091122/1258842406]]
-[[matplotlib によるデータ可視化の方法:http://qiita.com/ynakayama/items/8d3b1f7356da5bcbe9bc]]
-[[matplotlib:http://seesaawiki.jp/met-python/d/matplotlib#content_9_32]]
で、簡単には、subplotを使うのがよさそうとのこと。具体的には、
list = ["意欲_偏差値", "学びへの意識(%)", "進路総合(%)", "自己理解(%)"
, "6_復習をした", "5_宿題・課題はきちんとやった", "Q35 機会があれば、外国で勉強
や仕事をしてみたい"]
for u in list:
subplot(3, 3, i) # 画面を3×3に分割、その中のi番目の図をこれから描くぞ
plt.title(u.decode("utf-8"), fontname="VL Gothic", size=10)
x[u].hist()
plt.show() # 全部まとめて表示
plt.savefig("hist.png")
plt.clf()
だと、listが7要素なので3×3のうち7つだけ埋まる。
たくさん図を入れると、図と図の間隔を調整しなければならないことがある。予め
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.4)
などとすると、横方向の図の間隔を0.2に、縦方向を0.4にしてくれる。
たくさん図を入れると、軸の目盛の文字のフォントが大きすぎることになる。これを修正するのに1つ1つ面倒だという話になるが、デフォルトを変えてしまうことができる。
> 参照 ⇒ [[matplotlib:http://seesaawiki.jp/met-python/d/matplotlib#content_9_32]] の下の方
一時的にデフォルト値を変更する。
一時的に値を変更することもできる。繰り返し操作を行う際などで便利。
スクリプトか対話モードが終了するかデフォルトファイルの再読み込みをするまで有効。
import matplotlib as mpl
mpl.rc('lines', linewidth=2, color='r')
font = {'family' : 'monospace',
'weight' : 'bold',
'size' : 'larger'}
mpl.rc('font', **font)
実際に使ってみたのは、
font = {'family': 'VL Gothic',
'size': '9' }
matplotlib.rc('font', **font)
という感じ
*GnuPlotへリンクする方法 [#i4375e98]
[[Gnuplot.py:http://gnuplot-py.sourceforge.net/]]
[[ノート/ノート]]~
訪問者数 &counter(); 最終更新 &lastmod();
*Oregano上のPythonで、Webに描画させたい [#s396e644]
[[なお、別のデータ処理用途で、mint上でヒストグラム描画にも使った>ノート/Python/Pandas〜Pythonで統計処理]]
**matplotlibを使う方法 [#hb7902b1]
***matplotlibは [#re0ead6c]
[[matplotlibとは:http://matplotlib.org/]]
[[matplotlibの例題(Examples):http://matplotlib.org/examples/index.html]]
[[matplotlibのチュートリアル:http://www.loria.fr/~rougier/teaching/matplotlib/]]
[[matplotlibのHowTo:http://matplotlib.org/faq/howto_faq.html]] と
[[HowToの中のWebアプリサーバーを作る方法:http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
[[Cookbook - MatPlotLibをCGI Scriptで使う:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]
***matplotlibのインストール(Linux) [#s9b056a5]
Numpyが必要
-[[Numpy:http://numpy.scipy.org/]]
-[[LinuxにNumpyをインストール:http://empty-garden.com/web/others/numpy_install.html]]
には python setup.py build して puthon setup.py install しろと書いてあるが、
NumpyのINSTALL.txtを見るとFortranの環境に合わせて python setup.py build --fcompiler=gnu95 のようにfcompilerを指定せよと書いてある。なので、このマシンの環境であるgfortranに合わせて指定してみた。
-Numpyのテストをするには、パッケージ nose が必要だが、それをインストールするツールとしてSetupToolsパッケージが必要。
--[[Setuptoolsのダウンロード:http://pypi.python.org/pypi/setuptools#files]] oregano上のpythonは2.6なので、py2.6.eggをダウンロード
--インストールは、sh setuptools-0.6c11-py2.6.egg
--次に [[nose:http://somethingaboutorange.com/mrl/projects/nose/]] 本体をインストール(setuptoolsで自動ダウンロード)するには、easy_install nose
-[[簡単な使い方:http://gihyo.jp/dev/serial/01/machine-learning/0006]]~
[[numpy:http://ibisforest.org/index.php?python%2Fnumpy]]
次にmatplotlibのインストール
-[[matplotlib:http://matplotlib.org]] の [[インストールのページ:http://matplotlib.org/users/installing.html]]
-ソースから作ることにして、[[ダウンロード先:http://sourceforge.net/project/showfiles.php?group_id=80706]]
-matplotlib-1.1.1を展開して、その下で python setup.py build して python setup.py install
で、matplotlibのテスト
>>> import matplotlib
>>> matplotlib.use('Agg')
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.plot([1,2,3])
>>> fig.savefig('test.png')
を実行すると、直線グラフが描けた。
次に、CGIとして動作させたいときのやり方。
-[[Cookbook / Matplotlib / Using MatPlotLib in a CGI script:http://www.scipy.org/Cookbook/Matplotlib/Using_MatPlotLib_in_a_CGI_script]]~
サンプルプログラムをテスト
#!/usr/bin/python
import os,sys
import cgi
import cgitb; cgitb.enable()
# set HOME environment variable to a directory the httpd server can write to
os.environ[ 'HOME' ] = '/tmp/'
import matplotlib
# chose a non-GUI backend
matplotlib.use( 'Agg' )
import pylab
#Deals with inputing data into python from the html form
form = cgi.FieldStorage()
# construct your plot
pylab.plot([1,2,3])
print "Content-Type: image/png\n"
# save the plot as a png and output directly to webserver
pylab.savefig( sys.stdout, format='png' )
**[[matplotlibで日本語を表示する方法:http://www.sakito.com/2012/01/matplotlib.html]] [#zf238330]
>参照
-[[matplotlib で日本語を表示する設定:http://www.sakito.com/2012/01/matplotlib.html]]
-[[matplotlibで日本語入りのグラフを書く:http://sucrose.hatenablog.com/entry/2013/06/10/001250]]
-[[matplotlibで、日本語を表示させるための3ステップ設定:http://plog.web-hack.org/2011/01/matplotlib.html]]
-[[Pythonのmatplotlibの日本語表示:http://blog.godo-tys.jp/2013/09/16/4381/]]
-[[matplotlib:http://space.geocities.jp/hamesspam/hes2013/130630.html]]
いろいろあるが、基本は、
1)フォントそのものを入れる必要がある。黙っていてVL Gothicフォントが入っているはず。~
ここ ...\Python26\Lib\site-packages\matplotlib\mpl-data\fonts\ttf にもコピーがいるという説あり。Linuxではそうでもなさそう。~
2)matplotlibのconfigファイルにフォント指定。基本は自前環境 ($HOME)/.matplotlib/matplotlibrc を修正。下記2行をFONTセクションに追加。
font.serif : VL Gothic
font.sans-serif : VL Gothic
3)漢字を表示したいプログラム上で、
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
... 好きなことをやって
x.hist() " ヒストグラムを生成
plt.title(u"図のタイトル", fontname="VL Gothic", size=10) #ここで漢字用のフォント指定
plt.show()
plt.savefig("filename.png")
plt.clf()
とすればよい。fontname指定を忘れると出ないようだ。デフォルトのフォント指定ができるのかも知れない。
***matplotlibで複数の図を1ページに書く方法 [#s8aa09e9]
>参照
-[[Matplotlibサンプル集:http://kaiseki-web.lhd.nifs.ac.jp/wiki/index.php/Matplotlib_%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E9%9B%86]]
-[[matplotlibのグラフの体裁を整える:http://d.hatena.ne.jp/y_n_c/20091122/1258842406]]
-[[matplotlib によるデータ可視化の方法:http://qiita.com/ynakayama/items/8d3b1f7356da5bcbe9bc]]
-[[matplotlib:http://seesaawiki.jp/met-python/d/matplotlib#content_9_32]]
で、簡単には、subplotを使うのがよさそうとのこと。具体的には、
list = ["意欲_偏差値", "学びへの意識(%)", "進路総合(%)", "自己理解(%)"
, "6_復習をした", "5_宿題・課題はきちんとやった", "Q35 機会があれば、外国で勉強
や仕事をしてみたい"]
list = ["意欲_偏差値", "学びへの意識(%)", "進路総合(%)", "自己理解(%)",
"6_復習をした", "5_宿題・課題はきちんとやった", "Q35 機会があれば、外国で勉強や仕事をしてみたい"]
for u in list:
subplot(3, 3, i) # 画面を3×3に分割、その中のi番目の図をこれから描くぞ
plt.title(u.decode("utf-8"), fontname="VL Gothic", size=10)
x[u].hist()
plt.show() # 全部まとめて表示
plt.savefig("hist.png")
plt.clf()
だと、listが7要素なので3×3のうち7つだけ埋まる。
たくさん図を入れると、図と図の間隔を調整しなければならないことがある。予め
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.4)
などとすると、横方向の図の間隔を0.2に、縦方向を0.4にしてくれる。
たくさん図を入れると、軸の目盛の文字のフォントが大きすぎることになる。これを修正するのに1つ1つ面倒だという話になるが、デフォルトを変えてしまうことができる。
> 参照 ⇒ [[matplotlib:http://seesaawiki.jp/met-python/d/matplotlib#content_9_32]] の下の方
一時的にデフォルト値を変更する。
一時的に値を変更することもできる。繰り返し操作を行う際などで便利。
スクリプトか対話モードが終了するかデフォルトファイルの再読み込みをするまで有効。
import matplotlib as mpl
mpl.rc('lines', linewidth=2, color='r')
font = {'family' : 'monospace',
'weight' : 'bold',
'size' : 'larger'}
mpl.rc('font', **font)
実際に使ってみたのは、
font = {'family': 'VL Gothic',
'size': '9' }
matplotlib.rc('font', **font)
という感じ
*GnuPlotへリンクする方法 [#i4375e98]
[[Gnuplot.py:http://gnuplot-py.sourceforge.net/]]