简体   繁体   中英

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.

I have xyz data that looks like the below:

NEW:the xyz data in the file i produce - I extract these as x,y,z

And am desperately trying to get a plot that has x against y with z as the colour.

y is binned data that goes from (for instance) 2.5 to 0.5 in uneven bins. So the y values are all the same for one set of x and z data. The x data is temperature and the z is density info.

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.

However all the codes I've tried don't like my z values and the best I can do is:

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

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

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.

Am I missing something? Thank you for your help!

Edit in reponse to : 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小一个-从这个我可以修复我的代码-谢谢大家

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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