Python 柱状图 横坐标 名字_Python爬虫实例(二)——爬取新馆疫情每日新增人数

python是世界上最美的语言。

大家好,我是Henry!

疫情以来,相信大家每天都关注着疫情的实时动态,许多网站上也post了疫情的相关资料。

b41b7f593b4f68cd63822d0f8a694e6e.png

百香园

43bcc416145db9270068680b597565ee.png

百度

各个网站都会统计每日新增,刚学了Matplotlib绘图的Henry想着,我也可以通过python爬虫得到数据,然后自己来绘制一张全国每日新增图啊!不多说,直接开干!

先上Henry的效果图!

34a91de1fc31b7e3ff4bf9d0d554709a.png
粉色才是最适合猛男的颜色!!

一、实验内容

  • 通过python爬虫获取疫情每日新增数据(湖北地区和非湖北地区)。

  • 将获得的数据用库函数Matplotlib绘制成柱形图。

二、实验过程记录

(一)爬取数据

1.首先找到记录有疫情数据的网站https://view.inews.qq.com/g2/getOnsInfo?name=disease_other

2. 安装json,requests函数包并导入。

import jsonimport requests

3.通过request.get()函数获取链接中的HTML信息,并用json.loads将其转化为字典。

url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_other"html = requests.get(url)message = json.loads(html.text)

4.通过观察,发现我们需要的每日新增人数保存在一个字典的key-value中,现在把它提取出来。

mes = json.loads(message['data'])mes_dict = mes["dailyNewAddHistory"]

5.创建四个个空列表,对提取出来的mes_dict循环操作,将日期和全国每日新增人数、湖北新增人数、非湖北地区新增人数放到四个个空列表里。

date=[]       ##保存日期country=[]     ##分别保存全国、湖北、非湖北每日新增人数hubei=[]nothubei=[]n = 0for d in mes_dict:        date.append(d['date'])        country.append(d['country'])        hubei.append(d['hubei'])        nothubei.append(d['notHubei'])        n=n+1        if n>40:            break

6.至此,我们需要的数据已经爬取到四个列表:date和country中了。

(二)通过Matplotlib进行绘图

1. 安装matplotlib.pyplot、numpy并导入

import matplotlib.pyplot as pltimport numpy as npfrom matplotlib import ticker

2. 给轴初始化,并新建一个figure指定尺寸。

x = date                           ##初始化坐标y1 = countryy2 = hubeiy3 = nothubeiplt.figure(figsize=(20, 10))         ##新建figure并指定尺寸

3.添加题目、轴和轴的标签并绘图。

plt.title("Chart of the number of newly confirmed cases per day in February 2020")  ##添加题目plt.xlabel('Date')        ##添加标签plt.ylabel('Number of newly confirmed cases')plt.bar(x, y2, facecolor = 'pink',edgecolor='white',label='Hubei')          ##绘制柱状图plt.bar(x, y3, facecolor = '#ff9999',edgecolor='white',label ='notHubei')

4.为了美观,横坐标日期每三个显示一个。并将每个点的数据标在该点上方。同时添加图例。

plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(3))    ##横坐标日期每三个显示一个for x, y in zip(x, y):         ##将每个点的数据标在该点上方    plt.text(x, y+1, y)plt.legend()         ##添加图例

5.同时对特殊点坐标进行注释。

plt.annotate(r"$add\ clinically\ diagnosed\ cases$",xy=('02.12',15153),xycoords='data',xytext=(+30,-100),textcoords='offset points',arrowprops=dict(arrowstyle="->",connectionstyle="arc3,rad=.2"))

6.打印图像

plt.show()

7.得到图像。

34a91de1fc31b7e3ff4bf9d0d554709a.png
粉色才是最适合猛男的颜色!!

三、实验总结

  • 通过python爬虫获取数据,matplotlib绘图得到了1.20-2.29湖北和非湖北地区每日新增人数柱形图。

  • 程序还有不足的地方,比如没有程序界面。

下面贴下全代码

import jsonimport requestsimport matplotlib.pyplot as pltimport numpy as npfrom matplotlib import tickerurl='https://view.inews.qq.com/g2/getOnsInfo?name=disease_other'html = requests.get(url)message = json.loads(html.text)mes = json.loads(message['data'])mes_dict = mes["dailyNewAddHistory"]date=[]country=[]hubei=[]nothubei=[]n = 0for d in mes_dict:        date.append(d['date'])        country.append(d['country'])        hubei.append(d['hubei'])        nothubei.append(d['notHubei'])        n=n+1        if n>40:            breakx = datey1 = countryy2 = hubeiy3 = nothubeiplt.figure(figsize=(20, 10))plt.title("Chart of the number of newly confirmed cases per day in February 2020")plt.xlabel('Date')plt.ylabel('Number of newly confirmed cases')plt.bar(x, y2, facecolor = 'pink',edgecolor='white',label='Hubei')plt.bar(x, y3, facecolor = '#ff9999',edgecolor='white',label ='notHubei')plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(10))plt.annotate(r"$add\ clinically\ diagnosed\ cases$",xy=('02.12',15153),xycoords='data',xytext=(+30,-100),              textcoords='offset points',arrowprops=dict(arrowstyle="->",connectionstyle="arc3,rad=.2"))for x, y in zip(x, y1):    plt.text(x, y+1, y, ha='left')plt.legend()plt.show()

这就是本期的全部内容啦!希望大家点个在看~

欢迎大家关注我的知乎账号:HenryLau

5d4585f8700ef1cf500404e52f7b2e5f.png

欢迎大家关注我的CSDN账号:HenryLau7

abe04e1e00886f8978272960d79d5ccf.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值