TroubleShooting

TypeError: FigureBase.gca() got an unexpected keyword argument 'projection' 해결방법

JackSmith 2023. 10. 9.

아래 링크에서 matplotlib이라는 모듈을 써서 3차원 플롯을 그리는 법을 알게 되었다.

http://www.gisdeveloper.co.kr/?p=7353 

 

파이썬의 matplotlib 노트 – GIS Developer

파이썬의 matplotlib는 수치 데이터를 그래프로 효과적으로 표시해주는 API입니다. 이에 대해 간단한 활용 예시에 대한 코드를 기록해 둡니다. import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [1, 2, 3, 4, 5]

www.gisdeveloper.co.kr

위 링크에 따르면 코드와 그 결과는 다음과 같았다.

코드>

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()

출력결과>

 

출처:

https://stackoverflow.com/questions/76047803/typeerror-figurebase-gca-got-an-unexpected-keyword-argument-projection

 

TypeError: FigureBase.gca() got an unexpected keyword argument 'projection'

axes1 = fig.gca(projection = '3d') 1 axes1 = fig.add_axes(Axes3D(fig)) 2 axes1 = fig.gca(projection=Axes3D.name) 3 axes1 = fig.add_subplot(projection='3d') These are all the solutions i found dur...

stackoverflow.com

 

댓글