更改绘图上的轴值
如何更改一个轴上的数据?
我正在对一些数据进行频谱分析,我的 x 轴是某个矩阵的索引。我想更改它,使 x 轴成为数据本身。
我使用 imshow() 来绘制数据(我有一个矩阵,其元素具有一定的强度,y 轴是它们的探测器-源对应对,x 轴应该是它们的频率)。
它的代码写在这里:
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')
plt.colorbar()
locs, labels = xticks(find(b[1]), b[1])
plt.axis('tight')
ax.xaxis.set_major_locator(MaxNLocator(5))
我认为如果有一种方法可以将某个数组的索引与其值互换,我的问题就可以解决。
我成功地使用了 locs, labels = xticks(find(b[1]), b[1]) 行。但在我的图表上,我的轴间隔就不正确......我认为这与 MaxNLocator (我用来减少刻度数)有关。
如果我使用 xlim,我可以将数字设置为我想要的,但 x 轴仍然相同(在该 xlim 上我必须使用原始数据将其设置正确)。
我做错了什么?
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')
plt.colorbar()
locs, labels = xticks(find(b[1]), b[1])
plt.axis('tight')
ax.xaxis.set_major_locator(MaxNLocator(5))
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以使用此 示例中举例说明的
xticks
方法。还有更复杂的方法可以做到这一点。请参阅股票代码。
Yes, you can use the
xticks
method exemplified in this example.There are also more sophisticated ways of doing it. See ticker.