Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : How to plot 3d graphs using Python?

Tags:

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.

like image 520
Bruce Avatar asked Feb 19 '10 06:02

Bruce


People also ask

Can Matplotlib be used for 3D plotting?

3D plotting in Matplotlib starts by enabling the utility toolkit. We can enable this toolkit by importing the mplot3d library, which comes with your standard Matplotlib installation via pip. Just be sure that your Matplotlib version is over 1.0. Now that our axes are created we can start plotting in 3D.

How do you plot 3 axis in Python?

Using subplots() method, create a figure and a set of subplots. Plot [1, 2, 3, 4, 5] data points on the left Y-axis scales. Using twinx() method, create a twin of Axes with a shared X-axis but independent Y-axis, ax2. Plot [11, 12, 31, 41, 15] data points on the right Y-axis scale, with blue color.

How do you plot a 3D scatter in Python?

Generally 3D scatter plot is created by using ax. scatter3D() the function of the matplotlib library which accepts a data sets of X, Y and Z to create the plot while the rest of the attributes of the function are the same as that of two dimensional scatter plot.


1 Answers

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

like image 61
Daniel G Avatar answered Oct 12 '22 00:10

Daniel G