0

PREAMBLE: I have seen these, but I can't figure out from the answer how to do the plot. Also, I'm new with python and matplotlib.

I have a data file of the form

X        Y       Z
0.05     1       z
0.10     1       z
...     ...     ...
0.95     1       z
0.05     2       z
...     ...     ...
...     ...     ...
0.95    10       z

with z in [-0.02:0.5] for each of them. These results in 190 (x,y,z) points.

I acquire the data in this way

data_file = open('tau.txt', 'r')
buffer = data_file.read()
data_file.close()

data = [map(float, row.split('\t')) for row 
        buffer.strip().split("\n")]

As the link suggests, I convert them into a grid

mu = []
alpha = [] 
tau = []

for elements in data:
    mu.append(elements[0])
    alpha.append(elements[1])
    tau.append(elements[2])

x_data = np.asarray(mu)
y_data = np.asarray(alpha)
z_data = np.asarray(tau)

xi = np.linspace(0.05,0.95,19)
yi = np.linspace(1,10,10)
ar = griddata(x_data,y_data,z_data,xi,yi,interp='nn')

Then I do the plot: I would like this so that each (x,y) co-ordinate has a square centered on the co-ordinate, with a colorbar showing the z value.

cmap = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',
           ['white','grey','black'],256)

img = plt.imshow(ar,interpolation='nearest',cmap = 
           cmap,origin='lower')

plt.colorbar(img,cmap=cmap)

I obtain this:this

First of all, I want the colourbar to be of the same height of the plot itself. I can't understand how to avoid this trash.

Moreover, if you look at the file you immediately see that ranges are not right: x has to be in [0.05:0.95] and y in [1:10]. y is simply shifted of 1 (the white lines, with all z=0 should be for y=1 and not y=0), while x assumes values I can't understand.

I this is important to note that except for these, the plot is right, both in the z values and in the trend.

How can I fix my problem(s)?

DavidG
  • 24,279
  • 14
  • 89
  • 82
GRquanti
  • 527
  • 8
  • 23

1 Answers1

1

imshow is rather used for plotting images and matrices using a grid the same size as your matrix or image. Thats why your x- and y- axis are that way.

For what you are trying to do use pcolormesh or pcolor in combination with numpy.meshgrid to get the correct x and y spacing.

These functions should also support non-regular grid spacings.

This page has some information on how it works.

Joe
  • 6,758
  • 2
  • 26
  • 47