8

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?

finefoot
  • 9,914
  • 7
  • 59
  • 102
Sleepyhead
  • 1,009
  • 1
  • 10
  • 27
  • 5
    Try using `ax = Axes3D(fig)` instead of `add_subplot(2,1,1, projection='3d')`. For me using [this](http://matplotlib.sourceforge.net/mpl_examples/mplot3d/lines3d_demo.py) demo code, but replacing 'fig.gca(projection='3d')' for `Axes3d(fig)` and then adding `ax.view_init(20,-120)`, it works fine. – fraxel Apr 28 '12 at 09:49

1 Answers1

8

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.

finefoot
  • 9,914
  • 7
  • 59
  • 102