matplotlib 3d 线框图

发布于 2024-12-13 04:14:41 字数 1454 浏览 3 评论 0原文

是的,所以我有一个 x 值、y 值和 z 值的列表(我想我将其转换为数组?),我想制作一个曲面图,但它不起作用。

这就是我想要做的,你可以忽略大部分代码,因为它们非常无关紧要 - 只需看看最后的 xdisydisdist 以及我试图绘制 atm 的位置,我收到 ValueError: need more than 1 value to unpack :(。非常感谢您的帮助。

from math import *
from numpy import *
import pylab
def sweep (v,p,q,r,s):
    a=.98

    for i in range (1, len(v)-1):
        for j in range (1, len(v)-1):
            c =0.0

            if i==p and j==q: c =1.0
            if i==r and j==s: c= -1.0
            v[i,j]=(v[i -1,j]+v[i+1,j]+v[i,j -1]+v[i,j+1]+c-a*v[i,j])/(4-a)

def main():
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    from matplotlib.ticker import LinearLocator, FormatStrFormatter
    import matplotlib.pyplot as plt
    ydis=[]
    xdis=[]
    resis=[]
    for j in range(2,18):
        for i in range(2,18):
            v= zeros ((20,20),float )
            p=q=9
            r=i
            s=j
            dv =1.0e10
            lastdv =0
            count =0
            while (fabs(dv - lastdv)>1.0e-7*fabs(dv)):
                lastdv =dv
                sweep(v,p,q,r,s)
                dv=v[p,q]-v[r,s]
            resis.append(dv)
            xdis.append(r-p)
            ydis.append(s-q)

    X=array(xdis)
    Y=array(ydis)
    Z=array(resis)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_wireframe(X,Y,Z)
    plt.show()
main()

Right, so I've got a list of x values, y values and z values (which I think I converted into arrays?) which I want to make a surface plot, but it's not working.

Here's what I'm trying to do, you can ignore most of the code as it is pretty irrelevant - just look at the end where I have xdis, ydis and dist and where I'm trying to plot atm I'm getting ValueError: need more than 1 value to unpack :(. Help much appreciated.

from math import *
from numpy import *
import pylab
def sweep (v,p,q,r,s):
    a=.98

    for i in range (1, len(v)-1):
        for j in range (1, len(v)-1):
            c =0.0

            if i==p and j==q: c =1.0
            if i==r and j==s: c= -1.0
            v[i,j]=(v[i -1,j]+v[i+1,j]+v[i,j -1]+v[i,j+1]+c-a*v[i,j])/(4-a)

def main():
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    from matplotlib.ticker import LinearLocator, FormatStrFormatter
    import matplotlib.pyplot as plt
    ydis=[]
    xdis=[]
    resis=[]
    for j in range(2,18):
        for i in range(2,18):
            v= zeros ((20,20),float )
            p=q=9
            r=i
            s=j
            dv =1.0e10
            lastdv =0
            count =0
            while (fabs(dv - lastdv)>1.0e-7*fabs(dv)):
                lastdv =dv
                sweep(v,p,q,r,s)
                dv=v[p,q]-v[r,s]
            resis.append(dv)
            xdis.append(r-p)
            ydis.append(s-q)

    X=array(xdis)
    Y=array(ydis)
    Z=array(resis)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_wireframe(X,Y,Z)
    plt.show()
main()

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

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

发布评论

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

评论(2

陈甜 2024-12-20 04:14:41

plot_wireframe 需要三个二维数组(X, Y,Z) 作为输入。所以,

在:之后

X=np.array(xdis)
Y=np.array(ydis)
Z=np.array(resis)

添加:

X=X.reshape((-1,16))
Y=Y.reshape((-1,16))
Z=Z.reshape((-1,16))        

plot_wireframe expects three 2D-arrays (X,Y,Z) as input. So,

after:

X=np.array(xdis)
Y=np.array(ydis)
Z=np.array(resis)

add:

X=X.reshape((-1,16))
Y=Y.reshape((-1,16))
Z=Z.reshape((-1,16))        
失去的东西太少 2024-12-20 04:14:41

“sweep”函数似乎没有修改“v”,所以你得到一个空列表。

It doesn't seem like the "sweep" function is modifying 'v' so you're getting an empty list.

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