简体   繁体   English

从合并的xyz数据绘制2D等高线图

[英]Plotting a 2D contour plot from binned xyz data

EDIT: I responded in the comments but I've tried the method in the marked post - my z data is not calculated form my x and y so I can't use a function like that. 编辑:我在评论中做出了回应,但是我尝试了带标记的帖子中的方法-我的z数据不是从我的x和y计算出来的,所以我不能使用这样的函数。

I have xyz data that looks like the below: 我的xyz数据如下所示:

NEW:the xyz data in the file i produce - I extract these as x,y,z 新:我生成的文件中的xyz数据-我将它们提取为x,y,z

And am desperately trying to get a plot that has x against y with z as the colour. 并拼命试图得到一个图,其中x与y相对,y与z为颜色。

y is binned data that goes from (for instance) 2.5 to 0.5 in uneven bins. y是装箱后的数据,例如,在不均匀的装箱中从2.5到0.5。 So the y values are all the same for one set of x and z data. 因此,一组x和z数据的y值都相同。 The x data is temperature and the z is density info. x数据为温度,z为密度信息。

So I'm expecting a plot that looks like a bunch of stacked rectangles where there is a gradient of colour for one bin of y values which spans lots of x values. 因此,我期望图形看起来像一堆堆叠的矩形,其中一个y值箱的颜色梯度是渐变的,其中x值跨很多。

However all the codes I've tried don't like my z values and the best I can do is: 但是,我尝试过的所有代码都不喜欢我的z值,而我能做的最好的事情是:

The axes look right but the colour bar goes from the bottom to the top of the y axis instead of plotting one z value for each x value at the correct y value 轴看起来正确,但是色带从y轴的底部到顶部,而不是在正确的y值下为每个x值绘制一个z值

I got this to work with this code: 我可以将其与以下代码一起使用:

import matplotlib.cm as cm
from matplotlib.colors import LogNorm
import numpy as np
import scipy.interpolate
data=pandas.read_csv('Data.csv',delimiter=',', header=0,index_col=False)
x=data.tempbin
y=data.sizefracbin
z=data.den
x=x.values
y=y.values
z=z.values
X,Y=np.meshgrid(x,y)
Z=[]
for i in range(len(x)):
    Z.append(z)
Z=np.array(Z)
plt.pcolormesh(X,Y,Z)
plt.colorbar()
plt.show()

I've tried everything I could find online such as in the post here: matplotlib 2D plot from x,y,z values 我已经尝试了所有可以在网上找到的内容,例如此处的帖子: x,y,z值的matplotlib 2D图

But either there is a problem reshaping my z values or it just gives me empty plots with various errors all to do (I think) with my z values. 但是重塑我的z值有一个问题,或者它只是给我空的图,并带有与我的z值有关的所有错误(我认为)。

Am I missing something? 我想念什么吗? Thank you for your help! 谢谢您的帮助!

Edit in reponse to : ImportanceOfBeingErnest 根据以下内容进行编辑:ImportanceOfBeingErnest

I tried this : 我尝试了这个:

import matplotlib.cm as cm
from matplotlib.colors import LogNorm
import numpy as np
import scipy.interpolate
data=pandas.read_csv('Data.csv',delimiter=',', header=0,index_col=False)
data.sort_values('sizefrac')
x=data.tempbin
y=data.sizefrac
z=data.INP
x=x.values
y=y.values
z=z.values
X=x[1:].reshape(N,N)
Y=y[1:].reshape(N,N)
Z=z[1:].reshape(N,N)
plt.pcolormesh(X,Y,Z)
plt.colorbar()
plt.show()

and got a very empty plot. 并得到了一个非常空的情节。 Just showed me the axes and colourbar as in my attached image but pure white inside the axes! 就像给我展示的图像一样,向我展示了轴和彩条,但是轴内部是纯白色! No error or anything... And the reshaping I need to remove a data point from each because otherwise the reshaping won't work 没有错误或其他任何内容...重塑我需要从每个对象中删除一个数据点,因为否则重塑将无法进行

Adapting the linked question to you problem, you should get: 使链接的问题适应您的问题,您将获得:

import numpy as np
import matplotlib.pyplot as plt

x = list(range(10))*10
y = np.repeat(list(range(10)), 10)

# build random z data
z = np.multiply(x, y)

N = int(len(z)**.5)
Z = z.reshape(N, N)
plt.imshow(Z[::-1], extent=(np.amin(x), np.amax(x), np.amin(y), np.amax(y)), aspect = 'auto')

plt.show()

Silmathoron在对上面的答案的评论中找到了答案-上面的答案无济于事,但在评论中,他注意到X,Y数据没有以w方式网格化,这会在绘图上创建矩形,还提到Z需要比X和Y小一个-从这个我可以修复我的代码-谢谢大家

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

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