使用矩阵第一行的值作为 matplotlib.pyplot.imshow 的刻度

Using the values of the first row of a matrix as ticks for matplotlib.pyplot.imshow

我有这样一个文件:

820.5   815.3   810.4
2061    2082    2098
2094    2119    2071
2067    2079    2080
2095    2080    2116
2069    2103    2108

我正在使用以下代码打开它并绘制我的数据

import numpy as np
import matplotlib.pyplot as plt

img = np.loadtxt('example.txt')
wvl = img[0,:]
data = img[1:,:]

plt.imshow(data)

问题是:如何使用 numpy.ndarray "wvl" 中的值作为我的热图的 x 轴刻度标签?

我已经尝试使用 plt.xticks(range(wvl.size),wvl) 但在实际情况下,我的数组长度为 512,这会导致不可读的结果。

如果我没理解错的话,这里的问题是大量的 x 轴刻度。在这种情况下,要获得可读的解决方案,一种方法是在每个 n 步骤放置刻度。您还可以根据个人喜好选择是否旋转它们。为此,您可以执行以下操作。

我刚刚将你的数据集扩展了四次以获得众数数据点。

import numpy as np
import matplotlib.pyplot as plt

img = np.loadtxt('example.txt')

wvl = img[0,:]
data = img[1:,:]
ax.imshow(data)

step = 3
plt.xticks(range(0, wvl.size, step),wvl[::step], rotation=45);