pythonmatplotlibmatplotlib-3d

matplotlib 3D line plot


I am trying to plot a two series of data in 3d as lines but cant find a good example of how to structure the data to do this in python.

My example data is:

x = np.array([[1,1,1,1,5,1,1,1,1], [4.5,5,5,5,5.5,5,5,5,4.5]])

But I presume to do this I need 3 series of data.

Here is a sample of what the output should look like

enter image description here

I am assuming I need to add extra rows to the array, but not sure if I should try to build a 3d array or plot each axis with its own separate arrays?

In which case axis

y1 = np.ones(9)
z = np.array([0,1,2,3,4,5,6,7,8])

I did have a look here and read the documentation here but still could not work out how to apply it to what I am trying to do.

My attempt:

import matplotlib.pylab as pl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
pl.figure()

ax = pl.subplot(projection='3d')
data = np.array([[1,1,1,1,5,1,1,1,1], [4,5,5,5,5,5,5,5,4]])
y1 = np.ones(9)
z = np.array([1,2,3,4,5,6,7,8,9])

ax.plot(x, y, z, color = 'r')

UPDATE

This code displays the pseudo data, but ideally x would only have two axes - does it have to be axes of matching size? x just wants to have two ticks at 1, and another at 2.

import matplotlib.pyplot as pl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
pl.figure()

ax = pl.subplot(projection='3d')
z = np.array([[1,1,1,1,5,1,1,1,1], [1,4.5,5,5,5,5,5,5,4]])
x = np.ones(9)
y1 = np.array([1,2,3,4,5,6,7,8,9])
ax.plot(x, y1, z[0], color = 'r')
ax.plot(x*2, y1, z[1], color = 'g')

ax.set_xlabel('x')
ax.set_box_aspect(aspect = (1,1,2))

plot sample


Solution

  • Try this:

    import matplotlib.pyplot as pl
    from mpl_toolkits.mplot3d import Axes3D
    import numpy as np
    pl.figure(figsize = (12,12))
    
    ax = pl.subplot(projection='3d')
    z = np.array([[1,1,1,1,5,1,1,1,1], [0.5,4.5,5,4.8,5.1,5,5,5,4]])
    x = np.ones(9)
    y1 = np.array([1,2,3,4,5,6,7,8,9])
    ax.plot(x, y1, z[0], color = 'r')
    ax.plot(x*2, y1, z[1], color = 'g')
    
    ax.set_xlabel('x \n freq')
    ax.set_ylabel('y \n time (s)')
    ax.set_zlabel('z\n magnitude')
    
    ax.set_box_aspect(aspect = (0.5,1,1))
    ticks = np.array([0,1,2])
    ax.set_xticks(ticks) 
    

    enter image description here