通过在python中使用三个一维数组制作轮廓图 [英] Make a contour plot by using three 1D arrays in python

查看:191
本文介绍了通过在python中使用三个一维数组制作轮廓图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我想使用三个1D数组绘制轮廓图。假设

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])

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

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

要在matplotlib中绘制轮廓图,我将 x y 坐标划分为 X,Y = meshgrid(x,y),但是 z 数组也必须是2D数组。然后如何将 z 转换为二维数组以便可以使用它?

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?

推荐答案

您的 z 是错误的。它需要给出网格的每个点的值。如果 z x y 的函数,请计算 z 在下面称为 X_grid

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)

这篇关于通过在python中使用三个一维数组制作轮廓图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆