在 Matplotlib 中绘制带有月份名称(例如一月)的时间序列并在下方显示年份

问题描述 投票:0回答:2

我目前正在使用以下数据绘制时间散点图(您可以使用这些数据来重现我的绘图)。在 x 轴上绘制的数据是时间,特别是

datetime.datetime
对象 (
tp_pass
),而在 y 轴上绘制的数据是 -180 到 180 之间的角度 (
azip_pass
)。而且,他们都是
numpy.array

tp_pass=np.array([datetime.datetime(2019, 10, 29, 1, 4, 43),
        datetime.datetime(2019, 10, 31, 1, 11, 19),
        datetime.datetime(2019, 11, 20, 8, 26, 7),
        datetime.datetime(2019, 11, 20, 23, 50, 43),
        datetime.datetime(2019, 12, 10, 17, 5, 2),
        datetime.datetime(2020, 1, 2, 18, 23, 53),
        datetime.datetime(2020, 2, 13, 10, 33, 44),
        datetime.datetime(2020, 2, 20, 18, 57, 36),
        datetime.datetime(2020, 3, 25, 2, 49, 20),
        datetime.datetime(2020, 4, 10, 16, 44, 56),
        datetime.datetime(2020, 4, 18, 8, 25, 37),
        datetime.datetime(2020, 4, 19, 20, 39, 5),
        datetime.datetime(2020, 5, 3, 11, 54, 24),
        datetime.datetime(2020, 5, 4, 13, 7, 48),
        datetime.datetime(2020, 5, 30, 18, 13, 47),
        datetime.datetime(2020, 6, 13, 15, 51, 24),
        datetime.datetime(2020, 6, 24, 19, 47, 44),
        datetime.datetime(2020, 7, 30, 0, 35, 56),
        datetime.datetime(2020, 8, 1, 17, 9, 1),
        datetime.datetime(2020, 8, 3, 8, 31, 10),
        datetime.datetime(2020, 8, 18, 0, 3, 48),
        datetime.datetime(2020, 9, 15, 3, 41, 28),
        datetime.datetime(2020, 9, 20, 22, 13, 15),
        datetime.datetime(2020, 10, 3, 9, 31, 31),
        datetime.datetime(2020, 11, 6, 8, 56, 38),
        datetime.datetime(2020, 11, 15, 22, 37, 43),
        datetime.datetime(2020, 12, 10, 13, 19, 58),
        datetime.datetime(2020, 12, 20, 17, 23, 22),
        datetime.datetime(2020, 12, 24, 23, 43, 41),
        datetime.datetime(2021, 1, 12, 2, 39, 43),
        datetime.datetime(2021, 2, 13, 14, 7, 50),
        datetime.datetime(2021, 3, 2, 21, 22, 46)], dtype=object)

azip_pass=np.array([168.3472527 ,  160.09844756,  175.44976695,  159.46139347,
          168.4780719 ,  165.17699028,  158.22654417,  151.02735996,
          159.39235045,  164.8792118 ,  168.84217025,  166.09269395,
          -179.97929963,  163.3389004 ,  167.24285926,  167.08062597,
          163.71540408,  171.13687447,  163.61945117,  172.68473083,
          159.89871931,  166.72228462,  162.2774924 ,  166.13812415,
          14.7128006 ,   12.43499853,   11.86328998,   10.56097159,
          16.16589956,   12.81530251,   10.0220719 ,   4.21173499])

使用以下

Python
脚本,我生成了绘图。

import matplotlib.pyplot as plt
import numpy as np
import datetime
from matplotlib import dates
from matplotlib import rc
%config InlineBackend.print_figure_kwargs={'facecolor' : "w"}
rc('axes', edgecolor='k', linewidth="5.0")

fig, ax=plt.subplots(1, 1, figsize=(30, 10))
ax.xaxis.set_major_locator(dates.YearLocator())
ax.set_ylim(-185, 185)
ax.scatter(tp_pass, azip_pass, color="b", s=200, alpha=1.0, ec="k")
plt.xticks(fontsize=35)
plt.yticks([-180, -120, -60, 0, 60, 120, 180], ["${}^\circ$".format(x) for x in [-180, -120, -60, 0, 60, 120, 180]], fontsize=35)
plt.tight_layout()
plt.show()

绘图的

x 轴自动标记自我使用

matplotlib.dates.YearLocator()
以来的年份。事实上,我对此不太满意,还想定位年份之间的月份。但是,我希望月份以名称显示,而不是数字(例如一月、二月、三月等)。下图的x轴显示了我想要实现的内容。使用
matplotlib
可以吗?

已添加(2021-05-18)

使用

matplotlib.dates.MonthLocator()
,我能够显示月份。然而,年份数字却消失了。有没有办法使用
matplotlib
同时显示年份和月份(例如月份下方的年份)?

fig, ax=plt.subplots(1, 1, figsize=(30, 10))
ax.xaxis.set_major_locator(dates.YearLocator()) # This line does not work
ax.xaxis.set_major_locator(dates.MonthLocator(bymonthday=15))
ax.xaxis.set_major_formatter(dates.DateFormatter('%b'))
ax.set_ylim(-185, 185)
ax.scatter(tp_pass, azip_pass, color="b", s=200, alpha=1.0, ec="k")
plt.xticks(fontsize=35)
plt.yticks([-180, -120, -60, 0, 60, 120, 180], ["${}^\circ$".format(x) for x in [-180, -120, -60, 0, 60, 120, 180]], fontsize=35)
plt.tight_layout()
plt.show()

已添加(2021-05-19)

我找到了帕特里克·菲茨杰拉德(Patrick FitzGerald)对这个问题的回答如何更改 matplotlib 图的日期时间刻度标签频率?非常有帮助。这个答案不需要使用辅助 x 轴并且做了我想做的事情。

python-3.x matplotlib plot time-series
2个回答
5
投票

您可以创建第二个 x 轴,使用它仅显示年份,同时使用原始 x 轴将月份显示为单词。这是使用您的示例的方法。它看起来像这样。

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

# Using Data from OP: tp_pass and azip_pass

# Creating your plot

fig, ax=plt.subplots(1, 1, figsize=(30, 10))

ax.set_ylim(-185, 185)
ax.scatter(tp_pass, azip_pass, color="b", s=200, alpha=1.0, ec="k")

# Minor ticks every month.
fmt_month = mdates.MonthLocator()
# Minor ticks every year.
fmt_year = mdates.YearLocator()

ax.xaxis.set_minor_locator(fmt_month)
# '%b' to get the names of the month
ax.xaxis.set_minor_formatter(mdates.DateFormatter('%b'))
ax.xaxis.set_major_locator(fmt_year)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))

# fontsize for month labels
ax.tick_params(labelsize=20, which='both')
# create a second x-axis beneath the first x-axis to show the year in YYYY format
sec_xaxis = ax.secondary_xaxis(-0.1)
sec_xaxis.xaxis.set_major_locator(fmt_year)
sec_xaxis.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))

# Hide the second x-axis spines and ticks
sec_xaxis.spines['bottom'].set_visible(False)
sec_xaxis.tick_params(length=0, labelsize=35)

plt.yticks([-180, -120, -60, 0, 60, 120, 180], ["${}^\circ$".format(x) for x in [-180, -120, -60, 0, 60, 120, 180]], fontsize=35)
plt.tight_layout()
plt.show()

0
投票

我建议使用ConciseDateFormatter 如果您确实想要定位每个月,请使用自动定位器获取更多刻度:

fig, ax=plt.subplots(1, 1, figsize=(8, 4), constrained_layout=True)
plt.rcParams['date.converter'] = 'concise'
ax.xaxis.set_major_locator(mdates.AutoDateLocator(minticks=12, maxticks=20))

ax.set_ylim(-185, 185)
ax.scatter(tp_pass, azip_pass, color="b", s=200, alpha=1.0, ec="k")
plt.yticks([-180, -120, -60, 0, 60, 120, 180], 
    ["${}^\circ$".format(x) for x in [-180, -120, -60, 0, 60, 120, 180]])
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.