从(x,y,z)数据集中绘制matplotlib中的等值线/轮廓 [英] Plotting Isolines/contours in matplotlib from (x, y, z) data set

查看:291
本文介绍了从(x,y,z)数据集中绘制matplotlib中的等值线/轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,我正在尝试做一些可能很明显的事情,但是对于我一生,我无法弄清楚.我有一系列的x,y,z数据(在我的情况下,对应于距离,深度和pH).我想使用matplotlib在xy(距离,深度)网格上绘制z数据(pH)的等值线.有什么办法吗?谢谢.

解决方案

解决方案将取决于数据的组织方式.

常规网格上的数据

如果xy数据已经定义了网格,则可以轻松地将它们重塑为四边形网格.例如

#x  y  z
 4  1  3
 6  1  8
 8  1 -9
 4  2 10
 6  2 -1
 8  2 -8
 4  3  8
 6  3 -9
 8  3  0
 4  4 -1
 6  4 -8
 8  4  8 

可以绘制为 contour 使用

import matplotlib.pyplot as plt
import numpy as np
x,y,z = np.loadtxt("data.txt", unpack=True)
plt.contour(x.reshape(4,3), y.reshape(4,3), z.reshape(4,3))

任意数据

(a)如果数据不在四边形网格上,则可以将数据插值到网格上. matplotlib本身使用matplotlib.mlab.griddata提供了一种这样做的方法.

import matplotlib.mlab
xi = np.linspace(4, 8, 10)
yi = np.linspace(1, 4, 10)
zi = matplotlib.mlab.griddata(x, y, z, xi, yi, interp='linear')
plt.contour(xi, yi, zi)

(b)最后,人们可以完全绘制轮廓,而无需使用四边形网格.可以使用 tricontour .

plt.tricontour(x,y,z)

matplotlib页面上找到了比较后两种方法的示例. /p>

Hi I'm new to programming and I'm trying to do something that's probably really obvious but for the life of me I can't figure it out. I have a series of x, y, z data (in my case, corresponding to distance, depth, and pH). I would like to plot isolines of the z data (pH) on an xy (distance, depth) grid using matplotlib. Is there any way to do this? Thanks.

解决方案

The solution will depend on how the data is organized.

Data on regular grid

If the x and y data already define a grid, they can be easily reshaped to a quadrilateral grid. E.g.

#x  y  z
 4  1  3
 6  1  8
 8  1 -9
 4  2 10
 6  2 -1
 8  2 -8
 4  3  8
 6  3 -9
 8  3  0
 4  4 -1
 6  4 -8
 8  4  8 

can plotted as a contour using

import matplotlib.pyplot as plt
import numpy as np
x,y,z = np.loadtxt("data.txt", unpack=True)
plt.contour(x.reshape(4,3), y.reshape(4,3), z.reshape(4,3))

Arbitrary data

(a) In case the data is not living on a quadrilateral grid, one can interpolate the data on a grid. One method to do so is provided by matplotlib itself, using matplotlib.mlab.griddata.

import matplotlib.mlab
xi = np.linspace(4, 8, 10)
yi = np.linspace(1, 4, 10)
zi = matplotlib.mlab.griddata(x, y, z, xi, yi, interp='linear')
plt.contour(xi, yi, zi)

(b) Finally, one can plot a contour completely without the use of a quadrilateral grid. This can be done using tricontour.

plt.tricontour(x,y,z)

An example comparing the latter two methods is found on the matplotlib page.

这篇关于从(x,y,z)数据集中绘制matplotlib中的等值线/轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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