LaTeX

Matplotlib 으로 플로팅 하기 2

ForceCore 2011. 1. 3. 22:39
http://forcecore.tistory.com/1200
저번에 쓴 글은 pylab 인터페이스를 쓰지 않아서 코드가 매우 복잡했다. 이번은 pylab인터페이스를 써서 시범을 보이겠다. pylab인터페이스를 쓰면 매트랩과 유사한 코드로 플로팅을 할 수 있다.

#!/usr/bin/env python2
from pylab import *

# Data
nums = [1,2,3,4,5,6,7]
rgbhsv = [56.42, 62.88, 67.26, 68.66, 66.4, 68.74, 67.24]
rgb = [53.8, 61.76, 64.98, 65.64, 65.46, 64.74, 65.72]
hsv = [56.22, 63.22, 65.62, 67.1, 65.82, 67.18, 66.18]

# Plot data
plot( nums, rgbhsv, '^-' )
plot( nums, rgb, 'o-' )
plot( nums, hsv, 's-' )

# Axis stuff
axis([1,7,52,70])
xlabel("Number of Colors")
ylabel("Accuracy (%)")

# add legend
legend( ( "RGB+HSV", "RGB only", "HSV only" ), 'lower right' )

# adjust margins
subplots_adjust( left=0.1, right=0.95, bottom=0.1, top=0.95 )

if 0:
# show on screen
show()
else:
# save as eps
savefig( "colorcnt.eps" )

savefig 은 show()를 한 다음엔 안 먹힌다. 1회의 스크립트 실행에선 savefig, show() 둘 중 하나만 쓸 수 있단 얘기.

savefig은 확장자를 자동으로 인식한다. png, eps, pdf 등 흔히 쓸만한건 매우 잘 된다.

결과물:


참 쉽죠?