用matplotlib制作2D像素图 [英] Make a 2D pixel plot with matplotlib
问题描述
我从一些计算中得到了以下数据:
I got the following data from some calculations:
x, y, temp
其中x和y是维度为10x10的二维框中某个点的坐标。间距等于0.1。因此,有10000个不同点,结果文件如下所示:
where x and y are the coordinates of a point in a 2D box of dimensions 10x10. Spacing is equal to 0.1. So there are 10000 different points and the resulting file looks like:
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像素的2D图,其中每个像素获得一种颜色(彩虹颜色从红到紫,从第三列的最小值到最大值)并从该文件读取数据。我想知道matplotlib最好的方法是什么
I would like to prepare kind of a 2D plot with matplotlib, with 100x100 pixels, where each pixel gets a colour (rainbow colors going from red to violet, from the minimum to the maximum values of the third column) according to the value of the 3rd column, and data is read from this file. I wonder what is the best approach for this with matplotlib
推荐答案
基于它看起来像x,y,temp三元组(按行列出),您可以重新塑造临时列。
Based on the way it looks like your x,y,temp triples are ordered (listed out in rows), you can just reshape the "temp" column.
例如
E.g.
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_rainbow
。 matplotlib.cm.hsv
在底部回到红色。请参阅此处: https://matplotlib.org/users/colormaps.html 获取colormaps。
You probably actually wanted hsv
is the "rainbow" colormap you were referring to.matplotlib.cm.gist_rainbow
. matplotlib.cm.hsv
goes back to red at the bottom. See here: https://matplotlib.org/users/colormaps.html for a list of colormaps.
如果你的x,y,temp三元组没有实际排序,那么你需要重新设定你的点数。我在对上一个问题的回答中展示了一个例子。
If your x,y,temp triplets aren't actually ordered, then you'll need to regrid your points. I showed an example of this in my answer to your previous question.
这篇关于用matplotlib制作2D像素图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!