利用python绘制数据曲线图的实现

”在举国上下万众一心、众志成城做好新冠肺炎疫情防控工作的特殊时刻,我们不能亲临主战场,但我们能坚持在大战中坚定信心、不负韶华。“

1、爬取新闻保存为json文件,并将绘图所需数据保存至数据库

数据库表结构:

利用python绘制数据曲线图的实现_第1张图片

代码部分:

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:#将所得的数据存储为json文件
    json.dump(data,fp = fp,ensure_ascii = False,indent = 4,sort_keys=True)
  #dump函数有很多参数,第一个是目标object,第二个是要写入的文件对象
  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(("成功将{}数据存入数据库!").format(Today))
  db.close()
cc=pachong()
connectMysql(cc)

json文件:

利用python绘制数据曲线图的实现_第2张图片

2、利用matplotlib库函数绘制图表

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)#设置分辨率为80像素/每英寸
x=np.arange(Days)
#创建两个子图
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()

到此这篇关于利用python绘制数据曲线图的实现的文章就介绍到这了,更多相关python 数据曲线图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(利用python绘制数据曲线图的实现)