-1

For some reason I have an unsorted table of data (just regarding to the first two columns) as following:

1 2 0.9
2 2 0.4
3 3 0.3
1 3 0.4
1 1 0.5
2 3 0.9
2 1 0.9
3 1 0.8
3 2 0.9

To plot this properly in python (with imshow at matplotlib) I need to sort the content as following:

1 1 ?
1 2 ?
1 3 ?
2 1 ?
2 2 ?
2 3 ?
3 1 ?
3 2 ?
3 3 ?

question mark is the related values for the third column.

In another hand, if somebody knows about how to use imshow to plot unsorted data tables that would be helpful too. Think about the following data table as a 3x3 table coordinated by the first two columns and the content of each components is indicated by the value of in the 3ed column by a color.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rotail
  • 1,025
  • 4
  • 19
  • 40
  • What does your code look like so far? How are you currently storing the table data in Python? – Nacho May 20 '14 at 20:57
  • I did the following but it is not quite what I want: data= np.loadtxt("./data.dat" ); data=sort(data, axis=0) – Rotail May 20 '14 at 21:08

1 Answers1

4

provided your data looks like this:

data=[[1, 2, 0.9],
      [2, 2, 0.4],
      [3, 3, 0.3],
      [1, 3, 0.4],
      [1, 1, 0.5],
      [2, 3, 0.9],
      [2, 1, 0.9],
      [3, 1, 0.8],
      [3, 2, 0.9]]

you can just

data.sort()

lists default sort order are by first element, second element, third element, etc. To get your data into that format you can:

with open("./data.dat") as file:
    data = [map(float, ln.split()) for ln in file]
cmd
  • 5,754
  • 16
  • 30
  • Thanks! But I have a file.dat, how can I convert that to this format. It seems, data.sort() doesn't work when I do data= np.loadtxt("./data.dat" ); – Rotail May 20 '14 at 21:14
  • Thanks for reply. But it seems now it generates another issue in the following lines, trying to plot the sorted data via imshow: x3,y3,z3 = data.T nrows, ncols = 4, 4 grid3 = z3.reshape((nrows, ncols)) fig3 = plt.gcf() plt.imshow(grid3, extent=(x3.min(), x3.max(), y3.min(), y3.max()), origin='lower', interpolation='nearest', cmap=cm.gist_rainbow) – Rotail May 20 '14 at 21:25