24

I am using matplotlib for doing this

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

fig = plt.figure()
ax = Axes3D(fig)

x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]

ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')

plt.show()

Now this builds a graph that is horizontal in the 3d space. How do I make the graph vertical so that it faces the user?

What I want to do is build multiple such vertical graphs that are separated by some distance and are facing the user.

Daniel G
  • 67,224
  • 7
  • 42
  • 42
Bruce
  • 33,927
  • 76
  • 174
  • 262
  • Come think of it, isn't it a bit silly to use 3D graphs to plot 2D data? – badp Feb 19 '10 at 09:16
  • 2
    I am varying a particular parameter alpha(not x and y) and observing the change in the graph for the various values of alpha – Bruce Feb 19 '10 at 09:25

4 Answers4

12

bp's answer might work fine, but there's a much simpler way.

Your current graph is 'flat' on the z-axis, which is why it's horizontal. You want it to be vertical, which means that you want it to be 'flat' on the y-axis. This involves the tiniest modification to your code:

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

fig = plt.figure()
ax = Axes3D(fig)

x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
# put 0s on the y-axis, and put the y axis on the z-axis
ax.plot(xs=x, ys=[0]*len(x), zs=y, zdir='z', label='ys=0, zdir=z')
plt.show()

Then you can easily have multiple such graphs by using different values for the ys parameter (for example, ys=[2]*len(x) instead would put the graph slightly behind).

Daniel G
  • 67,224
  • 7
  • 42
  • 42
7

You can set the view angle of the 3d plot with the view_init() function. The example below is for version 1.1 of matplotlib.

from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

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

x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]

ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
ax.view_init(90, -90)
plt.show()
Ben
  • 1,038
  • 1
  • 19
  • 22
7

Mayavi, in particular the mlab module, provides powerful 3D plotting that will work on large and or complex data, and should be easy to use on numpy arrays.

Gael Varoquaux
  • 2,466
  • 2
  • 24
  • 12
3

According to the documentation you want to use the ax.plot_surface(x,y,z) method. More information and chart types here.

The following should work:

x = [1,2,3]
y = [4,5,6]
z = [7,8,9]
data = zip(x,y,z)

#map data on the plane
X, Y = numpy.meshgrid(arange(0, max(x), 1), arange(0, max(y), 1))
Z = numpy.zeros((len(Y), len(X)), 'Float32')
for x_,y_,z_ in data:
  Z[x_, y_] = z_ #this should work, but only because x and y are integers
                 #and arange was done with a step of 1, starting from 0
fig = p.figure()
ax = p3.Axes3D(fig)
ax.plot_surface(X, Y, Z)
badp
  • 11,409
  • 3
  • 61
  • 89