简体   繁体   中英

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

I have a file like this:

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

and I'm using the following code to open it and plot my data

import numpy as np
import matplotlib.pyplot as plt

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

plt.imshow(data)

The question is: how can I use the values inside the numpy.ndarray "wvl" as the x-axis tick labels for my heatmap?

I have already tried with plt.xticks(range(wvl.size),wvl) but in the real case the length of my array is 512 which leads to an unreadable result.

If I understood correctly, the issue here is the large number of x-axis ticks. In that case, to have a readable solution, one way is to put the ticks at every n th step. You can additionally choose to rotate them or not as a matter of personal taste. To do so, you can do like following.

I just extended your data set four times to have mode data points.

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);

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM