매번 업데이트하고 있습니다. 


* Multiple Plot 그릴 때 (특히 legend를 같이 그리고 싶을 때)

http://stackoverflow.com/questions/5484922/secondary-axis-with-twinx-how-to-add-to-legend


* Bar chart에서 bar 마다 색을 다르게 주기

http://stackoverflow.com/questions/18973404/settting-different-bar-color-in-matplotlib-python


* Scatter Plot 에서 point 에 색을 주기 (colormap 도 같이 있음)

http://stackoverflow.com/questions/8202605/matplotlib-scatterplot-colour-as-a-function-of-a-third-variable

http://matplotlib.org/examples/pylab_examples/scatter_demo2.html


* Plot에서 error를 색으로 칠하기

http://stackoverflow.com/questions/12957582/matplotlib-plot-yerr-xerr-as-shaded-region-rather-than-error-bars


* Legend 사이즈 조절하기

http://stackoverflow.com/questions/20048352/how-to-adjust-the-size-of-matplotlib-legend-box


* 그래프 퀴퉁이에 text 넣기

http://stackoverflow.com/questions/8482588/putting-text-in-top-left-corner-of-matplotlib-plot


* Colorbar 뒤집어 그리기 (cm에 있는 colorbar)

http://stackoverflow.com/questions/3279560/invert-colormap-in-matplotlib 

(가장 많은 추천의 답이 정답...!)


* 그림 png로 저장할 때 크기 및 해상도 조절하기

http://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib


* PolyCollection 사용하기 (Polygon을 그리고 색을 정해줄 수 있음)

http://matplotlib.org/examples/api/collections_demo.html


* Discrete color bar 만들기 (polygon 색칠할 때 색을 정해줄 경우, 이걸로 우회하여 해결... 어떻게 폴리곤마다 색을 지정해주는지는 아직 모르겠음..)

http://stackoverflow.com/questions/14777066/matplotlib-discrete-colorbar


* Discrete color bar 만들기2 -> 자신이 원하는 색으로 만드는 방법. 꽤 쉬움..

http://stackoverflow.com/questions/9707676/defining-a-discrete-colormap-for-imshow-in-matplotlib


* Axis 눈금을 동등하게 그리기

http://matplotlib.org/examples/pylab_examples/axis_equal_demo.html


* Imshow에서 square 로 plot 하기. 사실 aspect 를 조절하면 되는데, 이걸 계산해서 square로 만들어줘야 하니까... 좀 번거로움. 

http://stackoverflow.com/questions/7965743/python-matplotlib-setting-aspect-ratio



* 3D plot from data

http://stackoverflow.com/questions/4363857/matplotlib-color-in-3d-plotting-from-an-x-y-z-data-set-without-using-contour

http://stackoverflow.com/questions/12730436/matplotlib-plotting-non-uniform-data-in-3d-surface


*Scatter plot에서 point마다 label 주기

http://stackoverflow.com/questions/5147112/matplotlib-how-to-put-individual-tags-for-a-scatter-plot (Gorgeous!)

http://stackoverflow.com/questions/14432557/matplotlib-scatter-plot-with-different-text-at-each-data-point (Basic)

'Study > Computer' 카테고리의 다른 글

numpy array 잘라쓰기  (0) 2014.06.17
Python으로 n-gram 만들기  (0) 2014.05.22
위도, 경도 검색 프로그램  (0) 2014.05.09
Too many open files 에러가 로그에 남을 때  (0) 2014.04.07
py2exe로 pyQT4 코드 exe로 만들기  (0) 2014.03.13


6.1.8.py


Strogatz의 Nonlinear Dynamics 교과서에서 6장 연습문제에 보면 van der Pol oscillator가 나온다. 식은 다음과 같이 생겼다. 




이 미분방정식들의 Vector Field를 파이썬으로 그릴 수 있는데 최대한 matplotlib함수를 쓰지 않고(mgrid 등) 그리는 코드를 짰다. 거기에 덧붙여 임의의 점 하나를 잡고 trajectory를 시간에 대해 그리도록 하는 기능을 같이 넣었다. portrait 아래에 나오는 두개의 그래프는 x-t 그리고 y-t 그래프이다. 

trajectory를 따라가보면 시간이 조금 지난뒤에는 진동하는 모습을 보여주며 phase portrait에서도 볼 수 있다. 


Vector plot을 하게되면 trajectory가 뚜렷히 보이지 않는 단점이 있다. vector의 크기가 작을 경우 화살표가 상대적으로 작게 나와 잘 보이지 않기 때문인데, mathematica에서는 StreamPlot이라는 명령어로 trajectory들의 portrait를 살펴보기 쉽다. 하지만 matplotlib에서는 Stream을 plot해주는 함수가 없기 때문에 누군가 만들어 놓은 것이 있다. 아래 링크를 살펴보시면 된다.

http://stackoverflow.com/questions/8296617/how-to-plot-a-streamlines-when-i-know-u-and-v-components-of-velocitynumpy-2d


아래 코드에서간단히 1개의 trajectory를 그렸는데 이를 격자형태로 모두 계산해 그리면 Stream plot이 되는건 뻔한 이야기. -.- (하지만 이건 하고싶지 않아져서 패스)


프로그램을 실행시키면 아래와 같다. (이런걸 왜 넣고 있지.)

참! 예쁘게 나올 필요는 없어서 여백조정은 패스~(사실 이걸 알아보려고 또 20분남짓 소비하는게 싫어서...)




코드는 아래에~!


import numpy as np

import matplotlib.pyplot as plt


def xftn(x, y):

return y


def yftn(x, y):

tmp = -x + y*(1-x*x)

return tmp


def xRK4(x, y, dt):

    k1 = dt * xftn(x, y)

    k2 = dt * xftn(x + 0.5*k1, y + 0.5*k1)

    k3 = dt * xftn(x + 0.5*k2, y + 0.5*k2)

    k4 = dt * xftn(x + k3, y + k3)


    result = x + k1 / 6 + k2 / 3 + k3 / 3 + k4 / 6


    return result


def yRK4(x, y, dt):

    k1 = dt * yftn(x, y)

    k2 = dt * yftn(x + 0.5*k1, y + 0.5*k1)

    k3 = dt * yftn(x + 0.5*k2, y + 0.5*k2)

    k4 = dt * yftn(x + k3, y + k3)


    result = y + k1 / 6 + k2 / 3 + k3 / 3 + k4 / 6


    return result



Step = 0.1

xLeft = -2

xRight = 2

yTop = 2

yBottom = -2


SIZE = (int)((xRight-xLeft)/Step) + 1  #We only think square plane.


print("Step is", Step)


x = np.empty((SIZE,SIZE), float)

y = np.empty((SIZE,SIZE), float)


u = np.empty((SIZE,SIZE), float)

v = np.empty((SIZE,SIZE), float)


for i in range(SIZE):

for j in range(SIZE):

x[i][j] = xLeft + Step*i

y[i][j] = yBottom + Step*j

u[i][j] = xftn(x[i][j], y[i][j])

v[i][j] = yftn(x[i][j], y[i][j])


xPointList = [0.5]

yPointList = [0.5]

timeList   = [0]


dt = 0.01


for i in range(1500):

xPointList.append(xRK4(xPointList[i], yPointList[i], dt))

yPointList.append(yRK4(xPointList[i], yPointList[i], dt))

timeList.append(timeList[i]+dt)


ax1 = plt.subplot2grid((7,3), (0,0), rowspan=5, colspan=5)

ax1.quiver(x,y,u,v, angles='xy', pivot='middle')

ax1.plot(xPointList, yPointList)

plt.xlim(-2, 2)

plt.ylim(-2, 2)


ax2 = plt.subplot2grid((7,3), (5,0), colspan=3)

ax2.plot(timeList, xPointList)


ax3 = plt.subplot2grid((7,3), (6,0), colspan=3)

ax3.plot(timeList, yPointList)


plt.show()

파이썬으로 벡터장을 그려야 하는 경우가 있다. 심심하면 지도를 놓고 위에 풍향과 풍속을 그려볼 수도 있다. (기상청에서 데이터 받아서 그려보셔도 됩니다...정 심심하시면요...)


메뉴얼과 구글링을 통해서 더 깊이있는 방법들을 배울 수 있지만 가장 간단한 코딩은 아래와 같다.


import matplotlib.pyplot as plt

import numpy as np


x = np.empty((10,10), float)

y = np.empty((10,10), float)


u = np.empty((10,10), float)

v = np.empty((10,10), float)


for i in range(10):

    for j in range(10):

        x[i][j] =  i / 10.0

        y[i][j] =  j / 10.0

        u[i][j] =  i / 10 * 2 

        v[i][j] =  j / 10 * 2


print(x, y, u, v)


plt.quiver(x,y,u,v, angles='xy', pivot='middle')

plt.ylim(0,1.1)

plt.xlim(0,1.1)

plt.grid('on')

plt.show()


코드의 대부분은 그냥 정의하는거고....
벡터장을 그려주는 함수는 plt(matplotlib.pyplot)의 quiver라는 함수이다. 여기서 x, y는 벡터의 위치를 나타내며 u,v는 벡터의 성분을 말한다. angle은 벡터의 각도를 어디서부터 측정할꺼냐 그런 내용인데(아닐 수도 있음) 'xy'로 해야 우리가 상상하는 결과가 나온다. pivot은 벡터를 회전시킬 때 꼬리를 잡을지 머리를 잡을지 그것도 아니면 중간을 잡을지에 대한 옵션이다. 더 다양한 옵션은 공식 홈페이지를 참고하시고....

사실 mash를 이용해 u, v를 만들어도 된다. 여기서는 생략...

결과는 다음과 같다.



함수들의 자세한 arguments는 메뉴얼을 참고.


import matplotlib.pyplot (이외에도 많은 모듇들이 존재함)


1. 라벨붙이기.

pylab.xlabel('aaa')

pylab.ylabel('bbb')


2. 범위 정해주기

pylab.xlim(1, 100)

pylab.ylim(1, 100)


3. grid 보이기

pylab.grid('on' / 'off')


4. title 쓰기

pylab.title('aaaa')

'Study > Computer' 카테고리의 다른 글

Python으로 vector field그리기  (0) 2012.10.22
pylab과 pyplot의 차이?  (0) 2012.10.22
파일들을 gnuplot으로 한꺼번에 그리기  (0) 2012.10.03
python으로 중복된 tab없애기  (0) 2012.10.03
ssh 접속이 안될때  (0) 2012.09.21

+ Recent posts