首页 > 解决方案 > plt.imshow() 图的 yticks 标签的对齐方式

问题描述

我有一个 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()

代码生成的图像。

在此处输入图像描述

标签: pythonmatplotlib

解决方案


根据这个答案,您应该可以使用,

# 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()

这使,

在此处输入图像描述


推荐阅读