matplotlib 如何在3D曲面图上高亮显示切片?

ufj5ltwl  于 2023-10-24  发布在  其他
关注(0)|答案(2)|浏览(135)

我有一个3D表面图。我也想在2D中绘制这个图的切片,并以某种方式在3D图上指示切片的来源(例如为切片上的沿着点着色以“突出显示”切片,或绘制相交平面或其他东西)。
下面是一个示例,我只是将特定行设置为0,这样我就可以看到切片在3D图上的位置。

  1. import numpy as np
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import matplotlib.pyplot as plt
  4. # Grid and test function
  5. N = 29;
  6. x,y = np.linspace(-1,1, N*2), np.linspace(-1,1, N)
  7. X,Y = np.meshgrid(x,y)
  8. F = lambda X,Y : np.sin(10*X)/(1+5*(X**2+Y**2))
  9. Z = F(X,Y)
  10. # 3D Surface plot
  11. plt.figure(figsize = (5,6))
  12. Z2 = Z.copy(); Z2[10,:] = 0 # <----- Replace this code
  13. ax = plt.subplot(211, projection='3d')
  14. ax.plot_surface(X,Y,Z2)
  15. # 2D Plot of slice of 3D plot
  16. plt.subplot(212)
  17. plt.plot(x,Z[10,:])
  18. plt.show()
  19. plt.savefig('surfacePlotHighlight.png')

enyaitl3

enyaitl31#

您可以使用plot_surface中的facecolor选项以及plot中的类似颜色设置在X或Y方向上对切片进行着色。

  1. import numpy as np
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import matplotlib.pyplot as plt
  4. # Grid and test function
  5. N = 29;
  6. x,y = np.linspace(-1,1, N*2), np.linspace(-1,1, N)
  7. X,Y = np.meshgrid(x,y)
  8. F = lambda X,Y : np.sin(10*X)/(1+5*(X**2+Y**2))
  9. Z = F(X,Y)
  10. # 3D Surface plot
  11. plt.figure(figsize = (5,6))
  12. ax = plt.subplot(211, projection='3d')
  13. # Normalise Y for calling in the cmap.
  14. Ys = Y/Y.max()
  15. cmap = plt.cm.viridis
  16. ax.plot_surface(X, Y, Z2, facecolors=cmap(Ys))
  17. # 2D Plot of slice of 3D plot
  18. # Normalise y for calling in the cmap.
  19. ys = y/y.max()
  20. plt.subplot(212)
  21. plt.plot(x,Z[10,:], color=cmap(ys[10]))
  22. plt.plot(x,Z[20,:], color=cmap(ys[20]))
  23. plt.show()
  24. plt.savefig('surfacePlotHighlight.png')

**EDIT:**这可以用来突出显示单行(或列,或任意一组点),通过编辑颜色数组来调出特定的单元格,例如:

  1. # 3D Surface plot
  2. plt.figure(1,figsize = (5,6))
  3. ax = plt.subplot(211, projection='3d')
  4. # Create array to specify color of each pixel on surface
  5. Ys = Y*0
  6. Ys[:,:] = .3
  7. Ys[10] = 1
  8. Ys[20] = .7
  9. cmap = plt.cm.viridis
  10. ax.plot_surface(X, Y, Z, facecolors=cmap(Ys))
  11. # 2D Plot of slice of 3D plot
  12. # Normalise y for calling in the cmap.
  13. ys = Ys[:,0]
  14. plt.subplot(212)
  15. plt.plot(x,Z[10,:], color=cmap(ys[10]))
  16. plt.plot(x,Z[20,:], color=cmap(ys[20]))
  17. plt.show()
  18. plt.savefig('surfacePlotHighlight.png')

展开查看全部
y0u0uwnf

y0u0uwnf2#

您可以对显示为与其他行不同颜色的行进行着色。

  1. import numpy as np
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import matplotlib.pyplot as plt
  4. # Grid and test function
  5. N = 29;
  6. x,y = np.linspace(-1,1, N*2), np.linspace(-1,1, N)
  7. X,Y = np.meshgrid(x,y)
  8. F = lambda X,Y : np.sin(10*X)/(1+5*(X**2+Y**2))
  9. Z = F(X,Y)
  10. y0 = 10
  11. norm=plt.Normalize(Z.min(), Z.max())
  12. C = plt.cm.Blues_r(norm(Z)/2)
  13. C[y0] = plt.cm.Reds_r(norm(Z[y0])/2)
  14. # 3D Surface plot
  15. plt.figure(figsize = (5,6))
  16. ax = plt.subplot(211, projection='3d')
  17. ax.plot_surface(X,Y,Z, facecolors=C)
  18. # 2D Plot of slice of 3D plot
  19. plt.subplot(212)
  20. plt.plot(x,Z[y0,:], color=plt.cm.Reds(.7))
  21. plt.show()

展开查看全部

相关问题