首页 > 解决方案 > 是否可以在子图中控制 xlabel 步进标签?

问题描述

我相信这是一个关于子情节的老问题。但是,我无法找到我的答案。

问题:
是否可以让我subplot同样踩到x-label

y_label = 'db_server_6a_2018_df_IL1-A'

f, ax_list = plt.subplots(12, 1, gridspec_kw = {
    'height_ratios': [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
    'width_ratios': [30]
}, figsize=(20, 40))
for month in range(1, 12 + 1):
    start_dt = datetime.datetime(2018, month, 1)
    end_dt = start_dt + relativedelta(months=1)
    window_df = df[(start_dt < df['Time']) & (df['Time'] <= end_dt)]

    ax = ax_list[month-1]
    ax.plot(window_df['Time'], window_df[y_label])
    ax.set_ylabel(start_dt.strftime('%B'))
    ax.set_xlabel('datetime')
f.savefig(y_label + '.png')

这是输出 x 轴上的时间步长不是从第一个日期开始的

标签: pythonmatplotliblabelsubplot

解决方案


您是否已经尝试设置 xticks() 和 xlim(),即分别为每个子图设置应放置刻度的新位置列表并设置新 x 轴限制的元组?

xticks 文档

xlim 文档

它们也应该与子图很好地配合使用。

另一种解决方案可能是使用:

plt.subplot(something, sharex=ax, sharey=ax)

在哪里,如文档中所述

sharex, sharey : Axes, optional
Share the x or y axis with sharex and/or sharey. 
The axis will have the same limits, ticks, and scale as the axis of the shared axes.

PS我不能写评论,因为我没有足够的声誉。


推荐阅读