gca and latest version of Matplotlib

Viewed 5651

I've just installed the latest version of Matplotlib (3.4.1). When I use some code with the following line

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

I get the following warning:

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

How do I get round this so my code does not give this warning?

2 Answers

You can try:

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
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')

or

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

instead of

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

I hope this was helpful,

Related