2017년 12월 5일 화요일

Matplotlib을 이용한 파이썬 plotting

파이썬에서 그래프를 여러개 그려야할 때
(1) 하나의 창에 여러개의 그래프를 그리는 방법과
(2) 각각의 그래프를 각각의 창에 그리는 경우가 있을 수  있다.


1. 하나의 창에 여러개의 그래프를 그리는 방법 : subplot

import matplotlib.pyplot as plt
import numpy as np

csvData = np.loadtxt(Filename, delimiter=',' )
timeData = csvData[0:len(csvData)]

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.plot(timeData, label='sound data')
ax1.legend()

timeData = (timeData - np.mean(timeData))/maxSampleValue

ax2.plot(timeData, label='normalized sound data')
ax2.legend()
fig.show()


2. 각각의 그래프를 각각의 창에 그리는 경우

방법 #1
x = np.arange(5)
y = np.exp(x)
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(x, y)

z = np.sin(x)
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
ax2.plot(x, z)

w = np.cos(x)
ax1.plot(x, w)  # can continue plotting on the first axis

방법 #2
x = arange(5)
y = np.exp(x)
plt.figure(0)
plt.plot(x, y)

z = np.sin(x)
plt.figure(1)
plt.plot(x, z)

w = np.cos(x)
plt.figure(0)   # Here's the part I need
plt.plot(x, w)

Edit: Note that you can number the plots however you want (here, starting from 0) but if you don't provide figure with a number at all when you create a new one, the automatic numbering will start at 1 ("Matlab Style" according to the docs).

출처: <https://stackoverflow.com/questions/6916978/how-do-i-tell-matplotlib-to-create-a-second-new-plot-then-later-plot-on-the-o>


댓글 없음:

댓글 쓰기

람다 표현식 (Lambda expression)

람다 표현식(Lambda expression)  람다 표현식으로 함수를 정의하고, 이를 변수에 할당하여 변수를 함수처럼 사용한다. (1) 람다 표현식       lambda <매개변수> : 수식      ※ 람다식을 실행하...