ラベル matplotlib の投稿を表示しています。 すべての投稿を表示
ラベル matplotlib の投稿を表示しています。 すべての投稿を表示

2013年6月16日日曜日

[Python][matplotlib]区間横棒グラフ

0 から開始ではなく途中からの横棒グラフを描くには broken_barh() を使う
# -*- coding:utf-8 -*-
# broken_barh
import numpy
import matplotlib.pyplot

########################################################################
## Main function
########################################################################
ind = numpy.arange(8)
matplotlib.pyplot.axis([0, 10, 0, 10])
matplotlib.pyplot.grid(True)
# xranges: (xmin, xwidth)
# yrange: (ymin, ywidth)
# 左下:(1, 1), 右上:(2, 4)
matplotlib.pyplot.broken_barh([(1, 1)], (1, 3))
# 左下:(3, 3), 右上:(6, 4)
matplotlib.pyplot.broken_barh([(3, 3)], (3, 1), facecolor='red')
matplotlib.pyplot.savefig('test15.png')

[Python][matplotlib]横棒グラフ

横棒グラフを描く場合は barh() を使う
# -*- coding:utf-8 -*-
# barh
import numpy
import matplotlib.pyplot

########################################################################
## Main function
########################################################################
tokyo = [7.0, 6.5, 9.1, 12.4, 19.0, 23.6,
28.0, 29.6, 25.1, 18.9, 13.5, 9.9]
ind = numpy.arange(1, 13)

# 横棒グラフ
matplotlib.pyplot.barh(ind, tokyo)
matplotlib.pyplot.axis([0, 40, 1, 12])
matplotlib.pyplot.grid(True)
matplotlib.pyplot.savefig('test15-1.png')

[Python][matplotlib]Grid 付き棒グラフ

# -*- coding:utf-8 -*-
# Grid 付き
import numpy
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

temperature = (7.0, 29.6, 16.8, 28.9)
ind = numpy.arange(4)
print ind
width = 0.35
p1 = plt.bar(ind, temperature, width, color="b")

# フォントを指定して日本語表示
#prop = fm.FontProperties(fname='C:\\WINDOWS\\Fonts\\ipamp.ttf')
prop = fm.FontProperties(fname='/usr/share/fonts/ipa-pmincho/ipamp.ttf')
plt.xticks(ind + width / 2., (u'東京 1 月', u'東京 8 月', u'那覇 1 月', u'那覇 8 月'), fontproperties=prop)
plt.axis([-1, 4, 0, 35]) # axis([xmin, xmax, ymin, ymax])
plt.grid(True)
#plt.show()

# png 出力
plt.savefig('test06.png')

[Python][matplotlib]棒グラフ

棒グラフを描く場合は bar() を使用する
# -*- coding:utf-8 -*-
# 棒グラフ
import numpy
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

temperature = (7.0, 29.6, 16.8, 28.9)
ind = numpy.arange(4)
print ind
width = 0.35
p1 = plt.bar(ind, temperature, width, color="b")

# フォントを指定して日本語表示
#prop = fm.FontProperties(fname='C:\\WINDOWS\\Fonts\\ipamp.ttf')
prop = fm.FontProperties(fname='/usr/share/fonts/ipa-pmincho/ipamp.ttf')
plt.xticks(ind + width / 2., (u'東京 1 月', u'東京 8 月', u'那覇 1 月', u'那覇 8 月'), fontproperties=prop)
plt.axis([-1, 4, 0, 35]) # axis([xmin, xmax, ymin, ymax])
#plt.show()

# png 出力
plt.savefig('test05.png')

[Python][matplotlib]複数のファイルに出力する

figure() で figure object を複数作成し、それぞれ savefig() で画像ファイルに出力する
# -*- coding:utf-8 -*-
# 2 つのファイルに出力する
import numpy
import matplotlib.pyplot as plt

t1 = numpy.arange(0.0, 1.0, 0.02)
fig = plt.figure(1)

# 1 rows, 1 column, first plot
plt.subplot(1, 1, 1)
plt.title('sin()')
plt.plot(t1, numpy.sin(2 * numpy.pi * t1), 'k')

# png 出力
fig.savefig('test10-0.png')

# 新しい figure を作成、引数に 1 を渡すと上で作成した fig に追加でグラフを描く
fig = plt.figure(2)

# 1 rows, 1 column, first plot
plt.subplot(1, 1, 1)
plt.title('cos()')
plt.plot(t1, numpy.cos(2 * numpy.pi * t1), 'r')

# png 出力
fig.savefig('test10-1.png')

[Python][matplotlib]複数のグラフを 1 つのファイルにまとめる

figure() で figure object を作成し、そこに subplot で描画領域を区分けして plot していく
# -*- coding:utf-8 -*-
# 2 つのグラフを表示する
import numpy
import matplotlib.pyplot as plt

def f(t):
    return numpy.exp(-t) * numpy.cos(2 * numpy.pi * t)

t1 = numpy.arange(0.0, 5.0, 0.1)
t2 = numpy.arange(0.0, 5.0, 0.02)
fig = plt.figure(1)

# 2 rows, 1 column, first plot
plt.subplot(2, 1, 1)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

# 2 rows, 1 column, second plot
plt.subplot(2, 1, 2)
plt.plot(t2, numpy.cos(2 * numpy.pi * t2), 'r--')

# png 出力
fig.savefig('test08.png')


4 つ載せる場合
# -*- coding:utf-8 -*-
# 4 つのグラフを表示する
import numpy
import matplotlib.pyplot as plt

t1 = numpy.arange(0.0, 1.0, 0.02)
fig = plt.figure(1)

# 2 rows, 2 column, first plot
plt.subplot(2, 2, 1)
plt.title('sin()')
plt.plot(t1, numpy.sin(2 * numpy.pi * t1), 'k')

# 2 rows, 2 column, second plot
plt.subplot(2, 2, 2)
plt.title('cos()')
plt.plot(t1, numpy.cos(2 * numpy.pi * t1), 'r')

# 2 rows, 2 column, third plot
plt.subplot(2, 2, 3)
plt.title('tan()')
plt.plot(t1, numpy.tan(2 * numpy.pi * t1), 'b')

# 2 rows, 2 column, fourth plot
plt.subplot(2, 2, 4)
plt.title('log()')
plt.plot(t1, numpy.log(t1), 'g')

# png 出力
fig.savefig('test09.png')

[Python][matplotlib]出力するグラフのサイズを指定

  • figure() と subplot() を使用して出力サイズ (pixel) を指定する
  • figure で 4x3 inch に出力する指示をし、その figure object に subplot でグラフを描く
  • ファイル出力する時 (savefig) に、100dpi と指定すれば 400 x 300 pixel となる
# -*- coding:utf-8 -*-
# 400x300 pixel の画像として出力する
import numpy
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

temperature = (7.0, 29.6, 16.8, 28.9)
ind = numpy.arange(4)
print ind
width = 0.35
fig = plt.figure(figsize=(4, 3))
plt.subplot(111)
plt.bar(ind, temperature, width, color="b")

# フォントを指定して日本語表示
#prop = fm.FontProperties(fname='C:\\WINDOWS\\Fonts\\ipamp.ttf')
prop = fm.FontProperties(fname='/usr/share/fonts/ipa-pmincho/ipamp.ttf')
plt.xticks(ind + width / 2., (u'東京 1 月', u'東京 8 月', u'那覇 1 月', u'那覇 8 月'), fontproperties=prop)
plt.axis([-1, 4, 0, 35]) # axis([xmin, xmax, ymin, ymax])

# png 出力
fig.savefig('test07.png', dpi=100)

[Python][matplotlib]グラフを描く

plot() の第 3 引数で plot 描画の方法を指定する。
# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt

plt.plot([1,2,3,4],[1,4,9,18], 'ro') # r: red, o: circle marker
plt.plot([1,2,3,4],[3,5,15,2], 'b^') # b: blue, ^: triangle_up marker
plt.axis([0,5,0,20]) # axis([xmin, xmax, ymin, ymax])
plt.show()


numpy で生成した数列をグラフ化
# -*- coding:utf-8 -*-
# numpy で生成した数列をグラフ化
import numpy
import matplotlib.pyplot as plt

t = numpy.arange(0., 5., 0.2)
print t
plt.plot(t, t, 'r--')
plt.plot(t, t**2, 'bs') # 2 乗
plt.plot(t, t**3, 'g^') # 3 乗
plt.show()


Sine curve を描く
# -*- coding:utf-8 -*-
# Sine curve を描く
import numpy
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

t = numpy.arange(-numpy.pi, numpy.pi, 0.1)
print t

# フォントを指定して日本語表示
#prop = fm.FontProperties(fname='C:\\WINDOWS\\Fonts\\ipamp.ttf')
prop = fm.FontProperties(fname='/usr/share/fonts/ipa-pmincho/ipamp.ttf')
plt.title(u'Sine curve テスト', fontproperties=prop)
plt.plot(t, numpy.sin(t), 'k')
#plt.show()

# png 出力する場合はこちら
plt.savefig('test04.png')

[Python][matplotlib]インストール

Windows
  1. Numerical Python - Browse /NumPy/1.5.1 at SourceForge.net から numpy-1.5.1-win32-superpack-python2.7.exe をダウンロードしてインストールする
  2. matplotlib - Browse /matplotlib/matplotlib-1.0.1 at SourceForge.net から matplotlib-1.0.1.win32-py2.7.exe をダウンロードしてインストールする


Fedora
yum でインストール
# yum install numpy
# yum install python-matplotlib


動作確認
# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt

plt.plot([1,2,3,4],[1,4,9,18])
plt.ylabel('some numbers')
plot.show()
Fedora では問題なく動作したが、Windows の場合は以下のようなエラーが表示された。
> python test00.py
Traceback (most recent call last):
File "test00.py", line 2, in <module>
import matplotlib.pyplot as plt
File "C:\usr\Python27\lib\site-packages\matplotlib\pyplot.py", line 23, in <module>
from matplotlib.figure import Figure, figaspect
(略)
File "C:\usr\Python27\lib\site-packages\matplotlib\font_manager.py", line 214, in win32InstalledFonts
key, direc, any = _winreg.EnumValue( local, j)
MemoryError
これは matplotlib の backend (renderer) がデフォルトで tkagg backend を使うことになっているために発生している。agg backend に切り替えて動作させるとグラフ作成ができた。
# -*- coding:utf-8 -*-
import matplotlib
matplotlib.use('agg') # Backend を agg に変更する。matplatlib.pyplot を import する前にこれを実行すること
import matplotlib.pyplot as plt

plt.plot([1,2,3,4],[1,4,9,18])
plt.ylabel('some numbers')
plt.show()
plt.savefig('test01.png')

ここで Atcive Tcl を ActiveTcl Downloads - Tcl for Windows, Linux and Mac | ActiveState からダウンロード、インストールすると tkagg backend でも実行可能となる。