아래 링크에서 matplotlib이라는 모듈을 써서 3차원 플롯을 그리는 법을 알게 되었다.
http://www.gisdeveloper.co.kr/?p=7353
위 링크에 따르면 코드와 그 결과는 다음과 같았다.
코드>
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
Z = X**2 + Y**2
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_wireframe(X, Y, Z, color='black')
plt.show()
결과>
하지만 실제 파이참에서 실행해본 결과, 에러가 발생했다.
TypeError: FigureBase.gca() got an unexpected keyword argument 'projection'
stackoverflow에 따르면 두가지 방법이 있었다.
1.matplotlib의 버전을 2.0.2로 하향 업데이트 하기
2.gca()대신 add_subplot() 사용하기
나는 후자를 택했다.
수정된 코드>
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
Z = X**2 + Y**2
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
surf = ax.plot_wireframe(X, Y, Z, color='black')
plt.show()
출력결과>
출처:
댓글