Skip to content

Instantly share code, notes, and snippets.

@andy23512
Last active January 31, 2018 11:28
Show Gist options
  • Save andy23512/dda305a40595e5caefe12e902ec03dad to your computer and use it in GitHub Desktop.
Save andy23512/dda305a40595e5caefe12e902ec03dad to your computer and use it in GitHub Desktop.
matplotlib: plot multiple 2d line in 3d
"""
=======================
Plot 2D data on 3D plot
=======================
Demonstrates using ax.plot's zdir keyword to plot 2D data on
selective axes of a 3D plot.
"""
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
# Plot a sin curve using the x and y axes.
for z in range(0, 50):
x = np.linspace(0, 1, 100)
y = z * np.sin(x * 2 * np.pi) / 50 / 2 + 0.5
ax.plot(x, y, zs=z, zdir='z', label='curve in (x,y)')
# Make legend, set axes limits and labels
ax.legend()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 50)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# Customize the view angle so it's easier to see that the scatter points lie
# on the plane y=0
ax.view_init(elev=20., azim=-35)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment