温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用Matplotlib绘制疫情动图

发布时间:2021-12-22 09:06:28 来源:亿速云 阅读:179 作者:小新 栏目:大数据

这篇文章主要介绍了如何使用Matplotlib绘制疫情动图,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

使用 matplotlib,绘制 COVID-19 过去半年四个国家的每天死亡人数,获取数据的API接口为:

https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv

数据处理的逻辑如下,参考前几天推送的处理逻辑:

df = pd.read_csv('a.csv', delimiter=',', header='infer')
df_interest = df.loc[df['Country/Region'].isin(['United Kingdom', 'US', 'Italy', 'Germany'])& df['Province/State'].isna()]
df_interest.rename(index=lambda x: df_interest.at[x, 'Country/Region'], inplace=True)
df1 = df_interest.transpose()
df1 = df1.drop(['Province/State', 'Country/Region', 'Lat', 'Long'])
df1 = df1.loc[(df1 != 0).any(1)]
df1.index = pd.to_datetime(df1.index)

为了更方便大家理解,展示df_interest的部分数据:

如何使用Matplotlib绘制疫情动图

整理后df1的部分数据:

如何使用Matplotlib绘制疫情动图

可以看到截止昨天,美国COVID-19死亡人数已有:219286

绘制折线图动画展示的逻辑如下:

color = ['red', 'green', 'blue', 'orange']
fig = plt.figure()
plt.xticks(rotation=45, ha="right", rotation_mode="anchor")
plt.subplots_adjust(bottom = 0.2, top = 0.9)
plt.ylabel('No of Deaths')
plt.xlabel('Dates')

# 此函数是绘制动画的回调函数
# 有且仅有一个参数 i,表示帧数,表示df1的第几行
def showLine(i):
    plt.legend(df1.columns)
    p = plt.plot(df1[:i].index, df1[:i].values)
    for i in range(0,4):
        p[i].set_color(color[i])

绘制动画只有这一行,调用FuncAnimation,它的第二个参数为上面定义的函数showLine:

animator = ani.FuncAnimation(fig, showLine, interval = 10)
plt.show()

绘制后的折线图动画为:

如何使用Matplotlib绘制疫情动图

感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用Matplotlib绘制疫情动图”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI