python简单代码画曲线图教程-利⽤python绘制数据曲线图的
实现
”在举国上下万众⼀⼼、众志成城做好新冠肺炎疫情防控⼯作的特殊时刻,我们不能亲临主战场,但我们能坚持在⼤战中坚定信⼼、不负韶华。"
1、爬取新闻保存为json⽂件,并将绘图所需数据保存⾄数据库
数据库表结构:
代码部分:
import pymysql
import re
import sys,urllib,json
from urllib import request
from datetime import datetime
import pandas as pd
w().strftime(r"%Y-%m-%d")
#Today='2020-02-14'
def pachong():
url='api.tianapi/txapi/ncov/index?key=xxx&date={}'.format(Today)
req = request.Request(url)
resp = request.urlopen(req)
content = ad().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 = t("localhost", "root", "密码", "dbname",charset='utf8' )
cursor = db.cursor()
sql="""insert into {0} (DATE,SICK,SICK_NOW,RECOVER)values('{1}','{2}','{3}','{4}')"""
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
hbase是关系型数据库#/usr/local/mysql/bin/mysql -u root -p
date=[]
cSick=[]
aSick=[]
cNowSick=[]
aNowSick=[]
cRecover=[]
aRecover=[]
db = t("localhost", "root", "密码", "trends")
sql="select * from db1 ORDER BY DATE"
cursor = db.cursor()
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"
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,line) plt.plot(x,yNowSick,color='r',label="Current number of cases",linewidth=3,line)
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()
importantly用法plt.ylabel('Number')
plt.subplot(324)
#plt.title("Trends of March")
DrawLineChart(aSick,aNowSick)devc 编译器下载
DrawBarChart(aRecover)
python代码画图案电脑登录界面图片icks(x,date,rotation=0)
plt.xlabel('Date')遮天百度百科
plt.ylabel('Number')
plt.show()
到此这篇关于利⽤python绘制数据曲线图的实现的⽂章就介绍到这了,更多相关python 数据曲线图内容请搜索脚本之家以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持脚本之家!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论