脚本专栏 
首页 > 脚本专栏 > 浏览文章

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

(编辑:jimmy 日期: 2024/11/18 浏览:3 次 )

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

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

数据库表结构:

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

代码部分:

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"成功保存为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、利用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 中的 type(), dtype(), astype()的区别
下一篇:python图片指定区域替换img.paste函数的使用
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap