使用1D X,Y和Z变量在python中进行2D等高线图 [英] 2D contour plot in python using 1D X, Y and Z variables

查看:249
本文介绍了使用1D X,Y和Z变量在python中进行2D等高线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我开始承认自己对Python非常陌生。我想在Python中创建数据的轮廓图,以使过程自动化,否则可以使用Surfer轻松进行。我有数千个这样的数据文件,而手动创建可能非常繁琐。
我正在使用的数据如下所示,它是一个具有0、1和2标头以及1,2..279作为索引的数据框:

Let me begin this query by admitting that I am very new to Python. I want to create contour plot of the data in Python so as to automate the process, which otherwise can be easily carried out using Surfer. I have 1000s of such data files, and creating manually could be very tedious. The data I'm using looks like follows, which is a dataframe with 0, 1 and 2 headers and 1,2,..279 as index:

     0   1         2
0     3  -1 -0.010700
1     4  -1  0.040100
2     5  -1  0.061000
3     6  -1  0.052000
4     7  -1  0.013100
..   ..  ..       ...
275  30  -9 -1.530100
276  31  -9 -1.362300
277  32  -9 -1.190200
278  33  -9 -1.083600
279  30 -10 -1.864600

[280 rows x 3 columns]

这里

x=data[0]
y=data[1]
z=data[2]

作为轮廓函数pf matplotlib要求z为2D数组;这就是混乱的开始。以下是几个关于堆栈溢出查询的解决方案,我做了以下工作:

As contour function pf matplotlib requires z to be a 2D array; this is where the confusion begins. Following several solutions of stackoverflow queries, I did the following:

import numpy as np
x=np.array(x)
y=np.array(y)
z=np.array(z)
X, Y = np.meshgrid(x, y)
import scipy.interpolate
rbf = scipy.interpolate.Rbf(x, y, z, function='cubic')
Z=rbf(X,Y)

lmin=data[2].min()
lmax=data[2].max()
progn=(lmax-lmin)/20
limit=np.arange(lmin,lmax,progn)

fig, ax = plt.subplots(figsize=(6,2)) #x ranges between 3 to 57, y -1 to -10
ax.contour(X,Y,Z,limit) 
ax.set_title('Contour Plot')
plt.show()

使用上面的代码可以得出该图。

With the above code this plot is derived.

但是,这是不希望的,如果一次可以通过表面噪声线看到,那么下面就会有序的轮廓线,从此处冲浪者生成的轮廓图中可以看出,这实际上是需要的。

However, it is not desired and if once can see through the surfacial noise lines then there are ordered contour lines underneath, which actually is desired as seen from the contour plot generated by surfer here.

我想重申,在生成冲浪者图时使用了相同的数据。

I'd like to reiterate that the same data was used in generating the surfer plot.

在创建所需图形方面的任何帮助都将受到高度赞赏。

Any help in creating the desired plot shall be highly appreciated.

推荐答案

感谢@JohanC的回答。我想在我的查询中提出他的建议。

Thanks to @JohanC for the answer. I'd like to put his suggestion to perspective with my query.

ax.contour 替换为 ax .tricontour 解决了我的情况。然后 ax.tricontourf 完成轮廓填充。因此,我代码的最后一段将是:

ax.contour replaced by ax.tricontour solves my situation. And ax.tricontourf gets the contour fill done. Therefore, the last segment of my code would be:

fig, ax = plt.subplots(figsize=(6,2)) #x ranges between 3 to 57, y -1 to -10
ax.tricontour(X,Y,Z,limit) 
ax.tricontourf(X,Y,Z,limit) 
ax.set_title('Contour Plot')
plt.show()

这篇关于使用1D X,Y和Z变量在python中进行2D等高线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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