python绘图pyecharts+pandas的使用详解

时间:2021-08-03 04:03:23

pyecharts介绍

pyecharts 是一个用于生成 echarts 图表的类库。echarts 是百度开源的一个数据可视化 js 库。用 echarts 生成的图可视化效果非常棒

为避免绘制缺漏,建议全部安装

为了避免下载缓慢,作者全部使用镜像源下载过了

?
1
2
3
4
5
6
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-countries-pypkg
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-china-provinces-pypkg
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-china-cities-pypkg
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-china-counties-pypkg
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-china-misc-pypkg
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ echarts-united-kingdom-pypkg

python绘图pyecharts+pandas的使用详解

基础案例

?
1
2
3
4
5
6
7
8
9
from pyecharts.charts import bar
bar = bar()
bar.add_xaxis(['小嘉','小琪','大嘉琪','小嘉琪'])
bar.add_yaxis('得票数',[60,60,70,100])
#render会生成本地html文件,默认在当前目录生成render.html
# bar.render()
#可以传入路径参数,如 bar.render("mycharts.html")
#可以将图形在jupyter中输出,如 bar.render_notebook()
bar.render_notebook()

python绘图pyecharts+pandas的使用详解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from pyecharts.charts import bar
from pyecharts import options as opts
 
# 示例数据
cate = ['apple', 'huawei', 'xiaomi', 'oppo', 'vivo', 'meizu']
data1 = [123, 153, 89, 107, 98, 23]
data2 = [56, 77, 93, 68, 45, 67]
 
# 1.x版本支持链式调用
bar = (bar()
    .add_xaxis(cate)
    .add_yaxis('渠道', data1)
    .add_yaxis('门店', data2)
    .set_global_opts(title_opts=opts.titleopts(title="示例", subtitle="副标"))
   )
bar.render_notebook()

python绘图pyecharts+pandas的使用详解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from pyecharts.charts import pie
from pyecharts import options as opts
 
# 示例数据
cate = ['apple', 'huawei', 'xiaomi', 'oppo', 'vivo', 'meizu']
data = [153, 124, 107, 99, 89, 46]
 
pie = (pie()
    .add('', [list(z) for z in zip(cate, data)],
      radius=["30%", "75%"],
      rosetype="radius")
    .set_global_opts(title_opts=opts.titleopts(title="pie-基本示例", subtitle="我是副标题"))
    .set_series_opts(label_opts=opts.labelopts(formatter="{b}: {d}%"))
   )
 
pie.render_notebook()

python绘图pyecharts+pandas的使用详解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from pyecharts.charts import line
from pyecharts import options as opts
 
# 示例数据
cate = ['apple', 'huawei', 'xiaomi', 'oppo', 'vivo', 'meizu']
data1 = [123, 153, 89, 107, 98, 23]
data2 = [56, 77, 93, 68, 45, 67]
 
"""
折线图示例:
1. is_smooth 折线 or 平滑
2. markline_opts 标记线 or 标记点
"""
line = (line()
    .add_xaxis(cate)
    .add_yaxis('电商渠道', data1,
         markline_opts=opts.marklineopts(data=[opts.marklineitem(type_="average")]))
    .add_yaxis('门店', data2,
         is_smooth=true,
         markpoint_opts=opts.markpointopts(data=[opts.markpointitem(name="自定义标记点",
                                       coord=[cate[2], data2[2]], value=data2[2])]))
    .set_global_opts(title_opts=opts.titleopts(title="line-基本示例", subtitle="我是副标题"))
   )
 
line.render_notebook()

python绘图pyecharts+pandas的使用详解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from pyecharts import options as opts
from pyecharts.charts import geo
from pyecharts.globals import charttype
import random
 
province = ['福州市', '莆田市', '泉州市', '厦门市', '漳州市', '龙岩市', '三明市', '南平']
data = [(i, random.randint(200, 550)) for i in province]
 
geo = (geo()
    .add_schema(maptype="福建")
    .add("门店数", data,
      type_=charttype.heatmap)
    .set_series_opts(label_opts=opts.labelopts(is_show=false))
    .set_global_opts(
      visualmap_opts=opts.visualmapopts(),
      legend_opts=opts.legendopts(is_show=false),
      title_opts=opts.titleopts(title="福建热力地图"))
   )
 
geo.render_notebook()

python绘图pyecharts+pandas的使用详解

python绘图pyecharts+pandas的使用详解

啊哈这个还访问不了哈

importerror: missing optional dependency ‘xlrd'. install xlrd >= 1.0.0 for excel support use pip or conda to install xlrd.

python绘图pyecharts+pandas的使用详解

python绘图pyecharts+pandas的使用详解

20200822pyecharts+pandas 初步学习

作者今天学习做数据分析,有错误请指出
下面贴出源代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 获取数据
import requests
import json
china_url = 'https://view.inews.qq.com/g2/getonsinfo?name=disease_h5'
#foreign_url = 'https://view.inews.qq.com/g2/getonsinfo?name=disease_foreign'
headers = {
  'user-agent': 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/84.0.4147.125 safari/537.36 edg/84.0.522.59',
  'referer': 'https://news.qq.com/zt2020/page/feiyan.htm'
}
#获取json数据
response = requests.get(url=china_url,headers=headers).json()
 
print(response)
#先将json数据转 python的字典
data = json.loads(response['data'])
 
#保存数据 这里使用encoding='utf-8' 是因为作者想在jupyter上面看
with open('./国内疫情.json','w',encoding='utf-8') as f:
  #再将python的字典转json数据
  # json默认中文以ascii码显示 在这里我们以中文显示 所以false
  #indent=2:开头空格2
 
  f.write(json.dumps(data,ensure_ascii=false,indent=2))

转换为json格式输出的文件

python绘图pyecharts+pandas的使用详解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 将json数据转存到excel中
import pandas as pd
#读取文件
with open('./国内疫情.json',encoding='utf-8') as f:
  data = f.read()
  
#将数据转为python数据格式
data = json.loads(data)
type(data)#字典类型
lastupdatetime = data['lastupdatetime']
#获取中国所有数据
chinaareadict = data['areatree'][0]
#获取省级数据
provincelist = chinaareadict['children']
# 获取的数据有几个省市和地区
print('数据共有:',len(provincelist),'省市和地区')
#将中国数据按城市封装,例如【{湖北,武汉},{湖北,襄阳}】,为了方便放在dataframe中
china_citylist = []
for x in range(len(provincelist)):
  # 每一个省份的数据
  province =provincelist[x]['name']
  #有多少个市
  province_list = provincelist[x]['children']
  
  for y in range(len(province_list)):
    # 每一个市的数据
    city = province_list[y]['name']
    # 累积所有的数据
    total = province_list[y]['total']
    # 今日的数据
    today = province_list[y]['today']
    china_dict = {'省份':province,
           '城市':city,
           'total':total,
           'today':today
           }
    china_citylist.append(china_dict)
 
 
chinatotaldata = pd.dataframe(china_citylist)
nowconfirmlist=[]
confirmlist=[]
suspectlist=[]
deadlist=[]
heallist=[]
deadratelist=[]
healratelist=[]
 
# 将整体数据chinatotaldata的数据添加dataframe
for value in chinatotaldata['total'] .values.tolist():#转成列表
  confirmlist.append(value['confirm'])
  suspectlist.append(value['suspect'])
  deadlist.append(value['dead'])
  heallist.append(value['heal'])
  deadratelist.append(value['deadrate'])
  healratelist.append(value['healrate'])
  nowconfirmlist.append(value['nowconfirm'])
  
chinatotaldata['现有确诊']=nowconfirmlist 
chinatotaldata['累计确诊']=confirmlist
chinatotaldata['疑似']=suspectlist
chinatotaldata['死亡']=deadlist
chinatotaldata['治愈']=heallist
chinatotaldata['死亡率']=deadratelist
chinatotaldata['治愈率']=healratelist
 
#拆分today列
today_confirmlist=[]
today_confirmcutlist=[]
 
for value in chinatotaldata['today'].values.tolist():
  today_confirmlist.append(value['confirm'])
  today_confirmcutlist.append(value['confirmcuts'])
chinatotaldata['今日确诊']=today_confirmlist
chinatotaldata['今日死亡']=today_confirmcutlist
 
#删除total列 在原有的数据基础
chinatotaldata.drop(['total','today'],axis=1,inplace=true)
 
# 将其保存到excel中
from openpyxl import load_workbook
book = load_workbook('国内疫情.xlsx')
# 避免了数据覆盖
writer = pd.excelwriter('国内疫情.xlsx',engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title,ws) for ws in book.worksheets)
chinatotaldata.to_excel(writer,index=false)
writer.save()
writer.close()
 
chinatotaldata

python绘图pyecharts+pandas的使用详解

python绘图pyecharts+pandas的使用详解

python绘图pyecharts+pandas的使用详解

python绘图pyecharts+pandas的使用详解

作者这边还有国外的,不过没打算分享出来,大家就看看,总的来说我们国内情况还是非常良好的

python绘图pyecharts+pandas的使用详解

到此这篇关于python绘图pyecharts+pandas的使用详解的文章就介绍到这了,更多相关pyecharts pandas使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_33511315/article/details/108173301