简体   繁体   English

创建2D轮廓图

[英]Create 2D Contour Plot

Ok, I am at a total loss here. 好的,我在这里全然不知所措。 I think I am trying to construct a 2D Contour Plot. 我想我正在尝试构建2D轮廓图。 I am not sure if that is the name of the plot I am actually trying to construct though. 我不确定这是否是我实际上试图构建的地块的名称。 I have an attached picture of what I am trying to construct 我有一张我要构造的图片的附件

I have found several useful questions and guides on building such a plot ( Python : 2d contour plot from 3 lists , https://matplotlib.org/api/_as_gen/matplotlib.pyplot.contour.html ) the problem that I am running into is that everything requires your x and y axis to have the same number of data points. 我发现了一些有用的问题和有关构建此类图的指南( Python:3个列表中的2d等高线图https ://matplotlib.org/api/_as_gen/matplotlib.pyplot.contour.html )是我遇到的问题就是一切都需要您的x和y轴具有相同数量的数据点。 However, my x axis list has 26 values, while my y axis list has 1024 values. 但是,我的x轴列表具有26个值,而我的y轴列表具有1024个值。 The list that denotes what color each corresponding data point needs to be is 26*1024=26624 data points long. 表示每个相应数据点需要什么颜色的列表长26 * 1024 = 26624个数据点。

I am going to try to explain how the data that describes my plot works, but just in case I don't do a good job, I will also attach an example picture of my data. 我将尝试解释描述情节的数据如何工作,但是以防万一,如果我做得不好,我还将附上示例数据。 Basically, it lists one x value 1024 times. 基本上,它列出一个x值1024次。 Each time it lists the x value, it lists a corresponding y and z value. 每次列出x值时,都会列出相应的y和z值。 Then it moves on to the next x value. 然后它将移至下一个x值。

For instance: 例如:

x = np.array([0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4])
y = np.array([0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1)]
color_map = random.sample(xrange(10), 25)

I have no problem extracting the data, just knowing what to do with the data after I extract it 我提取数据没有问题,只是在提取数据后知道如何处理数据

FLR = np.genfromtxt("C:\\Users\\Downloads\\Python\\aupnipam_scan41_3DFLR(1).txt")
x = FLR[:,][:,0]
y = FLR[:,][:,1]
z = FLR[:,][:,2]

PLease help! 请帮忙!

我认为是2D等高线图

我的数据示例

I believe what you're looking for is this function in matplotlib 我相信您正在寻找的是matplotlib中的此功能

pcolormesh(x, y, z)

The best way to solve your problem is to follow the script included on this page . 解决问题的最佳方法是遵循此页面上包含的脚本。

Can you share a bit of the data in a format that can be copied? 您可以共享一种可以复制的格式的数据吗? I could share a code snippet that works with your data for additional clarification then. 我可以共享一个与您的数据一起使用的代码段,以便进一步澄清。

There is no requirement for having the same number of data along the two axes. 不需要沿两个轴具有相同数量的数据。 With

x = np.array([0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4])
y = np.array([0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1, 
              0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1)]

you have nx = 5 different x values and ny = 5 different y values, but you can equally have different numbers of values. 您有nx = 5不同的x值和ny = 5不同的y值,但同样可以有不同数量的值。 The only requirement is that you either have 唯一的要求是您要么

  • as many z values as the product of those two numbers, in this case 25 与这两个数字的乘积一样多的z值,在这种情况下为25

     z = np.random.rand(nx*ny) 
  • as many z values as the product of each number diminished by 1, in that case 16. 在这种情况下,与每个数字的乘积除以1的z值一样多。

     z = np.random.rand((nx-1)*(ny-1)) 

depending on whether you want to define the value at the edges of the grid or the center. 取决于要在网格边缘还是在中心边缘定义值。

In this case it seems the first of those cases applies. 在这种情况下,似乎第一种情况适用。

So you would just reshape your data to a 2D array (and in this case transpose it, because the x values go along the second array dimension). 因此,您只需将数据重塑为2D数组(在这种情况下,将其转置,因为x值沿着第二个数组维)。

nx = 5
ny = 5

Z = z.reshape(ny, nx).T

Finally you may plot it using imshow . 最后,您可以使用imshow对其进行imshow The tricky part is then to set the correct extent of the image, because the the image edge is not at the center pixel position, but half a pixel width shifted to the right or left. 然后,棘手的部分是设置正确的图像范围,因为图像边缘不在中心像素位置,而是向右或向左移动了一半像素宽度。

extent = [x.min()-np.diff(x)[0]/2.,x.max()+np.diff(x)[0]/2.,
          y.min()-np.diff(y)[0]/2.,y.max()+np.diff(y)[0]/2.,]
plt.imshow(Z, extent=extent, aspect="auto")

plt.show()

在此处输入图片说明

Another way to plot contours of irregular data: 绘制不规则数据轮廓的另一种方法:

import numpy as np
from scipy.interpolate import griddata

x = np.array([0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4])
y = np.array([0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1, 0.2, 0.4, 0.6, 0.8, 1])
z = np.random.rand(25)
xi = np.linspace(min(x),max(x),100)
yi = np.linspace(min(y),max(y),100)
zi = griddata((x,y),z,(xi[None:,],yi[:,None]),method='linear',fill_value=0.0)
plt.contourf(xi,yi,zi)
plt.colorbar()
plt.show()

在此处输入图片说明

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

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