相同图中的2D和3D轴

此示例显示如何在同一图上绘制2D和3D绘图。

相同图中的2D和3D轴示例

  1. # This import registers the 3D projection, but is otherwise unused.
  2. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. def f(t):
  6. s1 = np.cos(2*np.pi*t)
  7. e1 = np.exp(-t)
  8. return np.multiply(s1, e1)
  9. # Set up a figure twice as tall as it is wide
  10. fig = plt.figure(figsize=plt.figaspect(2.))
  11. fig.suptitle('A tale of 2 subplots')
  12. # First subplot
  13. ax = fig.add_subplot(2, 1, 1)
  14. t1 = np.arange(0.0, 5.0, 0.1)
  15. t2 = np.arange(0.0, 5.0, 0.02)
  16. t3 = np.arange(0.0, 2.0, 0.01)
  17. ax.plot(t1, f(t1), 'bo',
  18. t2, f(t2), 'k--', markerfacecolor='green')
  19. ax.grid(True)
  20. ax.set_ylabel('Damped oscillation')
  21. # Second subplot
  22. ax = fig.add_subplot(2, 1, 2, projection='3d')
  23. X = np.arange(-5, 5, 0.25)
  24. Y = np.arange(-5, 5, 0.25)
  25. X, Y = np.meshgrid(X, Y)
  26. R = np.sqrt(X**2 + Y**2)
  27. Z = np.sin(R)
  28. surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
  29. linewidth=0, antialiased=False)
  30. ax.set_zlim(-1, 1)
  31. plt.show()

下载这个示例