请注意,监控时间序列数据的季节性不同于绘制时间序列数据。随着时间的推移,需要将数据分解为其组件。你可以检查这个answer。然而,只是为了绘制时间序列数据而不管format of timestamps在黑暗背景下使用plt.style.use('dark_background'),它可能如下:
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('dark_background')
colors = [
'#08F7FE', # teal/cyan
'#FE53BB', # pink
'#F5D300', # yellow
'#00ff41' # matrix green
]
df = pd.DataFrame({'A': [1, 3, 9, 5, 2, 1, 1],
'B': [4, 5, 5, 7, 9, 8, 6],
'C': [7, 5, 3, 1, 5, 9, 3],
'D': [3, 6, 7, 4, 3, 2, 1],
'date':['10-10-2016', '10-10-2017', '10-10-2018', '10-10-2019', '10-10-2020', '10-10-2021', '10-10-2022']})
# make sure the time column is actually time format
df['date']=pd.to_datetime(df['date'])
# set time as the index
df.set_index('date',inplace=True)
fig, ax = plt.subplots()
df.plot(marker='o', color=colors, ax=ax)
ax.figure.autofmt_xdate(rotation=45, ha='center')
plt.legend(loc='best')
plt.show()
如果你想让它更花哨,你可以关注Time series Visualization 或Matplotlib Cyberpunk Style
为了解决以下问题:
理想情况下,这也可以创建一个数据框,其中包含年度数据列,索引为 dd/mm 日期格式。
基于此post,一旦将日期索引传递给 x 轴,您就可以使用具有所需日期格式的 import matplotlib.dates as md:
df.plot(marker='o', color=colors, ax=ax)
ax.set_xticks(df.index)
ax.figure.autofmt_xdate(rotation=45, ha='center')
####### Use the below functions #######
import matplotlib.dates as md
dtFmt = md.DateFormatter('%d-%b') # define the formatting
ax.xaxis.set_major_formatter(dtFmt) # apply the format to the desired axis
plt.legend(loc='best')
plt.show()