gpt4 book ai didi

python - 包含周、年和点击次数的时间序列图

转载 作者:行者123 更新时间:2023-12-05 07:31:24 26 4
gpt4 key购买 nike

 In[1]    df    
Out[0] week year number_of_cases
8 2010 583.0
9 2010 116.0
10 2010 358.0
11 2010 420.0
... ... ...
52 2010 300.0
1 2011 123.0
2 2011 145.0

如何创建一个时间轴图表,其中 y 轴是案例数,x 轴是增加与年份相对应的周数?我希望它从 2010 年的第 1 周到第 52 周,然后是 2011 年的第 1 周到第 52 周。并将其作为一张大图来查看每年的病例数如何随周变化。

Python 3, Pandas 。

最佳答案

您可以根据年份和星期创建一个datetime 列,根据日期绘制'number_of_cases',然后使用mdates格式化 x-ticks。

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

# Determine the date
df['date'] = pd.to_datetime(df.assign(day=1, month=1)[['year', 'month', 'day']])+pd.to_timedelta(df.week*7, unit='days')

# Plot
fig, ax = plt.subplots()
df.plot(x='date', y='number_of_cases', marker='o', ax=ax)

# Format the x-ticks
myFmt = mdates.DateFormatter('%Y week %U')
ax.xaxis.set_major_formatter(myFmt)

enter image description here

关于python - 包含周、年和点击次数的时间序列图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51813955/

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