Rodrigo Forti Rodrigo Forti - 1 year ago 99
Python Question

Changing axis values on a plot

How can I change the data on one axis?

I'm making some spectrum analysis on some data and my x-axis is the index of some matrix. I'd like to change it so that the x-axis becomes the data itself.

I'm using the imshow() to plot the data (I have a matrix whose elements are some intensity, the y axes are their detector-source correspondent pair and the x-axis should be their frequency).

The code for it is written down here:

def pltspec(dOD, self):
idx = 0
b = plt.psd(dOD[:,idx],Fs=self.fs,NFFT=512)
B = np.zeros((2*len(self.Chan),len(b[0])))
for idx in range(2*len(self.Chan)):
b = plt.psd(dOD[:,idx],Fs=self.fs,NFFT=512)
B[idx,:] = 20*log10(b[0])
fig = plt.figure()
ax = fig.add_subplot(111)
plt.imshow(B, origin = 'lower')
locs, labels = xticks(find(b[1]), b[1])

I think if there's a way of interchanging the index of some array with its value, my problem would be solved.

I've managed to use the line
locs, labels = xticks(find(b[1]), b[1])
. But with it on my graph my axis interval just isn't right... I think it has something to do with the MaxNLocator (which I used to decrease the number of ticks).

And if I use the xlim, I can set the figure to be what I want, but the x axis is still the same (on that xlim I had to use the original data to set it right).

What am I doing wrong?


Yes, you can use the xticks method exemplified in this example.

There are also more sophisticated ways of doing it. See ticker.