Sleepyhead
Sleepyhead

Reputation: 1021

Set 3d view angle with view_init in a subplot

I am trying to change the angle in one of the subplots of my figure which is a 3d plot. I do:

import matplotlib.pyplot as plt
f1 = plt.figure()
ax1 = f1.add_subplot(2, 1, 1, projection='3d')
ax1.view_init(20, -120)

But this doesn't change the view. What am I doing wrong?

Upvotes: 8

Views: 26236

Answers (1)

finefoot
finefoot

Reputation: 11234

After adding

from mpl_toolkits.mplot3d import Axes3D

to your imports, your code should work just fine. Here is the full code I used:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
f1 = plt.figure()
ax1 = f1.add_subplot(2, 1, 1, projection=Axes3D.name)
ax1.view_init(20, -120)
plt.show()

This shows:

Figure 1

And to compare it with another view, with ax1.view_init(-120, 20) for example, it shows:

Figure 2

By the way, a linter might complain about "unused import" Axes3D, so instead of projection='3d' I wrote projection=Axes3D.name in my code above. See How to directly use Axes3D from matplotlib in standard plot to avoid flake8 error for further reading.

Upvotes: 8

Related Questions