[英]visualization with pandas in python
I have the following problem
我有以下问题
I want to plot following table:我想 plot 下表:
I want to compare the new_cases from germany and france per week how can i visualise this?我想每周比较来自德国和法国的 new_cases,我该如何可视化? I already tried multiple plots but I'm not happy with the results:
我已经尝试了多个情节,但我对结果不满意:
for example:例如:
pivot_df['France'].plot(kind='bar')
plt.figure(figsize=(15,5))
pivot_df['France'].plot(kind='bar')
plt.figure(figsize=(15,5))`
But it only shows me france但它只向我展示了法国
I think you're trying to get a timeseries plot.我认为您正在尝试获取时间序列 plot。 For that you'll need to convert
year_week
to a datetime object.为此,您需要将
year_week
转换为日期时间 object。 Subsequently you can groupby
the country, and unstack and plot the timeseries:随后您可以按国家分组,并取消堆叠和
groupby
时间序列:
import datetime
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('https://opendata.ecdc.europa.eu/covid19/testing/csv/data.csv')
df = df[df['country'].isin(['France', 'Germany'])]
df = df[df['level'] == 'national'].reset_index()
df['datetime'] = df['year_week'].apply(lambda x: datetime.datetime.strptime(x + '-1', '%G-W%V-%u')) #https://stackoverflow.com/a/54033252/11380795
df.set_index('datetime', inplace=True)
grouped_df = df.groupby('country').resample('1W').sum()['new_cases']
ax = grouped_df.unstack().T.plot(figsize=(10,5))
ax.ticklabel_format(style='plain', axis='y')
result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.