如何在 Matplotlib 中设置月底的刻度线
How do I set the tick marks for the end of the month in Matplotlib
我有一个每个月末的日期列表,我希望绘图的网格线和标签与这些日期一致。目前我只能在月初绘制图表。有没有办法强制 Matplotlib 在其图表中使用月末值?
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib.dates import MonthLocator
from matplotlib.dates import DateFormatter
dts = [datetime(2020, 11, 30), datetime(2020, 12, 31),
datetime(2021, 1, 31), datetime(2021, 2, 28),
datetime(2021, 3, 31), datetime(2021, 4, 30),
datetime(2021, 5, 31), datetime(2021, 6, 30),
datetime(2021, 7, 31), datetime(2021, 8, 31),
datetime(2021, 9, 30), datetime(2021, 10, 31),
datetime(2021, 11, 30), datetime(2021, 12, 31),
datetime(2022, 1, 31), datetime(2022, 2, 28),]
vals = [2000, 2500, 3000, 4000, 4500, 4000, 3000, 2500, 4000, 4500, 3000,
2500, 6000, 4000, 3000, 5000]
figsize=(3, 6)
fig, ax = plt.subplots(figsize=figsize)
ax.xaxis.set_major_locator(MonthLocator((3, 6, 9, 12)))
fmt = DateFormatter("%Y-%m")
ax.xaxis.set_major_formatter(fmt)
ax.plot(dts, vals, color = 'blue')
ax.grid(True)
plt.show()
一个选项是手动或动态 set_ticks()
与:
ax.set_xticks([datetime(2020, 12, 31), datetime(2021, 3, 31), datetime(2021, 6, 30), datetime(2021, 9, 30), datetime(2021, 12, 31)])
或更好:
ax.set_xticks(dts[1::3]) # this uses every third element starting with second element
完整代码:
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib.dates import MonthLocator
from matplotlib.dates import DateFormatter
dts = [datetime(2020, 11, 30), datetime(2020, 12, 31),
datetime(2021, 1, 31), datetime(2021, 2, 28),
datetime(2021, 3, 31), datetime(2021, 4, 30),
datetime(2021, 5, 31), datetime(2021, 6, 30),
datetime(2021, 7, 31), datetime(2021, 8, 31),
datetime(2021, 9, 30), datetime(2021, 10, 31),
datetime(2021, 11, 30), datetime(2021, 12, 31),
datetime(2022, 1, 31), datetime(2022, 2, 28),]
vals = [2000, 2500, 3000, 4000, 4500, 4000, 3000, 2500, 4000, 4500, 3000,
2500, 6000, 4000, 3000, 5000]
figsize=(12, 6)
fig, ax = plt.subplots(figsize=figsize)
fmt = DateFormatter("%Y-%m-%d")
ax.xaxis.set_major_formatter(fmt)
ax.set_xticks([datetime(2020, 12, 31), datetime(2021, 3, 31), datetime(2021, 6, 30), datetime(2021, 9, 30), datetime(2021, 12, 31)])
ax.set_xticks(dts[1::3])
ax.plot(dts, vals, color = 'blue')
ax.grid(True)
plt.show()
我有一个每个月末的日期列表,我希望绘图的网格线和标签与这些日期一致。目前我只能在月初绘制图表。有没有办法强制 Matplotlib 在其图表中使用月末值?
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib.dates import MonthLocator
from matplotlib.dates import DateFormatter
dts = [datetime(2020, 11, 30), datetime(2020, 12, 31),
datetime(2021, 1, 31), datetime(2021, 2, 28),
datetime(2021, 3, 31), datetime(2021, 4, 30),
datetime(2021, 5, 31), datetime(2021, 6, 30),
datetime(2021, 7, 31), datetime(2021, 8, 31),
datetime(2021, 9, 30), datetime(2021, 10, 31),
datetime(2021, 11, 30), datetime(2021, 12, 31),
datetime(2022, 1, 31), datetime(2022, 2, 28),]
vals = [2000, 2500, 3000, 4000, 4500, 4000, 3000, 2500, 4000, 4500, 3000,
2500, 6000, 4000, 3000, 5000]
figsize=(3, 6)
fig, ax = plt.subplots(figsize=figsize)
ax.xaxis.set_major_locator(MonthLocator((3, 6, 9, 12)))
fmt = DateFormatter("%Y-%m")
ax.xaxis.set_major_formatter(fmt)
ax.plot(dts, vals, color = 'blue')
ax.grid(True)
plt.show()
一个选项是手动或动态 set_ticks()
与:
ax.set_xticks([datetime(2020, 12, 31), datetime(2021, 3, 31), datetime(2021, 6, 30), datetime(2021, 9, 30), datetime(2021, 12, 31)])
或更好:
ax.set_xticks(dts[1::3]) # this uses every third element starting with second element
完整代码:
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib.dates import MonthLocator
from matplotlib.dates import DateFormatter
dts = [datetime(2020, 11, 30), datetime(2020, 12, 31),
datetime(2021, 1, 31), datetime(2021, 2, 28),
datetime(2021, 3, 31), datetime(2021, 4, 30),
datetime(2021, 5, 31), datetime(2021, 6, 30),
datetime(2021, 7, 31), datetime(2021, 8, 31),
datetime(2021, 9, 30), datetime(2021, 10, 31),
datetime(2021, 11, 30), datetime(2021, 12, 31),
datetime(2022, 1, 31), datetime(2022, 2, 28),]
vals = [2000, 2500, 3000, 4000, 4500, 4000, 3000, 2500, 4000, 4500, 3000,
2500, 6000, 4000, 3000, 5000]
figsize=(12, 6)
fig, ax = plt.subplots(figsize=figsize)
fmt = DateFormatter("%Y-%m-%d")
ax.xaxis.set_major_formatter(fmt)
ax.set_xticks([datetime(2020, 12, 31), datetime(2021, 3, 31), datetime(2021, 6, 30), datetime(2021, 9, 30), datetime(2021, 12, 31)])
ax.set_xticks(dts[1::3])
ax.plot(dts, vals, color = 'blue')
ax.grid(True)
plt.show()