繁体   English   中英

Python绘制在for循环中生成的函数的轮廓

[英]Python plotting contour of function generated in for loop

我想为函数z (其值在zlist )绘制轮廓图,其中轴显示参数xy 在两个for循环内生成z ,其中我指定了不同的xy 我收到错误TypeError: Input z must be a 2D array运行此命令时, TypeError: Input z must be a 2D array 如何处理zlist ,使其可绘制?

import numpy as np
import matplotlib.pyplot as plt

def myfunction(x,y):
    c = x + 2*y
    z = c*x + 0.5*y
    return c,z

xlist = np.linspace(0,1,10)
ylist = np.linspace(0,10,20)

X,Y = np.meshgrid(xlist,ylist)

zlist = []

for x in xlist:

    for y in ylist:

        z = myfunction(x,y)[1]
        zlist.append(z)

plt.figure()        
plt.contour(X,Y,zlist)

plt.show()

遵循chthonicdaemon的建议,

X,Y = np.meshgrid(xlist,ylist)

_, Z = myfunction(X,Y)

plt.figure()        
plt.contour(X,Y,Z)

plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM