利用python繪制數(shù)據(jù)曲線圖的實(shí)現(xiàn)
”在舉國(guó)上下萬(wàn)眾一心、眾志成城做好新冠肺炎疫情防控工作的特殊時(shí)刻,我們不能親臨主戰(zhàn)場(chǎng),但我們能堅(jiān)持在大戰(zhàn)中堅(jiān)定信心、不負(fù)韶華?!?/p>
1、爬取新聞保存為json文件,并將繪圖所需數(shù)據(jù)保存至數(shù)據(jù)庫(kù)
數(shù)據(jù)庫(kù)表結(jié)構(gòu):
代碼部分:
import pymysql import re import sys,urllib,json from urllib import request from datetime import datetime import pandas as pd Today=datetime.now().strftime(r"%Y-%m-%d") #Today='2020-02-14' def pachong(): url='http://api.tianapi.com/txapi/ncov/index?key=xxx&date={}'.format(Today) req = request.Request(url) resp = request.urlopen(req) content = resp.read().decode() data=json.loads(content) with open('/Users/zhangyuchen/Desktop/latestTrends.json','w') as fp:#將所得的數(shù)據(jù)存儲(chǔ)為json文件 json.dump(data,fp = fp,ensure_ascii = False,indent = 4,sort_keys=True) #dump函數(shù)有很多參數(shù),第一個(gè)是目標(biāo)object,第二個(gè)是要寫入的文件對(duì)象 print("成功保存為json文件!") return(re.findall(r'"confirmedCount":(.+?),"',content),re.findall(r'"currentConfirmedCount":(.+?),"',content),re.findall(r'"curedCount":(.+?),"',content)) def connectMysql(cc): #/usr/local/mysql/bin/mysql -u root -p db = pymysql.connect("localhost", "root", "密碼", "dbname",charset='utf8' ) cursor = db.cursor() sql="""insert into {0} (DATE,SICK,SICK_NOW,RECOVER)values('{1}','{2}','{3}','{4}')""" cursor.execute(sql.format('db1',Today,int(cc[0][0]),int(cc[1][0]),int(cc[2][0]))) cursor.execute(sql.format('db2',Today,int(cc[0][1]),int(cc[1][1]),int(cc[2][1]))) db.commit() print(("成功將{}數(shù)據(jù)存入數(shù)據(jù)庫(kù)!").format(Today)) db.close() cc=pachong() connectMysql(cc)
json文件:
2、利用matplotlib庫(kù)函數(shù)繪制圖表
import numpy as np import matplotlib.pyplot as plt import matplotlib import pymysql import re import sys, urllib,json from urllib import request #/usr/local/mysql/bin/mysql -u root -p date=[] cSick=[] aSick=[] cNowSick=[] aNowSick=[] cRecover=[] aRecover=[] db = pymysql.connect("localhost", "root", "密碼", "trends") sql="select * from db1 ORDER BY DATE" cursor = db.cursor() cursor.execute(sql) results = cursor.fetchall() while results: for row in results: date.append(row[0].strftime("%d")) cSick.append(row[1]) cNowSick.append(row[2]) cRecover.append(row[3]) results=cursor.fetchone() #查詢Abroad Table sql="select * from db2" cursor.execute(sql) results = cursor.fetchall() while results: for row in results: aSick.append(row[1]) aNowSick.append(row[2]) aRecover.append(row[3]) results=cursor.fetchone() cursor.close() db.close() def DrawLineChart(ySick,yNowSick): plt.plot(x,ySick,color='y',label="Cumulative number of cases",linewidth=3,linestyle="--") plt.plot(x,yNowSick,color='r',label="Current number of cases",linewidth=3,linestyle="-") def DrawBarChart(yRecover): width=0.45#柱子寬度 p2 = plt.bar(x,yRecover,width,label="Cured Count",color="#87CEFA") Days=len(aSick) plt.figure(figsize=(16,12), dpi=80)#設(shè)置分辨率為80像素/每英寸 x=np.arange(Days) #創(chuàng)建兩個(gè)子圖 plt.subplot(322) plt.title("Trends of March") DrawLineChart(cSick,cNowSick) DrawBarChart(cRecover) plt.figlegend() plt.xticks(x,date) plt.ylabel('Number') plt.subplot(324) #plt.title("Trends of March") DrawLineChart(aSick,aNowSick) DrawBarChart(aRecover) plt.xticks(x,date,rotation=0) plt.xlabel('Date') plt.ylabel('Number') plt.show()
到此這篇關(guān)于利用python繪制數(shù)據(jù)曲線圖的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python 數(shù)據(jù)曲線圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中socket網(wǎng)絡(luò)通信是干嘛的
在本篇文章里小編給大家分享的是關(guān)于Python中socket網(wǎng)絡(luò)通信知識(shí)點(diǎn)內(nèi)容,需要的朋友們可以跟著學(xué)習(xí)下。2020-05-05Python深度學(xué)習(xí)線性代數(shù)示例詳解
這篇文章主要為大家講解了Python深度學(xué)習(xí)中線性代數(shù)的示例詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10Python Web開(kāi)發(fā)模板引擎優(yōu)缺點(diǎn)總結(jié)
這篇文章主要介紹了Python Web開(kāi)發(fā)模板引擎優(yōu)缺點(diǎn)總結(jié),需要的朋友可以參考下2014-05-05Python+Pygame實(shí)戰(zhàn)之吃豆豆游戲的實(shí)現(xiàn)
這篇文章主要為大家介紹了如何利用Python中的Pygame模塊實(shí)現(xiàn)仿吃豆豆游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python游戲開(kāi)發(fā)有一定幫助,需要的可以參考一下2022-06-06親手教你用Python打造一款摸魚(yú)倒計(jì)時(shí)界面
前段時(shí)間在微博看到一段摸魚(yú)人的倒計(jì)時(shí)模板,感覺(jué)很有意思,于是我用了一個(gè)小時(shí)的時(shí)間寫一個(gè)頁(yè)面出來(lái),下面小編把實(shí)現(xiàn)過(guò)程分享給大家,對(duì)Python摸魚(yú)倒計(jì)時(shí)界面感興趣的朋友一起看看吧2021-12-12python scipy.spatial.distance 距離計(jì)算函數(shù) ?
本文主要介紹了python scipy.spatial.distance 距離計(jì)算函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03python使用pyecharts庫(kù)畫地圖數(shù)據(jù)可視化的實(shí)現(xiàn)
這篇文章主要介紹了python使用pyecharts庫(kù)畫地圖數(shù)據(jù)可視化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03