Python中 Matplotlib绘制绘部分曲面图(如:L型曲面图)?

想用Matplotlib绘制如下的局部"表面图

请注意,它不是X-Y平面上的完整网格,但是在顶视图中缺少一个角.以下是我试过但没有用的代码.

将 numpy 导入为 np从 matplotlib 导入 pyplot从mpl_toolkits.mplot3d导入Axes3DX = np.array([[0,1][0,1,2][0,1,2,3]])Y = np.array([[0,0],[1,1,1],[2,2,2,2]])Z = np.array([[0.5, 0.6],[0.7, 0.8, 0.9],[1.0, 1.1, 1.2, 1.3],])无花果= pyplot.figure()ax = fig.add_subplot(111, 投影='3d')ax.plot_surface(X,Y,Z)

错误为:
<块引用>
ValueError:设置具有序列的数组元素.

任何指针将不胜感激!谢谢!

解决方案
您可以通过在不想绘制的区域中使用Z的 np.nan 值来轻松实现此目的.这是

from mpl_toolkits.mplot3d 导入 Axes3D从 matplotlib 导入 cm从matplotlib.ticker导入LinearLocator,FormatStrFormatter导入matplotlib.pyplot作为plt将numpy导入为np无花果= plt.figure()ax = fig.gca(投影='3d')X = np.arange(-550.25)Y = np.arange(-550.25)X, Y = np.meshgrid(X, Y)R = np.sqrt(X**2 + Y**2)Z = np.sin(.5*R)Z[X+Y>4.] = np.nan # 对角线切片surf = ax.plot_surface(X,Y,Z,rstride = 1,cstride = 1,cmap = cm.coolwarm,线宽=0,抗锯齿=假,vmin=-1,vmax=1)ax.set_zlim(-1.01, 1.01)ax.zaxis.set_major_locator(LinearLocator(10))ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))fig.colorbar(冲浪,收缩=0.5,方面=5)plt.show()

这里还要注意,我必须在 plot 命令中使用 vmin 和 vmax 关键字,否则 nans 会抛出颜色缩放.

I wanted to plot a “partial” surface plot like the following one with Matplotlib

Note that it’s not a complete meshgrid on X-Y plane but missing a corner from top view. The following is the code I tried but didn’t work.

import numpy as np
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D

X = np.array([[0,1],
              [0,1,2],
              [0,1,2,3],
             ])
Y = np.array([[0,0],
              [1,1,1],
              [2,2,2,2],
             ])
Z = np.array([[0.5, 0.6],
              [0.7, 0.8, 0.9],
              [1.0, 1.1, 1.2, 1.3],
             ])
fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X,Y,Z)

The error being:

ValueError: setting an array element with a sequence.

Any pointer would be appreciated! Thanks!

解决方案
You can do this easily by using np.nan values for Z in the regions you don’t want to plot. Here’s a modified version of this example but with the cut, as show below:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(.5*R)

#X[X+Y>4.] = np.nan  # the diagonal slice,

Z[X+Y>4.] = np.nan  # the diagonal slice

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False, vmin=-1, vmax=1)
ax.set_zlim(-1.01, 1.01)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

Note here also that I had to use vmin and vmax keywords in the plot command or the color scaling would be thrown by the nans.

见https://www.it1352.com/2297775.html

https://blog.csdn.net/waitingwinter/article/details/112217830

你可能感兴趣的:(Python语言专栏,Matlab语言专栏,android,插值,算法)