简体   繁体   English

set_major_locator是否从以前的子图中删除x-ticks(和标签)?

[英]`set_major_locator` removes x-ticks (and labels) from previous subplots?

I ran into this strange problem with using set_major_locator() , when using subplots which have different x-axis limits. 当使用具有不同x轴限制的子图时,使用set_major_locator()遇到了这个奇怪的问题。 A minimal example: 一个最小的例子:

import matplotlib.pyplot as pl
import matplotlib.dates as mdates
from datetime import datetime

h24 = mdates.HourLocator(interval=24)
fmt = mdates.DateFormatter('%d-%m %H:%M')

start1 = datetime(year=2016, month=7, day=7, hour=0)
end1   = datetime(year=2016, month=7, day=9, hour=0)

start2 = datetime(year=2016, month=9, day=30, hour=0)
end2   = datetime(year=2016, month=10, day=2, hour=0)

start3 = datetime(year=2016, month=5, day=8,  hour=0)
end3   = datetime(year=2016, month=5, day=10, hour=0)

pl.figure(figsize=(9,3))

ax=pl.subplot(131)
ax.set_xlim(start1, end1)
ax.xaxis.set_major_locator(h24)
ax.xaxis.set_major_formatter(fmt)

ax=pl.subplot(132)
ax.set_xlim(start2, end2)
ax.xaxis.set_major_locator(h24)
ax.xaxis.set_major_formatter(fmt)

ax=pl.subplot(133)
ax.set_xlim(start3, end3)
ax.xaxis.set_major_locator(h24)
ax.xaxis.set_major_formatter(fmt)

pl.tight_layout()

Which results in: 结果是:

在此处输入图片说明

If I set the x-limit of all sub-plots the same (using in this case ax.set_xlim(start1, end1) for all sub-plots) it works as expected: 如果我将所有子图的x限制设置为相同(在本例中为所有子图使用ax.set_xlim(start1, end1) ),则它将按预期工作:

在此处输入图片说明

Also, leaving the different set_xlim() 's and removing the set_major_locator() and set_major_formatter() lines works (although I get unreadable x-labels in this case..): 另外,保留不同的set_xlim()并删除set_major_locator()set_major_formatter()行是set_major_formatter() (尽管在这种情况下,我得到了不可读的x标签。):

在此处输入图片说明

Am I making a silly mistake somewhere, or are the missing x-ticks and labels in my first example a bug in Matplotlib? 我是在某个地方犯了一个愚蠢的错误,还是我的第一个示例中缺少的x标记和标签是Matplotlib中的错误?

ps Matplotlib 3.0.2, Python 3.7.2 ps Matplotlib 3.0.2,Python 3.7.2

As of current versions of matplotlib you cannot reuse the same ticker and formatter for dates on several axes. 从matplotlib的当前版本开始,您无法在多个轴上为日期重复使用相同的代码和格式化程序。 So you need one locator and one formatter per axes . 因此, 每个轴需要一个定位器和一个格式化程序。

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime


def fmt_xaxes(ax):
    h24 = mdates.HourLocator(interval=24)
    fmt = mdates.DateFormatter('%d-%m %H:%M')
    ax.xaxis.set_major_locator(h24)
    ax.xaxis.set_major_formatter(fmt)


start1 = datetime(year=2016, month=7, day=7, hour=0)
end1   = datetime(year=2016, month=7, day=9, hour=0)

start2 = datetime(year=2016, month=9, day=30, hour=0)
end2   = datetime(year=2016, month=10, day=2, hour=0)

start3 = datetime(year=2016, month=5, day=8,  hour=0)
end3   = datetime(year=2016, month=5, day=10, hour=0)


fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(9,3))


ax1.set_xlim(start1, end1)
fmt_xaxes(ax1)

ax2.set_xlim(start2, end2)
fmt_xaxes(ax2)

ax3.set_xlim(start3, end3)
fmt_xaxes(ax3)

plt.tight_layout()
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM