Matplotlib 3D 图 - 输入数据的 2D 格式?

发布于 2025-01-02 21:10:05 字数 949 浏览 3 评论 0原文

我正在使用 matplotlib 绘制两个参数的函数。我复制了 matplotlib 教程中的一个示例,并用我自己的输入数据进行了转换:向量 X 和 Y(在 -3:3 中等距的数字)和 Z=peaks(X,Y),其中峰值是我预先定义的函数。怎么了?

def peaks(x,y):
   xsq=x**2
   ysq=y**2
   xsq_one=(x+1)**2
   ysq_one=(y+1)**2
   m1=3*(1-x)**2
   m2=10*(x/5-x**3-y**5)
   m3=1/3
   return m1*numpy.exp(-xsq-ysq_one)-m2*numpy.exp(-xsq-ysq)-m3*numpy.exp(-xsq_one-ysq)


from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
X=Y=numpy.arange(-3,3,0.01).tolist()
Z=[]
for i in range(len(X)):
Z.append(peaks(X[i],Y[i]))

ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
cset = ax.contour(X, Y, Z, zdir='z', offset=-100)
cset = ax.contour(X, Y, Z, zdir='x', offset=-40)
cset = ax.contour(X, Y, Z, zdir='y', offset=40)

ax.set_xlabel('X')
ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)

plt.show()

感谢您的建议!

I am plotting a function of two parameters with matplotlib. I copied an example in matplotlib tutorial and transformed with my own input data: vectors X and Y (equally spaces numbers in -3:3) and Z=peaks(X,Y) with peaks a function that I defined befohand. What is wrong?

def peaks(x,y):
   xsq=x**2
   ysq=y**2
   xsq_one=(x+1)**2
   ysq_one=(y+1)**2
   m1=3*(1-x)**2
   m2=10*(x/5-x**3-y**5)
   m3=1/3
   return m1*numpy.exp(-xsq-ysq_one)-m2*numpy.exp(-xsq-ysq)-m3*numpy.exp(-xsq_one-ysq)


from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
X=Y=numpy.arange(-3,3,0.01).tolist()
Z=[]
for i in range(len(X)):
Z.append(peaks(X[i],Y[i]))

ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
cset = ax.contour(X, Y, Z, zdir='z', offset=-100)
cset = ax.contour(X, Y, Z, zdir='x', offset=-40)
cset = ax.contour(X, Y, Z, zdir='y', offset=40)

ax.set_xlabel('X')
ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)

plt.show()

Thanks for advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所的.畏惧 2025-01-09 21:10:05

您需要生成网格。 X、Y 和 Z 必须是 2D 数组

import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

def peaks(x,y):
    return x * numpy.sin(y)

fig = plt.figure()
ax = fig.gca(projection='3d')
X = Y= numpy.arange(-3, 3, 0.1).tolist()
X, Y = numpy.meshgrid(X, Y)

Z = []
for i in range(len(X)):
    Z.append(peaks(X[i],Y[i]))

# Z must be an array
Z = numpy.array(Z)

ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
cset = ax.contour(X, Y, Z, zdir='z', offset=-8)
cset = ax.contour(X, Y, Z, zdir='x', offset=-8)
cset = ax.contour(X, Y, Z, zdir='y', offset=8)

ax.set_xlabel('X')
ax.set_xlim(-8, 8)
ax.set_ylabel('Y')
ax.set_ylim(-8, 8)
ax.set_zlabel('Z')
ax.set_zlim(-8, 8)

plt.show()

在此处输入图像描述

You need to generate the meshgrid. X,Y and Z must be 2D arrays

import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

def peaks(x,y):
    return x * numpy.sin(y)

fig = plt.figure()
ax = fig.gca(projection='3d')
X = Y= numpy.arange(-3, 3, 0.1).tolist()
X, Y = numpy.meshgrid(X, Y)

Z = []
for i in range(len(X)):
    Z.append(peaks(X[i],Y[i]))

# Z must be an array
Z = numpy.array(Z)

ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
cset = ax.contour(X, Y, Z, zdir='z', offset=-8)
cset = ax.contour(X, Y, Z, zdir='x', offset=-8)
cset = ax.contour(X, Y, Z, zdir='y', offset=8)

ax.set_xlabel('X')
ax.set_xlim(-8, 8)
ax.set_ylabel('Y')
ax.set_ylim(-8, 8)
ax.set_zlabel('Z')
ax.set_zlim(-8, 8)

plt.show()

enter image description here

梦里兽 2025-01-09 21:10:05

接受的答案不再有效。遗憾的是,审稿人拒绝了我建议的编辑,这本来可以使其成为一个可行的答案。所以这里又是相同的答案,但需要进行一些小的更改才能使其在当前版本的 matplotlib 中工作。

    import numpy
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import axes3d

    def peaks(x,y):
        return x * numpy.sin(y)

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    X = Y= numpy.arange(-3, 3, 0.1).tolist()
    X, Y = numpy.meshgrid(X, Y)

    Z = numpy.zeros(X.shape)
    for i in range(len(X)):
        for j in range(len(Y)):
            Z[i,j] = peaks(X[i,j],Y[i,j])

    ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
    cset = ax.contour(X, Y, Z, zdir='z', offset=-8)
    cset = ax.contour(X, Y, Z, zdir='x', offset=-8)
    cset = ax.contour(X, Y, Z, zdir='y', offset=8)

    ax.set_xlabel('X')
    ax.set_xlim(-8, 8)
    ax.set_ylabel('Y')
    ax.set_ylim(-8, 8)
    ax.set_zlabel('Z')
    ax.set_zlim(-8, 8)

    plt.show()

The accepted answer no longer works. Sadly reviewers rejected my suggested edit which would have made it a working asnwer. So here is the same answer again but with the small change necessary to make it work in the current release of matplotlib.

    import numpy
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import axes3d

    def peaks(x,y):
        return x * numpy.sin(y)

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    X = Y= numpy.arange(-3, 3, 0.1).tolist()
    X, Y = numpy.meshgrid(X, Y)

    Z = numpy.zeros(X.shape)
    for i in range(len(X)):
        for j in range(len(Y)):
            Z[i,j] = peaks(X[i,j],Y[i,j])

    ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
    cset = ax.contour(X, Y, Z, zdir='z', offset=-8)
    cset = ax.contour(X, Y, Z, zdir='x', offset=-8)
    cset = ax.contour(X, Y, Z, zdir='y', offset=8)

    ax.set_xlabel('X')
    ax.set_xlim(-8, 8)
    ax.set_ylabel('Y')
    ax.set_ylim(-8, 8)
    ax.set_zlabel('Z')
    ax.set_zlim(-8, 8)

    plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文