python - 使用 matplotlib 制作 2D 像素图

标签 python grid matplotlib plot pixel

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

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 准备一种 2D 图,像素为 100x100,其中每个像素都有一种颜色(彩虹色从红色到紫色,从第三列的最小值到最大值)第三列的值,并从此文件中读取数据。我想知道使用 matplotlib 的最佳方法是什么

最佳答案

根据 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

关于python - 使用 matplotlib 制作 2D 像素图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6323737/

相关文章:

python - 如何在 TensorFlow 中实现递归神经网络?

python - 了解如何使用 Google OR 工具设置软约束

python - Tkinter 16 个网格(4x4 方向),每个网格有 5x5 按钮网格

html - 我的 div 容器随着浏览器不断缩小

python - 检查点是否在python中的多边形内的最快方法是什么

Python:强制新型类

Python tkinter - 编译成功,但编译为 Exe 时程序运行不正确

css - 960.gs 的固定-流体-固定布局

python - 使用 Matplotlib imshow() 显示缩放 = 1 的图像(如何?)

python - pandas Series'对象没有属性 'find'