使用matplotlib绘制二维像素图

2024-05-21 20:22:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一些计算中得到了以下数据:

x, y, temp

其中x和y是尺寸为10x10的2D框中的点的坐标。间距等于0.1。因此有10000个不同的点,结果文件如下:

0.0 0.0 5.6
0.1 0.0 3.2
0.2 0.0 4.1
...
9.9 9.9 2.1

我想用matplotlib准备一种二维图,像素为100x100,每个像素根据第三列的值得到一种颜色(彩虹颜色从红色变为紫色,从第三列的最小值到最大值),并从这个文件读取数据。我想知道matplotlib的最佳方法是什么


Tags: 文件数据方法matplotlib颜色尺寸像素读取数据
1条回答
网友
1楼 · 发布于 2024-05-21 20:22:12

根据x,y,temp三元组的排序方式(按行列出),您只需重新调整“temp”列。

例如

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

x,y,temp = np.loadtxt('data.txt').T #Transposed for easier unpacking
nrows, ncols = 100, 100
grid = temp.reshape((nrows, ncols))

plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min()),
           interpolation='nearest', cmap=cm.gist_rainbow)
plt.show()

hsv是您所指的“彩虹”颜色映射。编辑:您可能实际上需要matplotlib.cm.gist_rainbowmatplotlib.cm.hsv返回底部的红色。请参见此处:https://matplotlib.org/users/colormaps.html以获取颜色映射列表。

如果你的x,y,temp三胞胎实际上没有订购,那么你需要重新计算你的分数。我在上一个问题的my answer中展示了一个例子。

enter image description here

相关问题 更多 >