绘制每月值总和的直方图[重复]

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

绘制确诊的 COVID 19 病例数据集的直方图,其中日期需要在 x 轴上,而确诊的 covid 19 病例在 y 轴上

我想在 x 轴上绘制月份,即 2020 年 2 月、2020 年 3 月,而 y 轴上绘制确诊的 covid 19 例,测量值为 1e6。

数据集:

[Dataset and sample output ]

我写了下面的代码,但我不知道如何准确合并日期与已确认的 covid 19 病例。


df_us["Date"] = pd.to_datetime(df_us["Date"])

df_us["Month"] = df_us["Date"].dt.to_period("M")

monthly_data = df_us.groupby(df_us["Month"])["Count"].sum()

plt.figure(figsize=(12, 6))
plt.hist(monthly_data, bins=20, color="blue", alpha=0.7, edgecolor='black')
plt.title("Histogram of Monthly Confirmed COVID-19 Cases (US)")
plt.xlabel("Month")
plt.ylabel("Confirmed Cases")
plt.grid(True)
plt.tight_layout()
plt.show()
 

python pandas matplotlib bar-chart histogram
1个回答
2
投票
  • 您正在绘制
    sum
    的每月
    'new_cases'
    的条形图。
import pandas as pd

# read the data; selected columns, parse the date column and set it as the index
df = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv',
                 usecols=['location', 'date', 'new_cases'], parse_dates=['date'],
                 date_format='%Y-%m-%d', index_col='date')

# select data based on location, and only select the new_cases column
us = df.loc[df.location.eq('United States'), 'new_cases']

# calculate the monthly sum of new_cases
us_monthly = us.groupby(pd.Grouper(freq='M')).sum()

# format the index to Year-month
us_monthly.index = us_monthly.index.strftime('%Y-%m')

# plot
ax = us_monthly.plot(kind='bar', figsize=(8, 6), xlabel='Date', ylabel='Confirmed Cases', title='Monthly Confirmed COVID-19 Cases (US)')


数据

df

  • 这是一个
    pandas.DataFrame
               location  new_cases
date                              
2020-01-03  Afghanistan        0.0
2020-01-04  Afghanistan        0.0
2020-01-05  Afghanistan        0.0
2020-01-06  Afghanistan        0.0
2020-01-07  Afghanistan        0.0
...                         
2023-07-29  Zimbabwe           NaN
2023-07-30  Zimbabwe           0.0
2023-07-31  Zimbabwe           0.0
2023-08-01  Zimbabwe           0.0
2023-08-02  Zimbabwe           0.0

us

  • 这是一个
    pandas.Series
date
2020-01-03    0.0
2020-01-04    0.0
2020-01-05    0.0
2020-01-06    0.0
2020-01-07    0.0
Name: new_cases, dtype: float64

us_monthly

  • 这是一个
    pandas.Series
date
2020-01         8.0
2020-02        61.0
2020-03    173074.0
2020-04    857163.0
2020-05    755116.0
Name: new_cases, dtype: float64
© www.soinside.com 2019 - 2024. All rights reserved.