GCA和Matplotlib的最新版本是什么?

38

我刚刚安装了最新版本的Matplotlib(3.4.1)。当我使用一些带有以下代码行的代码时

ax = fig.gca(projection='3d')
我收到以下警告:
Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. 
Starting two minor releases later, gca() will take no keyword arguments.  
The gca() function should only be used to get the current axes,
or if no axes exist, create new axes with default keyword arguments.
To create a new axes with non-default arguments,
use plt.axes() or plt.subplot().
我该如何避免让我的代码出现这个警告?

3
至少你收到了一个警告。我正在使用3.6.1版本,但是它会出现“gca() got an unexpected keyword argument 'projection'”的错误。不过改用add_subplot就解决了问题。 - yuranos
3个回答

73

您可以尝试:

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

9
fig = plt.figure()
plt.subplot(projection='3d')

plt.plot([0, v1[0]], [0, v1[1]], [0, v1[2]], 'b')
plt.plot([0, v2[0]], [0, v2[1]], [0, v2[2]], 'r')

或者

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

ax.plot([0, v1[0]], [0, v1[1]], [0, v1[2]], 'b')
ax.plot([0, v2[0]], [0, v2[1]], [0, v2[2]], 'r')

替代

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.plot([0, v1[0]], [0, v1[1]], [0, v1[2]], 'b')
ax.plot([0, v2[0]], [0, v2[1]], [0, v2[2]], 'r')

我希望这有所帮助,


0
fig = plt.figure()
ax =fig.add_subplot(projection='3d')

解决了错误,而且比其他方法更简洁。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接