程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2024-11(1)

利用python绘制数据曲线图

发布于2020-04-09 18:50     阅读(2172)     评论(0)     点赞(29)     收藏(2)


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

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

数据库表结构:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200408120741751.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzk1OTQyMQ==,size_16,color_FFFFFF,t_70
代码部分:

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文件:
在这里插入图片描述

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()

我们相信:待凛冬离去,雪融草青,一定有新的相逢将温暖延续。武汉加油!!

原文链接:https://blog.csdn.net/weixin_43959421/article/details/103981713



所属网站分类: 技术文章 > 博客

作者:飞龙出海

链接:https://www.pythonheidong.com/blog/article/319667/58855813d0bfbdc3234b/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

29 0
收藏该文
已收藏

评论内容:(最多支持255个字符)