简体   繁体   中英

How to plot 2D matrix whose data is not equally spaced along x-axis using Python?

I am trying to plot a 2D matrix using the matplotlib library (the only one I know so far). However, the function matplotlib.pyplot.imshow(matrix) presumes that the data is equally spaced along each axis while the data I want to plot is not. My data looks like: the first column of matrix is data on line x=0, the second column's data is on line x=1.27 instead of x=1, and data of third column is on line x=1.42 but not x=2 and so on.

BTW, data is equally spaced along y-axis.

So I'm wondering if there is anyway I can plot this in the way I hope? Thank you for your time and kind help in advance!

------ newbee in Programming

update: First of all, thank you all for your suggestions! I have tried the methods and here are the outputs: 在此处输入图片说明

This is the graph plotted by contourf which is not as I expected. pcolormesh doesn't not give an ideal output either. What I am trying to plot is data of seismic traces which represents the underground structure. I hope to plot something like this: 在此处输入图片说明

If there's any advice on how I can do that, please give me a hint! Thank you very much!

I would suggest you to use the function pcolormesh .

You just need to enter the the intervals defined in the 1D arrays x, y and the 2D data, accordingly. Below you find a simple example.

from pylab import *
x = logspace(log10(1),log10(100),11)
y = linspace(1,100,21)

data = rand(20,10)
pcolormesh(x,y,data)
show()

Notice that x and y have an extra point. In this case, data fills up the intervals defined by x and y. Otherwise, you can use the function contourf .

Cheers

You want filled contours ...

The examples should help you get started. Let us know if you have specific problems.

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