python一维数组画图,通过在python中使用三个一维数组制作轮廓图

As the title indicates I would like to make a contour plot by using three 1D arrays. Let's say that

x = np.array([1,2,3])

and

y = np.array([1,2,3])

and

z = np.array([20,21,45])

To do a contourplot in matplotlib i meshed the x and y coordinate as X,Y = meshgrid(x,y) but then the z array must also be a 2D array. How do I then turn z into a 2d array so it can be used?

解决方案

Your z is wrong. It needs to give the values at every point of the mesh. If z is a function of x and y, calculate z at what I refer to as X_grid below:

import numpy as np

import matplotlib.pyplot as plt

def f(x):

return (x[:,0]**2 + x[:,1]**2)

x = np.array([1,2,3])

y = np.array([1,2,3])

xx, yy = np.meshgrid(x, y)

X_grid = np.c_[ np.ravel(xx), np.ravel(yy) ]

z = f(X_grid)

z = z.reshape(xx.shape)

plt.contour(xx, yy, z)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值