是否可以在子图中控制xlabel步进标签?

时间:2018-12-27 13:02:53

标签: python matplotlib label subplot

我认为这是关于子图的老问题。但是,我找不到答案。

问题:
可以让我的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')

这是输出 timestep in x axis does not start from first date

2 个答案:

答案 0 :(得分:1)

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

xticks documentation

xlim documentation

他们还应该与子图一起很好地工作。

另一种解决方法是使用:

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.

P.S。我没有足够的声誉,因此无法发表评论。

答案 1 :(得分:0)

感谢@ElidorDD的回答。我只是知道他的正确措辞。这是我的问题的答案,它是从$newNameserver = '8.8.4.4' Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter 'IPEnabled=True' | ForEach-Object { $dnsServers = $_.DNSServerSearchOrder[0], $newNameserver $_.SetDNSServerSearchOrder($dnsServers) } 类而不是matplotlib.axes.Axes

继承的
matplotlib.pyplot

Same tick for all months, but keep picture short just 3 months