gpt4 book ai didi

python - 不可见的主要网格线隐藏可见的次要网格线

转载 作者:行者123 更新时间:2023-12-05 04:26:42 25 4
gpt4 key购买 nike

我正在尝试根据日期绘制一些数据,但遇到了有关网格的问题。我想要在 x 轴上显示日期,月份的主要刻度和周的次要刻度。但是对于网格,我只想看到次要的。在构建绘图时,我注意到 11 月的网格缺失了一部分:

enter image description here

这是重现上述问题的最小示例。

from datetime import datetime

import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import dates as mdates


fig, ax = plt.subplots()

ax.xaxis.set_minor_locator(mdates.WeekdayLocator(byweekday=mdates.MO))
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(
mdates.ConciseDateFormatter(mdates.AutoDateLocator())
)

ax.set_xlim(datetime(2021, 9, 1), datetime(2022, 1, 1))

ax.xaxis.grid(color="gray", linestyle="dashed", alpha=0.2, which="minor")

plt.show()

我认为这是因为不可见的主线与应该绘制的副线重叠。任何人都可以解释发生了什么以及如何解决这个问题吗?

最佳答案

原因是次要刻度线显示在次要刻度线位置,如果次要刻度线与主要刻度线位置重合,则次要刻度线被抑制。

此行为可以通过 remove_overlapping_locs 控制.

设置

ax.xaxis.remove_overlapping_locs = False

结果

enter image description here

关于python - 不可见的主要网格线隐藏可见的次要网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72976310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com