Python plt.imshow()图的yticks标签对齐

Python plt.imshow()图的yticks标签对齐,python,matplotlib,Python,Matplotlib,我有一个pb,刻度的标签与imshow图形对齐。我想在两个Y形刻度之间居中放置标签,但我没有成功。 我知道我们可以找到很多例子(包括StackOverflow),但我没有找到我想要的 import datetime dateFMT = '%Y-%m-%d %H:%M:%S' import pandas as pd import numpy as np import matplotlib.pyplot as plt # Fontsize ftsize = 12 # - Months in Fr

我有一个pb,刻度的标签与imshow图形对齐。我想在两个Y形刻度之间居中放置标签,但我没有成功。 我知道我们可以找到很多例子(包括StackOverflow),但我没有找到我想要的

import datetime
dateFMT = '%Y-%m-%d %H:%M:%S'
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
# Fontsize
ftsize = 12

# - Months in French 
tabMois=['Janvier','Fevrier','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre',
 'Octobre','Novembre','Decembre']

# - Nb of Days of Measurements 
NbOfDays = 7.99

# - Generating a DateTimeIndex from January the 1st and during ~8 days
start = datetime.datetime.strptime('2020-01-01 00:00:00', dateFMT)
end = start + datetime.timedelta(days=NbOfDays)
times = pd.date_range(freq='60Min', start=start, end=end)

# - Total number of values
NbOfDates = len (times)

# - Creating the dataframe with random values
df = pd.DataFrame(np.random.randint(0,4,size=(NbOfDates, 1)), 
                  columns=['Temperature'], index=times)

# - Creating 2 columns for referencing dates and hours
date = [d.strftime('%d/%m') for d in df.index]
df['jour'] = date
heure = [d.strftime('%H') for d in df.index]
df['heure'] = heure

# - Creating a new DataFrame (dfPivot) containing the matrix of the previous 
# Dataframe
dfPivot = df.pivot(index='jour', columns='heure', values='Temperature')

# - X Labels for the plot
XTicksLabels = [ str(i)+'h' for i in dfPivot.columns ]

# - Small function to split days and monthes and to translate in French
def splitDate( i ):
    day = i.split('/')[0]
    month = int(i.split('/')[1])
    monthInFrench = tabMois[month-1]
    label = str(day)+' '+monthInFrench
    return label

# - Y Labels for the plot
YTicksLabels = [ splitDate( i ) for i in dfPivot.index ]

# - Plot
fig, ax = plt.subplots()

im = ax.imshow(dfPivot, aspect = 'auto', interpolation='None')

ax.xaxis.set(ticks=np.arange(0.5, len(XTicksLabels)), ticklabels=XTicksLabels)
ax.set_xticklabels(XTicksLabels, rotation=90, ha='right', minor=False)

ax.yaxis.set(ticks=np.arange(0.5, len(YTicksLabels)), ticklabels=YTicksLabels)
ax.set_yticklabels(YTicksLabels, rotation=0, ha='right', va = 'baseline',
                   minor=False, fontsize=ftsize)

ax.tick_params('y', length=10)
fig.colorbar(im)

plt.ylabel('Jours', size=ftsize)
plt.xlabel('Heures', size=ftsize)

plt.tight_layout()
plt.show()
由代码生成的图像

根据答案,您应该能够使用

# Create offset transform by 5 points in y direction
dx = 0/72.; dy = 10/72. 
offset = matplotlib.transforms.ScaledTranslation(dx, dy, fig.dpi_scale_trans)

# apply offset transform to all x ticklabels.
for label in ax.yaxis.get_majorticklabels():
    label.set_transform(label.get_transform() + offset)
作为使用数据的完整示例

import datetime
dateFMT = '%Y-%m-%d %H:%M:%S'
import pandas as pd
import numpy as np
import matplotlib.transforms
import matplotlib.pyplot as plt
# Fontsize
ftsize = 12

# - Months in French 
tabMois=['Janvier','Fevrier','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Decembre']

# - Nb of Days of Measurements 
NbOfDays = 7.99

# - Generating a DateTimeIndex from January the 1st and during ~8 days
start = datetime.datetime.strptime('2020-01-01 00:00:00', dateFMT)
end = start + datetime.timedelta(days=NbOfDays)
times = pd.date_range(freq='60Min', start=start, end=end)

# - Total number of values
NbOfDates = len (times)

# - Creating the dataframe with random values
df = pd.DataFrame(np.random.randint(0,4,size=(NbOfDates, 1)), columns=['Temperature'], index=times)

# - Creating 2 columns for referencing dates and hours
date = [d.strftime('%d/%m') for d in df.index]
df['jour'] = date
heure = [d.strftime('%H') for d in df.index]
df['heure'] = heure

# - Creating a new DataFrame (dfPivot) containing the matrix of the previous 
# Dataframe
dfPivot = df.pivot(index='jour', columns='heure', values='Temperature')

# - X Labels for the plot
XTicksLabels = [ str(i)+'h' for i in dfPivot.columns ]

# - Small function to split days and monthes and to translate in French
def splitDate( i ):
    day = i.split('/')[0]
    month = int(i.split('/')[1])
    monthInFrench = tabMois[month-1]
    label = str(day)+' '+monthInFrench
    return label

# - Y Labels for the plot
YTicksLabels = [ splitDate( i ) for i in dfPivot.index ]

# - Plot
fig, ax = plt.subplots()

im = ax.imshow(dfPivot, aspect = 'auto', interpolation='None')

ax.xaxis.set(ticks=np.arange(0.5, len(XTicksLabels)), ticklabels=XTicksLabels)
ax.set_xticklabels(XTicksLabels, rotation=90, ha='right', minor=False)

ax.yaxis.set(ticks=np.arange(0.5, len(YTicksLabels)), ticklabels=YTicksLabels)
ax.set_yticklabels(YTicksLabels, rotation=0, ha='right', va = 'baseline', minor=False, fontsize=ftsize)

ax.tick_params('y', length=10)
fig.colorbar(im)

plt.ylabel('Jours', size=ftsize)
plt.xlabel('Heures', size=ftsize)

# Create offset transform by 5 points in y direction
dx = 0/72.; dy = 10/72. 
offset = matplotlib.transforms.ScaledTranslation(dx, dy, fig.dpi_scale_trans)

# apply offset transform to all x ticklabels.
for label in ax.yaxis.get_majorticklabels():
    label.set_transform(label.get_transform() + offset)

plt.tight_layout()
plt.show()
这就给了,

试着这样做:

  • 将标签移高一点(例如0.1)
  • 用小蜱虫在两天之间做较大的间隔,把它们放在大蜱虫之前的位置
  • 通过将主记号的长度设置为零来删除主记号
ax.yaxis.set(ticks=np.arange(0.1,len(YTicksLabels)),ticklebels=YTicksLabels)
ax.set_-yticks(滴答声=np.arange(0.5,len(yticklabels)),minor=True)
最大勾选参数(轴为y轴,其中为次轴,长度为10)
ax.勾选参数(轴=y',其中=major',长度=0)