使用matplotlib制作2D像素图

时间:2023-02-09 08:57:54

I got the following data from some calculations:

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

x, y, temp

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:

其中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

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

我想准备一个带有matplotlib的2D绘图,具有100x100像素,其中每个像素获得一种颜色(彩虹颜色从红色变为紫色,从第三列的最小值到最大值)根据第3列,从该文件中读取数据。我想知道使用matplotlib最好的方法是什么

1 个解决方案

#1


29  

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.

基于它看起来像你的x,y,temp三元组的排序方式(在行中列出),你可以重塑“temp”列。

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 is the "rainbow" colormap you were referring to. Edit: You probably actually wanted 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.

hsv是你所指的“彩虹”色彩图。编辑:你可能真的想要matplotlib.cm.gist_rainbow。 matplotlib.cm.hsv在底部返回红色。请参阅此处:https://matplotlib.org/users/colormaps.html以获取色彩映射列表。

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.

如果您的x,y,temp三元组实际上没有订购,那么您需要重新计算您的积分。我在回答你上一个问题时给出了一个例子。

使用matplotlib制作2D像素图

#1


29  

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.

基于它看起来像你的x,y,temp三元组的排序方式(在行中列出),你可以重塑“temp”列。

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 is the "rainbow" colormap you were referring to. Edit: You probably actually wanted 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.

hsv是你所指的“彩虹”色彩图。编辑:你可能真的想要matplotlib.cm.gist_rainbow。 matplotlib.cm.hsv在底部返回红色。请参阅此处:https://matplotlib.org/users/colormaps.html以获取色彩映射列表。

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.

如果您的x,y,temp三元组实际上没有订购,那么您需要重新计算您的积分。我在回答你上一个问题时给出了一个例子。

使用matplotlib制作2D像素图