简体   繁体   English

在不更改图的情况下重新缩放y轴

[英]Rescale y-axis without altering plot

What I have 我有的

I am trying to create a 'heatmap' of values using python 3 . 我正在尝试使用python 3创建值的“热图”。
I'm stuck on the spacing between the y-ticks, and several SO questions could not help me out... 我卡在y刻度之间的间距上,几个SO问题无法帮助我...

The code that I have so far is the following: 到目前为止,我的代码如下:

data = np.load('path\to\data\datafile.npy')

# plot using imshow (and use interpolation)
plt.imshow(data,
           cmap          = 'RdBu_r', 
           aspect        = 'auto', 
           vmin          = -.4,
           vmax          =  .4,
           origin        = 'lower',
           extent        = [-1000, 4500, 2, 48], 
           interpolation = 'hanning')

# plt parameters
plt.xlim(-750, 4250)
plt.colorbar()
plt.vlines(0, 
           frequencies[0], 
           frequencies[-1], 
           colors = 'black',
           label = 'Stimulus onset',
           linewidth = 2.5)
plt.legend(loc=(1,1.04))
plt.title('Time-frequency using Morlet wavelets\nSubject: {}'.format(SUB_ID))
plt.ylabel('Frequency (in Hz)')
plt.xlabel('Time (in ms)')
plt.show()

This gives me the following plot: 这给了我以下情节:

Plot with wrong y-axis scaling y轴缩放比例错误的绘图

What I want 我想要的是

The labels on the y-axis are wrong. y轴上的标签错误。
Specifically, the y-axis should have the following labels (which are spaced in a logarithmic fashion): 具体地说,y轴应具有以下标签(以对数方式隔开):

In [15]:
# parameters
min_freq = 2;
max_freq = 48;
num_frex = 20;

# define frequencies of interest
frequencies = np.logspace(np.log10(min_freq), 
                          np.log10(max_freq), 
                          num_frex)
frequencies

Out[15]: 
array([ 2.        ,  2.36413729,  2.79457255,  3.30337659,  3.90481788,
        4.61576277,  5.45614844,  6.44954198,  7.62380134,  9.01185651,
       10.652633  , 12.59214343, 14.8847779 , 17.59482922, 20.7982959 ,
       24.58501342, 29.06117345, 34.35230187, 40.60677887, 48.        ])

The resulting plot should look like this (when focusing on the y-axis): 结果图应如下所示(当聚焦于y轴时):

Plot with correct y-label spacing 以正确的y标签间距绘制

So: 所以:

  • The plot should not be altered 情节不应该改变
  • The y-axis labels should be converted from linear to log y轴标签应从线性转换为对数
  • The spacing between the labels should be as in the latter plot 标签之间的间距应与后面的图相同

datafile.npy can be found here datafile.npy可以在这里找到

You just need to provide the plot with a custom ytick list: 您只需要为绘图提供自定义的ytick列表:

# parameters
min_freq = 2;
max_freq = 48;
num_frex = 20;

# define frequencies of interest
frequencies = np.logspace(np.log10(min_freq), 
                      np.log10(max_freq), 
                      num_frex)

plt.yticks(frequencies)

see https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.yticks.html#matplotlib.pyplot.yticks for more 有关更多信息,请参见https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.yticks.html#matplotlib.pyplot.yticks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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