pyecharts官网https://pyecharts.org/
简洁的 API 设计,使用如丝滑般流畅,支持链式调用
囊括了 30+ 种常见图表,应有尽有
支持主流 Notebook 环境,Jupyter Notebook 和 JupyterLab
可轻松集成至 Flask,Django 等主流 Web 框架
高度灵活的配置项,可轻松搭配出精美的图表
详细的文档和示例,帮助开发者更快的上手项目
多达 400+ 地图文件以及原生的百度地图,为地理数据可视化提供强有力的支持
利用Bar调用add添加数据做出柱状图,render()渲染后得到render.html文件,用webbrowser显示在网页上
webbrowser.open(“file://”+ os.path.realpath(“render.html”))
from pyecharts import Bar
import webbrowser
import os
x_axis = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y_axis = [5, 20, 36, 10, 75, 90]
bar = Bar()
bar.add("",x_axis,y_axis)
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
bar.render()
webbrowser.open("file://"+ os.path.realpath("render.html"))
'''
某地区一年当中的降水量和蒸发量数据如下
蒸发量:[2.0,4.9,7.0,23.2,25.6,76.7,135.6,162.2,32.6,20.0,6.4,3.3]
降水量:v2=[2.6,5.9,9.0,26.4,28.7,70.7,175.6,182.2,48.7,18.8,6.0,2.3]
'''
from pyecharts import Bar
import webbrowser
import os
attr=["{}月".format(i) for i in range(1,13)]
v1=[2.0,4.9,7.0,23.2,25.6,76.7,135.6,162.2,32.6,20.0,6.4,3.3]
v2=[2.6,5.9,9.0,26.4,28.7,70.7,175.6,182.2,48.7,18.8,6.0,2.3]
bar=Bar("柱状图")
#mark_line为标记线
bar.add("蒸发量",attr,v1,mark_line=["average"],marl_point=["max","min"])
bar.add("降水量",attr,v2,mark_line=["average"],marl_point=["max","min"])
bar.render()
webbrowser.open("file://"+os.path.realpath("render.html"))
**常用的参数:**标签有很多,根据需要选择即可。
is_splitline_show:是否显示网格线
is_label_show:是否显示标签
label_pos:标签的位置,有’top’(默认), ‘left’, ‘right’, ‘bottom’, ‘inside’,‘outside’可选
label_text_color/size:标签字体颜色/大小
is_random:是否随机排列颜色列表
label_color:自定义标签颜色
mark_point/line:标记点/线,默认有’min’, ‘max’, ‘average’可选。可自定义标记点线,具体格式如:[{‘coord’: [x, y], ‘name’: ‘目标标记点’}],记住格式是一个列表
mark_point/line_symbol:标记点/线图形,默认为’pin’(点),有’circle’(圆形), ‘rect’(正方形), ‘roundRect’(圆角正方形), ‘triangle’(三角形), ‘diamond’(菱形), ‘pin’(点), ‘arrow’(箭头)可选
利用Line()新建折线图,每段数据需要调用一次add(),传入数据,最后用render()渲染并用webbrowser打开
#折线图
from pyecharts import Line
import webbrowser
import os
attr = ["{}月".format(i) for i in range(1,13)]
v1 = [2.0,4.9,7.0,23.2,25.6,76.7,135.6,162.2,32.6,20.0,6.4,3.3]
v2 = [2.6,5.9,9.0,26.4,28.7,70.7,175.6,182.2,48.7,18.8,6.0,2.3]
line = Line("折线图")
#mark_point设置标记线,取值为“min”,“max”,“average”
line.add("蒸发量",attr,v1,mark_line=["average"],mark_point = ["max", "min"]) #先传入x轴数据,然后y轴数据
line.add("降水量",attr,v2,mark_line=["average"],mark_point = ["max", "min"])
line.render()
webbrowser.open("file://"+os.path.realpath("render.html"))
饼状图样式定制
①Pie()中的参数:
title_pos: "left"左对齐,"right"右对齐,"center"居中对齐
②add()中的参数:
legend_pos:"left"左对齐,"rigth"右对齐,"center"居中对齐
legend_orient: "horizontal"水平布局, "vertical"垂直布局
is_label_show: "True"显示标签 , "False"不显示(默认)
rosetype: "radius"扇区圆心角展现数据的百分比,半径展现数据的大小(默认)
"area"所有扇区圆心角相同,仅通过半径展现数据大小
#饼状图
from pyecharts import Pie
import webbrowser
import os
star_num = {
"水瓶座":3,
"双鱼座":3,
"白羊座":2,
"金牛座":1,
"双子座":2,
"巨蟹座":5,
"狮子座":4,
"处女座":3,
"天秤座":4,
"天蝎座":6,
"射手座":4,
"摩羯座":3,
}
key = list(star_num.keys())
value = list(star_num.values())
pie = Pie("班级星座统计")
pie.add(
"",
key,
value,
legend_pos="left",
legend_orient = "vertical",
is_label_show = True,
)
pie.render()
webbrowser.open("file://" + os.path.realpath("render.html"))
rosetype -> str
是否展示成南丁格尔图,通过半径区分数据大小,有’radius’半径和’area’区域两种模式。
默认为’radius’
①radius:扇区圆心角展现数据的百分比,半径展现数据的大小
②area:所有扇区圆心角相同,仅通过半径展现数据大小
pie = Pie("班级星座统计")
pie.add(
"",
key,
value,
legend_pos="right",
legend_orient = "vertical",
is_label_show = True,
rosetype = "radius"
#rosetype = "area"
)
from pyecharts import WordCloud
import webbrowser,os
with open(r'匆匆.txt','r',encoding = 'utf-8')as f:
text = f.read()
print(text)
from pyecharts import WordCloud
import webbrowser,os
import jieba
with open(r'匆匆.txt','r',encoding = 'utf-8')as f:
text = f.read()
words_list = jieba.lcut(text)
words_dict = {}
drop = ['我们','你们','他们','就是','没有','自己']
for word in words_list:
#去重判断
if word in words_dict:
words_dict[word] += 1
else:
#数据清洗过滤
if len(word) > 1 and word not in drop:
#创建键值
words_dict[word] = 1
print(words_dict)
wordcloud = WordCloud(width=1440, height=900)
name = '匆匆'
words = list(words_dict.keys())
nums = list(words_dict.values())
wordcloud.add(name,words,nums)
wordcloud.render()
webbrowser.open("file://" + os.path.realpath('render.html'))
美化词云图,设置形状shape、间隔word_gap以及字体大小范围word_size_range
shape–>list
词云图轮廓,有’circle‘,’cardioid‘,’diamon‘,’triangle-forward‘,’triangle‘,’pentagon‘,‘star’可选
word_gap->int
单词间隔,默认为20
word_size_range->list
单词字体大小范围,默认为[12,60]
rotate_step->int
旋转单词角度,默认为45
wordcloud.add(name,words,nums,shape="star",word_gap=10,word_size_range=[12,100])
散点图同时展示了鸢尾花花瓣的宽度 x 和长度 y 值,而 x 与 y 值之间的关系受鸢尾花种类的影响,使得不同品种数据的坐标点分布在 各自的区域,形成聚类的效果,从而很容易在数据中划分出不同的品种。
读取相应的鸢尾花数据来做出散点图
#列表生成式
List_1 = [i**2 for i in range(1,11)]
List_2 = [i for i in 'abcde']
print(List_1,'\n',List_2)
import json
from pyecharts import Scatter
import webbrowser,os
with open('iris.json','r')as f:
data = json.load(f)
# print(type(data),data)
scatter = Scatter("鸢尾花品种散点图")
setosa_h = [i[2] for i in data['setosa']]
setosa_w = [i[3] for i in data['setosa']]
versicolor_h = [i[2] for i in data['versicolor']]
versicolor_w = [i[3] for i in data['versicolor']]
virginica_h = [i[2] for i in data['virginica']]
virginica_w = [i[3] for i in data['virginica']]
#mark_point: 标记点,这个代码中将是平均数标记出来
#xaxis_name, yaxis_name:增加横纵坐标说明
scatter.add('setosa',setosa_w,setosa_h,mark_point = ['average'])
scatter.add('versicolor',versicolor_w,versicolor_h,mark_point = ['average'])
scatter.add('virginica',virginica_w,virginica_h,mark_point = ['average'],xaxis_name = ['花瓣宽度'],yaxis_name = ['花瓣长度'])
scatter.render()
webbrowser.open("file://" + os.path.realpath('render.html'))
Echarts是一个由百度开源的数据可视化,结合巧妙的交互性,精巧的图表设计,得到了开发者的认可。而 Python 是一门富有表达力的语言,很适合用于数据处理。分析遇上数据可视化时,pyecharts诞生了。在pyecharts官网上还有很多3D图形的绘制,也是非常有意思的,大家有兴趣的可以去探索一下。