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

Python 如何更改y轴的比例以在热图中看得更清楚,python,matplotlib,heatmap,imshow,Python,Matplotlib,Heatmap,Imshow,我目前在热图上有一个比例问题: 正如你在开始和结束时所看到的,有温度变化,但由于它是在非常小的距离上进行的,而且规模很大,我们根本看不到任何东西 那么,有没有一种方法或功能来解决这个问题,并自动应用更好的比例,以便更好地查看?在有变化的地方应用小尺度,在没有变化的时候应用大尺度 以下是生成此图像的代码: x = np.linspace(0,L,Nx+1) #array for y-axis t = np.linspace(0.0, t_fin,Nt+1) #array to plot the

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

正如你在开始和结束时所看到的,有温度变化,但由于它是在非常小的距离上进行的,而且规模很大,我们根本看不到任何东西

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

以下是生成此图像的代码:

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)
如果你有任何问题,不要犹豫


谢谢大家!

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

您可以在y轴上使用刻度。但是,这不适用于,因为值必须在0和1之间。你可以用一个新的

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

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,0,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, cmap='jet', 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)