非矩形域上的Python 3D绘图

非矩形域上的Python 3D绘图,python,numpy,matplotlib,plot,3d,Python,Numpy,Matplotlib,Plot,3d,我有一些z=f(x,y)数据要绘制。问题在于(x,y)不是“漂亮”矩形的一部分,而是任意平行四边形,如附图所示(这个特殊的平行四边形也是矩形,但您可以考虑更一般的情况)。因此,我很难弄清楚如何在这种情况下使用plot\u surface,因为这通常将x和y作为二维数组,这里的x和y值是1d。谢谢 辅助点可以作为1D阵列提供给。它们是否遵循特定的结构并不重要 取决于数据结构的其他方法如下: 在规则矩形网格上插值点。这可以通过使用。参见示例 重新调整输入数组的形状,使其位于规则曲面上,然后使用pl

我有一些
z=f(x,y)
数据要绘制。问题在于
(x,y)
不是“漂亮”矩形的一部分,而是任意平行四边形,如附图所示(这个特殊的平行四边形也是矩形,但您可以考虑更一般的情况)。因此,我很难弄清楚如何在这种情况下使用
plot\u surface
,因为这通常将x和y作为二维数组,这里的x和y值是1d。谢谢

辅助点可以作为1D阵列提供给。它们是否遵循特定的结构并不重要

取决于数据结构的其他方法如下:

  • 在规则矩形网格上插值点。这可以通过使用。参见示例
  • 重新调整输入数组的形状,使其位于规则曲面上,然后使用
    plot\u surface()
    。根据提供点的顺序,对于具有“平行四边形”形状的网格,这可能是一个非常简单的解决方案。
    从中可以看出,
    plot_surface()
    也适用于网格形状非常不均匀的情况,只要它是以规则的方式构造的
以下是一些例子:

为完整起见,请在此处找到生成上述图像的代码:

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

f = lambda x,y: np.sin(x+0.4*y)*0.23+1

fig = plt.figure(figsize=(5,6))
plt.subplots_adjust(left=0.1, top=0.95,wspace=0.01)


ax0 = fig.add_subplot(322, projection="3d")

ma = 6*(np.random.rand(100)-0.5)
mb = 6*(np.random.rand(100)-0.5)
phi = np.pi/4
x = 1.7*ma*np.cos(phi) + 1.7*mb*np.sin(phi)
y = -1.2*ma*np.sin(phi) +1.2* mb*np.cos(phi)
z = f(x,y)
ax0.plot_trisurf(x,y,z)

ax1 = fig.add_subplot(321)
ax0.set_title("random plot_trisurf()")
ax1.set_aspect("equal")
ax1.scatter(x,y, marker="+", alpha=0.4)
for i  in range(len(x)):
    ax1.text(x[i],y[i], i  , ha="center", va="center", fontsize=6)


n = 10
a = np.linspace(-3, 3, n)
ma, mb = np.meshgrid(a,a)
phi = np.pi/4
xm = 1.7*ma*np.cos(phi) + 1.7*mb*np.sin(phi)
ym = -1.2*ma*np.sin(phi) +1.2* mb*np.cos(phi)
shuf = np.c_[xm.flatten(), ym.flatten()]
np.random.shuffle(shuf)
x = shuf[:,0]
y = shuf[:,1]
z = f(x,y)

ax2 = fig.add_subplot(324, projection="3d")
ax2.plot_trisurf(x,y,z)

ax3 = fig.add_subplot(323)
ax2.set_title("unstructured plot_trisurf()")
ax3.set_aspect("equal")
ax3.scatter(x,y, marker="+", alpha=0.4)
for i  in range(len(x)):
    ax3.text(x[i],y[i], i  , ha="center", va="center", fontsize=6)


x = xm.flatten()
y = ym.flatten()
z = f(x,y)

X = x.reshape(10,10)
Y = y.reshape(10,10)
Z = z.reshape(10,10)

ax4 = fig.add_subplot(326, projection="3d")
ax4.plot_surface(X,Y,Z)

ax5 = fig.add_subplot(325)
ax4.set_title("regular plot_surf()")
ax5.set_aspect("equal")
ax5.scatter(x,y, marker="+", alpha=0.4)
for i  in range(len(x)):
    ax5.text(x[i],y[i], i  , ha="center", va="center", fontsize=6)


for axes in [ax0, ax2,ax4]:
    axes.set_xlim([-3.5,3.5])
    axes.set_ylim([-3.5,3.5])
    axes.set_zlim([0.9,2.0])
    axes.axis("off")
plt.savefig(__file__+".png")
plt.show()

一元点可以作为一维阵列提供给。它们是否遵循特定的结构并不重要

取决于数据结构的其他方法如下:

  • 在规则矩形网格上插值点。这可以通过使用。参见示例
  • 重新调整输入数组的形状,使其位于规则曲面上,然后使用
    plot\u surface()
    。根据提供点的顺序,对于具有“平行四边形”形状的网格,这可能是一个非常简单的解决方案。
    从中可以看出,
    plot_surface()
    也适用于网格形状非常不均匀的情况,只要它是以规则的方式构造的
以下是一些例子:

为完整起见,请在此处找到生成上述图像的代码:

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

f = lambda x,y: np.sin(x+0.4*y)*0.23+1

fig = plt.figure(figsize=(5,6))
plt.subplots_adjust(left=0.1, top=0.95,wspace=0.01)


ax0 = fig.add_subplot(322, projection="3d")

ma = 6*(np.random.rand(100)-0.5)
mb = 6*(np.random.rand(100)-0.5)
phi = np.pi/4
x = 1.7*ma*np.cos(phi) + 1.7*mb*np.sin(phi)
y = -1.2*ma*np.sin(phi) +1.2* mb*np.cos(phi)
z = f(x,y)
ax0.plot_trisurf(x,y,z)

ax1 = fig.add_subplot(321)
ax0.set_title("random plot_trisurf()")
ax1.set_aspect("equal")
ax1.scatter(x,y, marker="+", alpha=0.4)
for i  in range(len(x)):
    ax1.text(x[i],y[i], i  , ha="center", va="center", fontsize=6)


n = 10
a = np.linspace(-3, 3, n)
ma, mb = np.meshgrid(a,a)
phi = np.pi/4
xm = 1.7*ma*np.cos(phi) + 1.7*mb*np.sin(phi)
ym = -1.2*ma*np.sin(phi) +1.2* mb*np.cos(phi)
shuf = np.c_[xm.flatten(), ym.flatten()]
np.random.shuffle(shuf)
x = shuf[:,0]
y = shuf[:,1]
z = f(x,y)

ax2 = fig.add_subplot(324, projection="3d")
ax2.plot_trisurf(x,y,z)

ax3 = fig.add_subplot(323)
ax2.set_title("unstructured plot_trisurf()")
ax3.set_aspect("equal")
ax3.scatter(x,y, marker="+", alpha=0.4)
for i  in range(len(x)):
    ax3.text(x[i],y[i], i  , ha="center", va="center", fontsize=6)


x = xm.flatten()
y = ym.flatten()
z = f(x,y)

X = x.reshape(10,10)
Y = y.reshape(10,10)
Z = z.reshape(10,10)

ax4 = fig.add_subplot(326, projection="3d")
ax4.plot_surface(X,Y,Z)

ax5 = fig.add_subplot(325)
ax4.set_title("regular plot_surf()")
ax5.set_aspect("equal")
ax5.scatter(x,y, marker="+", alpha=0.4)
for i  in range(len(x)):
    ax5.text(x[i],y[i], i  , ha="center", va="center", fontsize=6)


for axes in [ax0, ax2,ax4]:
    axes.set_xlim([-3.5,3.5])
    axes.set_ylim([-3.5,3.5])
    axes.set_zlim([0.9,2.0])
    axes.axis("off")
plt.savefig(__file__+".png")
plt.show()

如果您的数据是有序的,并且您知道平行图的大小,则重塑可能就足够了:

ax.surface(x.reshape(10, 10), y.reshape(10, 10), z.reshape(10, 10))

如果平行四边形每侧有10个点,并且这些点以之字形排列,则该方法有效如果您的数据有序,并且您知道平行四边形的大小,则重塑可能就足够了:

ax.surface(x.reshape(10, 10), y.reshape(10, 10), z.reshape(10, 10))

如果平行四边形每边有10个点,并且这些点是以之字形排列的,那么这会起作用吗

你事先知道平行线的规格吗?不知道,我得到了数据,我需要绘图——当然我可以根据给定的数据计算规格,我猜…你知道数据的顺序吗?你事先知道平行线的规格吗?不是真的,我得到了数据,我需要绘图--当然我可以根据给定的数据计算规格,我猜…你知道数据的顺序吗?不是说
曲面
不需要矩形网格,它只需要一个四边形网格。因此,如果正确地重新排列,OPs数据可以直接传递到
曲面
,而无需任何数字更改。您可以在具有凹边界的域上绘制trisurf函数吗?这家伙想知道:
surface
不需要矩形网格,它只需要一个四边形网格。因此,如果正确地重新排列,OPs数据可以直接传递到
曲面
,而无需任何数字更改。您可以在具有凹边界的域上绘制trisurf函数吗?这家伙想知道:谢谢埃里克,你的把戏也成功了。因为我以后会考虑用更奇怪的形状来支持网格,所以现在我将使用trisurf。@Faser:您正在生成这些网格吗?另外,你说的是“网格”而不是“网格”,这意味着
surface
应该总是足够的。谢谢Eric,你的技巧也做到了。因为我以后会考虑用更奇怪的形状来支持网格,所以现在我将使用trisurf。@Faser:您正在生成这些网格吗?另外,你说的是“网格”而不是“网格”,这意味着
surface
应该已经足够了