I have a matplotlib figure that I want to be able to switch between 2D and 3D projections. I can go from 2D to 3D but I can't seem to work out how to go the other way. Example...
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def randrange(n, vmin, vmax):
return (vmax-vmin)*np.random.rand(n) + vmin
fig = plt.figure()
# Create a 3D scatter plot...
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zl, zh)
ax.scatter(xs, ys, zs, c=c, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
# Now I want a 2D plot...
ax.cla()
ax = fig.add_subplot(111)
ax.plot(xs, ys)
plt.show()
The plot stays in the 3D projection and projection="2D" isn't a valid kwarg...
I thought perhaps ax.clf() would do what I wanted and let me define a new figure. But it just gives me the following error: ValueError: Unknown element o
Can anyone give me a hint as to the solution to this? Is the ValueError related to the problem or a hint to something else wrong with my setup? Is there a kwarg to switch the projection from 3D to 2D?