Search code examples
pythonmatplotlibline-plotmatplotlib-3d

Plot 3D line plot with breaks


I have three arrays formed by following lines:

x = np.tile(np.arange(0,200), 14)
y = np.random.randint(200, size=2800)
z = np.repeat(np.arange(1,15), 200)

and I use the following code to have 3D plot:

ax = plt.axes(projection='3d')
ax.plot3D(x, z, y)

The resulting plot looks like this: enter image description here

But here you can see there are lines connecting in the middle. how can I introduce breaks in the middle?


Solution

  • enter image description here

    You don't want to plot a single curve, you want to plot 14 different curves, the x is always the same, the z is almost the same, what is really changing is the y's.

    So I'd suggest to use the same x, the same z and a ys array, shaped (14, 200) and have an enumerated loop on ys's rows, as follows

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    x = np.arange(0,200)
    ys = np.sin(x*(0.4+0.6*np.random.rand(14))[:,None]/10)
    z = np.ones(200)
    fig, ax = plt.subplots(subplot_kw={"projection":"3d"})
    for n, y in enumerate(ys): 
        ax.plot3D(x, z+n, y, color=['blue', 'red'][n%2])