images from data

Hi all,

I am trying to use imshow to plot an image from some data. Basically,
I have a list of x, y, and z values, and I want to plot z as a
function of x and y.

Unfortunately, most of the examples do this as far as I can
tell. Instead, they (at most) take a list of x and y then make a
*function* z out of that, and plot z, rather than having actual data
from a file for all three.

The only one that does seem to do something like this seems to be
dependent on a binary file, which unfortunately is not the format I
have the data in. Sure, I could convert the very large files I already
have into binary, but in the long run, I think it would be better for
me to be able to simply use the ASCII data that I am provided with
since that can't be changed.

Is there an easy way to do this, or will I just have to convert the
files?

Thanks,

Mike

If you data is regularly spaced, you can just use imshow or pcolor. If
it's not regularaly spaced, see
http://www.scipy.org/Wiki/Cookbook/Matplotlib/Gridding_irregularly_spaced_data

Michael V. De Palatis wrote:

···

Hi all,

I am trying to use imshow to plot an image from some data. Basically,
I have a list of x, y, and z values, and I want to plot z as a
function of x and y.

Unfortunately, most of the examples do this as far as I can
tell. Instead, they (at most) take a list of x and y then make a
*function* z out of that, and plot z, rather than having actual data
from a file for all three.

The only one that does seem to do something like this seems to be
dependent on a binary file, which unfortunately is not the format I
have the data in. Sure, I could convert the very large files I already
have into binary, but in the long run, I think it would be better for
me to be able to simply use the ASCII data that I am provided with
since that can't be changed.

Is there an easy way to do this, or will I just have to convert the
files?

Thanks,

Mike

-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

If you data is regularly spaced, you can just use imshow or pcolor. If
it's not regularaly spaced, see
http://www.scipy.org/Wiki/Cookbook/Matplotlib/Gridding_irregularly_spaced_data

Perhaps I'm missing something or did not explain my problem
properly. I am well aware that what I want to use is imshow (or,
apparently, pcolor). However, my problem is that I have all THREE
values in data form, not just x and y (that is, I do not have a
function to get z(x, y), but rather I have data points for z).

I do not see a straightforward way to use imshow or pcolor to be able
to plot this data as they want a matrix that I have no idea how to
generate.

Thanks,

Mike

···

On Sat, Apr 29, 2006 at 09:59:14AM -0700, Andrew Straw wrote:

Michael V. De Palatis wrote:

>Hi all,
>
>I am trying to use imshow to plot an image from some data. Basically,
>I have a list of x, y, and z values, and I want to plot z as a
>function of x and y.
>
>Unfortunately, most of the examples do this as far as I can
>tell. Instead, they (at most) take a list of x and y then make a
>*function* z out of that, and plot z, rather than having actual data
>from a file for all three.
>
>The only one that does seem to do something like this seems to be
>dependent on a binary file, which unfortunately is not the format I
>have the data in. Sure, I could convert the very large files I already
>have into binary, but in the long run, I think it would be better for
>me to be able to simply use the ASCII data that I am provided with
>since that can't be changed.
>
>Is there an easy way to do this, or will I just have to convert the
>files?
>
>Thanks,
>
>Mike
>
>
>-------------------------------------------------------
>Using Tomcat but need to do more? Need to support web services, security?
>Get stuff done quickly with pre-integrated technology to make your job easier
>Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
>http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
>_______________________________________________
>Matplotlib-users mailing list
>Matplotlib-users@lists.sourceforge.net
>matplotlib-users List Signup and Options
>
>

Michael V. De Palatis wrote:

If you data is regularly spaced, you can just use imshow or pcolor. If
it's not regularaly spaced, see
http://www.scipy.org/Wiki/Cookbook/Matplotlib/Gridding_irregularly_spaced_data

Perhaps I'm missing something or did not explain my problem
properly. I am well aware that what I want to use is imshow (or,
apparently, pcolor). However, my problem is that I have all THREE
values in data form, not just x and y (that is, I do not have a
function to get z(x, y), but rather I have data points for z).

I do not see a straightforward way to use imshow or pcolor to be able
to plot this data as they want a matrix that I have no idea how to
generate.

Okay, so your x,y data is regularly spaced. What you have to do is create an
array of the appropriate size to put all of your z values in. Then go through
your list of points and populate the z array in the appropriate places. You will
have to convert your x,y coordinates into i,j indices. If you know the spacing,
this should not be difficult.

Alternatively, you can sort the (x,y,z) tuples, turn it into an array, extract
the z column, reshape the z column into the appropriate matrix, and transpose it
(presuming you want y to be vertical):

In [18]: xyz = [(1, 0, 10), (0, 1, 20), (0, 0, 30), (1, 1, 40)]

In [19]: xyz.sort()

In [20]: xyz
Out[20]: [(0, 0, 30), (0, 1, 20), (1, 0, 10), (1, 1, 40)]

In [21]: xyza = array(xyz)

In [22]: z = xyza[:,-1]

In [23]: z
Out[23]: array([30, 20, 10, 40])

In [24]: z = transpose(reshape(z, (2,2)))

In [25]: z
Out[25]:
array([[30, 10],
       [20, 40]])

···

On Sat, Apr 29, 2006 at 09:59:14AM -0700, Andrew Straw wrote:

--
Robert Kern
robert.kern@...287...

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
  -- Umberto Eco

Michael,

How easy it is to do this depends on your ascii file format, and how the data are organized in it. Are the points in order? e.g.

x0, y0, z00
x0, y1, z01
x0, y2, z02
x1, y0, z10
x1, y1, z11
x1, y2, z12

Are the values space-delimited, or comma-delimited, or in some other format?

Is the grid uniform in the sense that all the x-intervals are the same and all the y-intervals are the same?

Eric

Michael V. De Palatis wrote:

···

Hi all,

I am trying to use imshow to plot an image from some data. Basically,
I have a list of x, y, and z values, and I want to plot z as a
function of x and y.

Unfortunately, most of the examples do this as far as I can
tell. Instead, they (at most) take a list of x and y then make a
*function* z out of that, and plot z, rather than having actual data
from a file for all three.

The only one that does seem to do something like this seems to be
dependent on a binary file, which unfortunately is not the format I
have the data in. Sure, I could convert the very large files I already
have into binary, but in the long run, I think it would be better for
me to be able to simply use the ASCII data that I am provided with
since that can't be changed.

Is there an easy way to do this, or will I just have to convert the
files?

Thanks,

Mike

-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

How easy it is to do this depends on your ascii file format, and how the
data are organized in it. Are the points in order? e.g.

x0, y0, z00
x0, y1, z01
x0, y2, z02
x1, y0, z10
x1, y1, z11
x1, y2, z12

This is precisely the ordering.

Are the values space-delimited, or comma-delimited, or in some other
format?

The values are space-delimited. The way I am reading them in is with
readline, then splitting the resulting string and appending to a list
of x, y, and z values.

Is the grid uniform in the sense that all the x-intervals are the same
and all the y-intervals are the same?

Unfortunately, the grid is not uniform. From the responses I have been
getting thus far, it's looking like this is going to be far more
complicated than I had hoped...

Would it in principle be difficult to write a function similar to
imshow that can take 3 arrays and plot z (in color, say) as a function
of x and y? To me, it would seem that it would essentially be the same
as the plot function, except that it takes an extra array...

Mike

···

On Sat, Apr 29, 2006 at 08:41:48AM -1000, Eric Firing wrote:

Michael V. De Palatis wrote:

Unfortunately, the grid is not uniform. From the responses I have been
getting thus far, it's looking like this is going to be far more
complicated than I had hoped...

Look in examples/quadmesh_demo.py .

Or pretend like it's not a grid at all and just use the information Andrew gave
you the first time:

http://www.scipy.org/Wiki/Cookbook/Matplotlib/Gridding_irregularly_spaced_data

···

--
Robert Kern
robert.kern@...287...

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
  -- Umberto Eco

Michael V. De Palatis wrote:

How easy it is to do this depends on your ascii file format, and how the data are organized in it. Are the points in order? e.g.

x0, y0, z00
x0, y1, z01
x0, y2, z02
x1, y0, z10
x1, y1, z11
x1, y2, z12

This is precisely the ordering.

Are the values space-delimited, or comma-delimited, or in some other
format?

The values are space-delimited. The way I am reading them in is with
readline, then splitting the resulting string and appending to a list
of x, y, and z values.

Is the grid uniform in the sense that all the x-intervals are the same and all the y-intervals are the same?

Unfortunately, the grid is not uniform. From the responses I have been
getting thus far, it's looking like this is going to be far more
complicated than I had hoped...

It might not be complicated, but forget about using imshow--it is for image data, which are inherently evenly spaced. Pcolor and pcolormesh don't have this limitation. Note that Zij will give the color of the box with diagonal corners (Xij, Yij) and (Xi+1,j+1, Yi+1,j+1), so if Z has the same dimensions as X and Y, the last row and column will not be shown. If your input data are intended to give the (x,y) positions of the centers of regions rather than the corners, then you will need to do some sort of regridding of Z, or shifting of your X and Y points, in order to get everything exactly right with pcolor and pcolormesh.

The attached script and sample data file show how you can put the data from your ascii file into X, Y, and Z arrays, and plot them using pcolor or pcolormesh. This is for the simple case in which treating x and y as corners is good enough. Note that the grid does not have to form rectangular cells; they can be quadrilaterals. I illustrated this in the sample file by shifting one point.

Eric

xyz.asc (240 Bytes)

pc_xyz.py (455 Bytes)

···

On Sat, Apr 29, 2006 at 08:41:48AM -1000, Eric Firing wrote:

Unfortunately, I couldn't even get this example to work. It was some
sort of error related to pcolor not being a method of subplot, or
something like that (sorry, I don't have the exact error message with
me right now).

Essentially, it seems to me that the above example *would* in
principle work, though it's still going to take me a while to get
things working. I have a thesis to turn in on Friday, or else I would
keep playing with it until I got it to work. As it is, I'm going to
have to do without it (luckily, I can use the program that's exporting
the data to make my plots, I just think they're ugly).

Perhaps in the next few weeks I'll play with it some more and write up
the results if I get it working well.

Thanks for all the responses,

Mike

···

On Sun, Apr 30, 2006 at 08:48:06AM -1000, Eric Firing wrote:

It might not be complicated, but forget about using imshow--it is for
image data, which are inherently evenly spaced. Pcolor and pcolormesh
don't have this limitation. Note that Zij will give the color of the
box with diagonal corners (Xij, Yij) and (Xi+1,j+1, Yi+1,j+1), so if Z
has the same dimensions as X and Y, the last row and column will not be
shown. If your input data are intended to give the (x,y) positions of
the centers of regions rather than the corners, then you will need to do
some sort of regridding of Z, or shifting of your X and Y points, in
order to get everything exactly right with pcolor and pcolormesh.

The attached script and sample data file show how you can put the data
from your ascii file into X, Y, and Z arrays, and plot them using pcolor
or pcolormesh. This is for the simple case in which treating x and y as
corners is good enough. Note that the grid does not have to form
rectangular cells; they can be quadrilaterals. I illustrated this in
the sample file by shifting one point.

Eric

1.0 1.0 1.1
1.0 1.5 1.2
1.0 2.5 1.1
1.0 4.0 1.0
1.0 4.5 1.0
2.0 1.0 1.2
2.0 1.5 1.3
2.5 2.9 1.4
2.0 4.0 1.1
2.0 4.5 1.1
3.0 1.0 1.0
3.0 1.5 1.3
3.0 2.5 1.5
3.0 4.0 1.2
3.0 4.5 1.2
4.0 1.0 1.0
4.0 1.5 1.3
4.0 2.5 1.5
4.0 4.0 1.2
4.0 4.5 1.2

#!/usr/bin/env python

from pylab import subplot, show
from pylab import nx as N

fname = 'xyz.asc'
nx, ny = (4,5)

lines = open(fname).readlines()
a_list = [[float(field) for field in line.split()] for line in lines]
a = N.array(a_list)

X = N.reshape(a[:,0], (nx, ny))
Y = N.reshape(a[:,1], (nx, ny))
Z = N.reshape(a[:,2], (nx, ny))

ax = subplot(1,2,1)
ax.pcolor(X,Y,Z, shading='flat')

ax = subplot(1,2,2)
ax.pcolormesh(X,Y,Z, shading='flat')

show()