Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中对数据表进行排序?_Python_Sorting_Matplotlib - Fatal编程技术网

如何在python中对数据表进行排序?

如何在python中对数据表进行排序?,python,sorting,matplotlib,Python,Sorting,Matplotlib,出于某种原因,我有一个未排序的数据表(仅关于前两列),如下所示: 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 1 1 ? 1 2 ? 1 3 ? 2 1 ? 2 2 ? 2 3 ? 3 1 ? 3 2 ? 3 3 ? 要在python中正确地绘制此图(在matplotlib中使用imshow),我需要对内容进行如下排序: 1 2 0.9 2 2 0.4 3 3 0.3 1 3 0.4 1 1

出于某种原因,我有一个未排序的数据表(仅关于前两列),如下所示:

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
1 1 ?
1 2 ?
1 3 ?
2 1 ?
2 2 ?
2 3 ?
3 1 ?
3 2 ?
3 3 ?
要在python中正确地绘制此图(在matplotlib中使用imshow),我需要对内容进行如下排序:

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
1 1 ?
1 2 ?
1 3 ?
2 1 ?
2 2 ?
2 3 ?
3 1 ?
3 2 ?
3 3 ?
问号是第三列的相关值


另一方面,如果有人知道如何使用imshow绘制未排序的数据表,这也会很有帮助。将以下数据表视为由前两列协调的3x3表,每个组件的内容由3ed列中的值以颜色表示

如果您的数据如下所示:

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]]
你可以

data.sort()
按第一个元素、第二个元素、第三个元素等列出默认排序顺序。 要将数据转换为该格式,您可以:

with open("./data.dat") as file:
    data = [map(float, ln.split()) for ln in file]

到目前为止,您的代码是什么样子的?您目前是如何用Python存储表数据的?我做了以下操作,但不是我想要的:data=np.loadtxt(“./data.dat”);数据=排序(数据,轴=0)谢谢!但是我有一个file.dat,我怎样才能把它转换成这种格式。看起来,当我执行data=np.loadtxt(“./data.dat”)时,data.sort()不起作用@曼哈顿博士补充道:谢谢你的回复。但现在它似乎在以下几行中产生了另一个问题,试图通过imshow绘制排序后的数据:x3,y3,z3=data.T nrows,ncols=4,4 grid3=z3。重塑((nrows,ncols))fig3=plt.gcf()plt.imshow(grid3,extent=(x3.min(),x3.max(),y3.min(),y3.max()),origin='lower',interpolation='nect',cmap=cm.gist\u)