我的建议是使用subplots ,并在Axes类上操作 xticks 。
例如,对于单个图:
_, axis = plt.subplots(1, 1) # create an Axes class for a single plot
axis.hist(data, bins=12)
axis.set_xticklabels(axis.get_xticks(), rotation=60)
plt.show()
上面的代码产生了一个具有以下美学的图:
此外,考虑到在您的情况下,您正在绘制许多特征,您可以使用 subplots 函数生成绘图网格,如下所示:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
holiday_features = [name for name in df.columns if 'DAYS FROM' in name]
# define number of rows and columns to the subplot
ncols = 3 # considering a grid with 3 plots per row
nrows = int(np.ceil(len(holiday_features)/3))
# create subplots
_, axes = plt.subplots(nrows, ncols)
axes = axes.flatten()
# plot features with rotated ticks
for j, feature in enumerate(holiday_features):
axes[j].hist(df[feature], bins=12)
axes[j].set_title(feature)
axes[j].set_xticklabels(axes[j].get_xticks(), rotation=60)
# remove empty plots
for k in range(j, len(axes)):
sns.despine(ax=axes[k], top=True, right=True, left=True, bottom=True)
axes[k].xaxis.set_major_formatter(ticker.NullFormatter())
axes[k].xaxis.set_ticks_position('none')
axes[k].yaxis.set_major_formatter(ticker.NullFormatter())
axes[k].yaxis.set_ticks_position('none')
# show plot
plt.tight_layout()
plt.show()