如何更改 y 轴的比例以在热图中看得更清楚

问题描述

我目前有一个热图的比例问题:

enter image description here

正如你在开头和结尾看到的那样,有一个温度变化,但由于它是在很小的距离上完成的,而且比例很大,所以我们根本看不到任何东西。

那么有没有办法或功能解决这个问题并自动应用更好的比例以更好地查看?有变化的地方用小尺度,没有变化的地方用大尺度?

这是生成此图像的代码

x = np.linspace(0,L,Nx+1) #array for y-axis
t = np.linspace(0.0,t_fin,Nt+1) #array to plot the time in the title

x = np.round(x,2) #change decimals 
t = np.round(t,5)


y = np.arange(T[Nt,:].shape[0]) #T[Nt,:] is an array that contains the temperature
my_yticks = x #change the number of points in the y-axis
frequency = 100


data = np.vstack(T[Nt,:]) #to use in imshow
df = pd.DataFrame(data)

fig = plt.figure(figsize=(3,9)) #plotting
titre = f"Température à {t[Nt]} s"
plt.ylabel('Profondeur en m')
plt.yticks(y[::frequency],my_yticks[::frequency])
im = plt.imshow(df,cmap='jet',aspect ='auto',interpolation='bilinear')
ax = plt.gca()
ax.get_xaxis().set_visible(False)
cb = plt.colorbar()
cb.set_label('Température en °C')
plt.title(titre)

如果您有任何问题,请不要犹豫。

谢谢!

解决方法

您可以在 y 轴上使用 logit 刻度。但是,这不适用于 imshow,因为值必须完全介于 0 和 1 之间。您可以改用 pcolormesh

import matplotlib.pyplot as plt
import numpy as np

M,N = 100,20
a = np.array(M*[15])
a[:3] = [0,5,10]
a[-3:] = [20,25,30]
a = np.outer(a,np.ones(N))

fig,(axl,axr) = plt.subplots(ncols=2,figsize=(3,6))

axl.imshow(a,cmap='jet',aspect ='auto',interpolation='bilinear',extent=(N,M,0))
axl.yaxis.set_ticklabels([f'{t/M*10:.3g}' for t in  axl.yaxis.get_ticklocs()])
axl.get_xaxis().set_visible(False)
axl.set_title('linear')

eps = 1e-3
X,Y = np.meshgrid(np.linspace(0,1,N),np.linspace(1-eps,eps,M))
cm = axr.pcolormesh(X,Y,a,shading='gouraud')
axr.set_yscale('logit')
axr.yaxis.set_ticklabels([f'{10*(1-t):.3g}' for t in  axr.yaxis.get_ticklocs()])
axr.get_xaxis().set_visible(False)
axr.set_title('logit')

cb = plt.colorbar(cm,pad=0.2)

enter image description here

警告:仅当您不想平移/缩放图像时,将 y 标签设置为固定值才有用。

,

您可以通过 ax.set_ylim([,]) 设置 y 轴限制