What I have
I am trying to create a 'heatmap' of values using python 3
.
I'm stuck on the spacing between the y-ticks, and several SO questions could not help me out...
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
What I want
The labels on the y-axis are wrong.
Specifically, the y-axis should have the following labels (which are spaced in a logarithmic fashion):
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):
Plot with correct y-label spacing
So:
- The plot should not be altered
- The y-axis labels should be converted from linear to log
- The spacing between the labels should be as in the latter plot