当python 遇见 Echarts , 就产生了pyecharts,pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。用 Echarts 生成的图可视化效果非常好, 是为了与 Python 进行对接,方便在 Python 中直接使用数据生成图。
Echarts官网: https://www.echartsjs.com/zh/index.html
pyecharts官网: http://pyecharts.org/#/zh-cn/intro
操作平台: win10, python37, jupyter notebook
pyecharts版本: pyecharts.__version__
——> 0.5.11
安装指定版本: pip install pyecharts==0.5.11
from pyecharts import Bar
bar =Bar("我的第一个图表", "这里是副标题") #标题
bar.add("服装", #标签
["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],#x坐标
[5, 20, 36, 10, 75, 90]) #y坐标
# is_more_utils = True #显示右侧的工具栏
# bar.show_config() # 打印所有参数
bar.render('my_first_chart.html')#保存图列
参数 | 说明 |
---|---|
Tip | 可以按右边的下载按钮将图片下载到本地 |
data view | 可以在其中直接填充数据 |
add() | 主要方法,用于添加图表的数据和设置各种配置项 |
is_more_utils = True | 用于显示右侧的工具栏 |
show_config() | 打印输出图表的所有配置项 |
render() | 默认将会在根目录下生成一个 render.html 的文件,支持 path 参数,设置文件保存位置,如 render(r"e:my_first_chart.html"),文件用浏览器打开。 |
基本上所有的图表类型都是这样绘制的:
chart_name = Type() 初始化具体类型图表。
add() 添加数据及配置项。
render() 生成 .html 文件。
from pyecharts import Bar
attr = ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子'] #x轴
v1 = [5,20,36,10,75,90] #y轴1
v2 = [10,25,8,60,20,80] #y轴1
bar = Bar('柱状信息堆叠图')
bar.add('商家A',attr,v1,is_stack = True) #is_stack = True才表示堆叠在一起
bar.add('商家B',attr,v2,is_stack = True)
bar
from pyecharts import Bar
attr = ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
v1 = [5,20,36,10,75,90]
v2 = [10,25,8,60,20,80]
bar = Bar('标记线和标记示例','作者:阿优乐扬')
bar.add('商家A',attr,v1,mark_point = ['average']) #标记点:商家A的平均值
bar.add('商家B',attr,v2,mark_line = ['min','max']) #标记线:商家B最小/最大值
bar.render('./first04.html')
bar
Line.add()方法签名
add(
name,x_axis,y_axis,
is_symbol_show = True,
is_smooth = False,
is_stack = False,
is_step = False,
is_fill = False,**kwargs
)
以下为属性默认值:
is_symbol_show = True, #是否显示标记图形
is_smooth = False, #是否显示平滑曲线
is_stack = False, #是否数据堆叠
is_step = False, #是否是阶梯线
is_fill = False,**kwargs #是否填充曲线区域面积
(1)线型图
from pyecharts import Line
attr = ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
v1 = [5,20,36,10,75,90]
v2 = [10,25,8,60,20,80]
line = Line('折线示例图', '作者:阿优乐扬')
line.add('商家A',attr,v1,mark_point = ['average'])
line.add('商家B',attr,v2,is_smooth = True, mark_line = ['max','average'])
(2)形状
from pyecharts import Line
attr = ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
v1 = [5,20,36,10,75,90]
v2 = [10,25,10,60,20,80]
line = Line('折线示例图', '作者:阿优乐扬')
line.add('商家A',attr,v1,
mark_point = ['average','max','min'], #标注点:平均值,最大值,最小值
mark_point_symbol = 'diamond', #标注点:钻石形状
mark_point_textcolor = '#40ff27') #标注点:标注文本颜色
line.add('商家B',attr,v2,mark_point = ['average','max','min'],
mark_point_symbol = 'arrow',
mark_point_symbolsize = 40)
line
from pyecharts import Line
attr = ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
v1 = [5,20,36,10,75,90]
v2 = [10,25,8,60,20,80]
line = Line('折线面积示例图', '作者:阿优乐扬')
line.add('商家A',attr,v1,is_fill = True,
line_opacity = 0.2, #线条不透明度
area_opacity = 0.4,
symbol = None)
line.add('商家B',attr,v2,is_fill = True,
line_color = '#000', #黑色
area_opacity = 0.3, #填充不透明度
is_smooth = True)
from pyecharts import Line
attr =["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 =[5, 20, 36, 10, 10, 100]
v2 =[55, 60, 16, 20, 15, 80]
line =Line("折线图-阶梯图示例", "作者:阿优乐扬")
line.add("商家A", attr, v1, is_step=True, is_label_show=True)
line
from pyecharts import Bar,Line,Overlap
attr = ['A','B','C','D','E','F']
v1 = [10,20,30,40,50,60]
v2 = [38,28,35,58,65,70]
bar = Bar('Line - Bar示例')
bar.add('bar',attr,v1)
line = Line()
line.add('line',attr,v2)
overlop = Overlap()
overlop.add(bar)
overlop.add(line)
overlop
(2)线性+散点
#EffectScatter + Line 叠加
from pyecharts import Line,EffectScatter,Overlap
attr = ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
v1 = [5,20,36,10,10,90]
line = Line('线性_闪烁图示例')
line.add('',attr,v1,is_random = True)
es = EffectScatter()
es.add('',attr,v1,effect_scale=8) #闪烁
overlop = Overlap()
overlop.add(line) #必须先添加line,在添加es
overlop.add(es)
overlop
from pyecharts import Pie
attr = ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
v1 = [5,20,36,10,75,90]
pie = Pie('饼图示例', '作者:阿优乐扬')
pie.add('',attr,v1,is_label_show = True)
pie
from pyecharts import Pie
attr = ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
v1 = [5,20,36,10,75,90]
pie = Pie('饼图-环形图示例', title_pos = 'center')
pie.add(
'',attr,v1, #'':图例名(不使用图例)
radius = [40,75], #环形内外圆的半径
is_label_show = True, #是否显示标签
label_text_color = None, #标签颜色
legend_orient = 'vertical', #图例垂直
legend_pos = 'left'
)
pie
from pyecharts import Pie
attr =["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 =[11, 12, 13, 10, 10, 10]
v2 =[19, 21, 32, 20, 20, 33]
pie =Pie("饼图-玫瑰图示例", title_pos='center', width=900)
pie.add("商品A", attr, v1, center=[25, 50], is_random=True, radius=[30, 75], rosetype='radius')
pie.add("商品B", attr, v2, center=[75, 50], is_random=True, radius=[30, 75], rosetype='area', is_legend_show=False, is_label_show=True)
pie
from pyecharts import Pie
pie =Pie("17级信息学院专业分布","作者:阿优乐扬", title_pos='center', width=1000, height=600)
pie.add("", ['信管', '计科', '软工', '电商', '教技', '网工'], [57, 114, 56, 72, 52, 50], radius=[40, 55],is_label_show=True)
pie.add("", ['管理类', '工科', '理学'], [139, 230, 52], radius=[0, 25], legend_orient='vertical', is_label_show=True, legend_pos='left')
pie
from pyecharts import Pie
pie =Pie('各类电影中"好片"所占的比例', "数据来着豆瓣", title_pos='center')
pie.add("", ["剧情", ""], [25, 75], center=[10, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, )
pie.add("", ["奇幻", ""], [24, 76], center=[30, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, legend_pos='left')
pie.add("", ["爱情", ""], [14, 86], center=[50, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["惊悚", ""], [11, 89], center=[70, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["冒险", ""], [27, 73], center=[90, 30], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["动作", ""], [15, 85], center=[10, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["喜剧", ""], [54, 46], center=[30, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["科幻", ""], [26, 74], center=[50, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["悬疑", ""], [25, 75], center=[70, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None)
pie.add("", ["犯罪", ""], [28, 72], center=[90, 70], radius=[18, 24], label_pos='center', is_label_show=True, label_text_color=None, is_legend_show=True, legend_top="center")
pie
from pyecharts import Scatter
v1 = [5,20,35,50,65,80]
v2 = [10,20,30,40,50,60]
scatter = Scatter('散点示例图','作者:阿优乐扬')
scatter.add('A',v1,v2)
scatter.add('B',v1[::-1],v2) #v1[::-1]代表切片倒序
scatter
from pyecharts import Scatter
v1 = [5,20,35,50,65,80]
v2 = [10,20,30,40,50,60]
scatter = Scatter('散点-气泡示例图')
scatter.add('A',v1,v2)
scatter.add('B',v1[::-1],v2, #v1[::-1]代表切片倒序
is_visualmap = True, #显示滑动条
symbol_size = 30, #显示图内标点大小
vasual_range_size = [20,80]) #显示滑动范围
scatter
from pyecharts import EffectScatter
v1 =[10, 20, 30, 40, 50, 60]
v2 =[25, 20, 15, 10, 60, 33]
es =EffectScatter("动态散点图示例")
es.add("effectScatter", v1, v2)
es.render()
es =EffectScatter("动态散点图各种图形示例", '作者:阿优乐扬')
es.add("", [10], [10], symbol_size=20, effect_scale=3.5, effect_period=3, symbol="pin")
es.add("", [20], [20], symbol_size=12, effect_scale=4.5, effect_period=4,symbol="rect")
es.add("", [30], [30], symbol_size=30, effect_scale=5.5, effect_period=5,symbol="roundRect")
es.add("", [40], [40], symbol_size=10, effect_scale=6.5, effect_brushtype='fill',symbol="diamond")
es.add("", [50], [50], symbol_size=16, effect_scale=5.5, effect_period=3,symbol="arrow")
es.add("", [60], [60], symbol_size=6, effect_scale=2.5, effect_period=3,symbol="triangle")
es
from pyecharts import Funnel
attr =["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
value =[20, 40, 60, 80, 100, 120]
funnel =Funnel("漏斗图示例")
funnel.add("商品", attr, value, is_label_show=True, label_pos="inside", label_text_color="#fff")
funnel
from pyecharts import Gauge
gauge = Gauge("仪表盘示例","作者:阿优乐扬")
gauge.add("业务指标", "完成率", 66.66)
gauge
(1)示例1
from pyecharts import Liquid
liquid =Liquid("水球图示例", "作者:阿优乐扬")
liquid.add("Liquid", [0.5])
liquid
from pyecharts import Liquid
liquid =Liquid("水球图示例")
liquid.add("Liquid", [0.6, 0.5, 0.4, 0.3], is_liquid_outline_show=False)
liquid
from pyecharts import Liquid
liquid =Liquid("水球图示例","作者:阿优乐扬")
liquid.add("Liquid", [0.6, 0.5, 0.4, 0.3], is_liquid_animation=False, shape='diamond')
liquid
from pyecharts import Graph
nodes =[{"name": "结点1", "symbolSize": 10},
{"name": "结点2", "symbolSize": 20},
{"name": "结点3", "symbolSize": 25},
{"name": "结点4", "symbolSize": 32},
{"name": "结点5", "symbolSize": 35},
{"name": "结点6", "symbolSize": 16},
{"name": "结点7", "symbolSize": 24},
{"name": "结点8", "symbolSize": 20}]
links =[]
for i in nodes:
for j in nodes:
links.append({"source": i.get('name'), "target": j.get('name')})
graph =Graph("关系图-环形布局示例")
graph.add("", nodes, links, is_label_show=True, repulsion=8000, layout='circular', label_text_color=None)
graph
from pyecharts import Polar
# 极坐标
radius =['周一', '周二', '周三', '周四', '周五', '周六', '周日']
polar =Polar("极坐标系-堆叠柱状图示例", width=1200, height=600)
polar.add("A", [1, 2, 3, 4, 3, 5, 1], radius_data=radius, type='barRadius', is_stack=True)
polar.add("B", [2, 4, 6, 1, 2, 3, 1], radius_data=radius, type='barRadius', is_stack=True)
polar.add("C", [1, 2, 3, 4, 1, 2, 5], radius_data=radius, type='barRadius', is_stack=True)
polar
from pyecharts import Radar
schema =[ ("销售", 6500),
("管理", 16000),
("信息技术", 30000),
("客服", 38000),
("研发", 52000),
("市场", 25000)]
v1 =[[4300, 10000, 28000, 35000, 50000, 19000]]
v2 =[[5000, 14000, 28000, 31000, 42000, 21000]]
radar =Radar()
radar.config(schema)
radar.add("预算分配", v1, is_splitline=True, is_axisline_show=True)
radar.add("实际开销", v2, label_color=["#4e79a7"], is_area_show=False)
radar
from pyecharts import Kline
v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38],
[2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8],
[2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82],
[2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38],
[2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73],
[2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03],
[2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07],
[2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82],
[2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78],
[2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63],
[2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65],
[2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14],
[2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02],
[2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96],
[2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33],
[2255.77, 2270.28, 2253.31, 2276.22]]
kline = Kline("K 线图示例")
kline.add("k线图", ["2020/03/{}".format(i + 1) for i in range(30)], v1)
kline
from pyecharts import Parallel
c_schema =[{"dim": 0, "name": "data"},
{"dim": 1, "name": "AQI"},
{"dim": 2, "name": "PM2.5"},
{"dim": 3, "name": "PM10"},
{"dim": 4, "name": "CO"},
{"dim": 5, "name": "NO2"},
{"dim": 6, "name": "CO2"},
{"dim": 7, "name": "等级",
"type": "category",
"data": ['优', '良', '轻度污染', '中度污染', '重度污染', '严重污染']}]
data =[[1, 91, 45, 125, 0.82, 34, 23, "良"],
[2, 65, 27, 78, 0.86, 45, 29, "良"],
[3, 83, 60, 84, 1.09, 73, 27, "良"],
[4, 109, 81, 121, 1.28, 68, 51, "轻度污染"],
[5, 106, 77, 114, 1.07, 55, 51, "轻度污染"],
[6, 109, 81, 121, 1.28, 68, 51, "轻度污染"],
[7, 106, 77, 114, 1.07, 55, 51, "轻度污染"],
[8, 89, 65, 78, 0.86, 51, 26, "良"],
[9, 53, 33, 47, 0.64, 50, 17, "良"],
[10, 80, 55, 80, 1.01, 75, 24, "良"],
[11, 117, 81, 124, 1.03, 45, 24, "轻度污染"],
[12, 99, 71, 142, 1.1, 62, 42, "良"],
[13, 95, 69, 130, 1.28, 74, 50, "良"],
[14, 116, 87, 131, 1.47, 84, 40, "轻度污染"]]
parallel =Parallel("平行坐标系-用户自定义指示器")
parallel.config(c_schema=c_schema)
parallel.add("parallel", data)
parallel
#qq个性
from pyecharts import WordCloud
name = [
'学生','技术宅','有为青年','IT民工','乐活族',
'阳光','执着','90后','单身','上进'
]
value = [10000,6189,4556,2356,2233,
542,462,361,265,125]
worldcloud = WordCloud(width = 1300,height = 620)
worldcloud.add('', name,value,word_size_range = [20,100])
worldcloud
Map.add() 方法签名
add(
name,attr,value,
maptype = 'china',
is_roam = True,
is_map_symobol_show = True
**kwargs
)
maptype -> str: 地图类型,支持China,world,北京,天津,上海,湖南,湖北,……363个二线城市
is_roam -> bool/str 是否开启鼠标缩放,漫游等,默认 True,
若只想开启缩放/平移 设置scale/move 设置成 True 开启
is
is_map_symobol_show 是否显示地图标记,默认 True。
(1)入门学习
from pyecharts import Map
value = [240, 155, 10, 66, 78]
attr = ['贵州', '福建','山东','北京','上海']
map = Map('全国地图示例',width = 1200,height = 600)
map.add('',attr,value,maptype = 'china')
map.render('./map01.html') #保存到本地
map
(2)高级实战数值方法
import numpy as np #导入numpy库
from pyecharts import Map #从pyecharts库导入Map函数
attr=['北京','上海','天津','重庆','辽宁','吉林','黑龙江','河北','河南','山东','山西','江苏','安徽','江西','浙江','福建','广东','海南','贵州','云南','四川','湖南','湖北','陕西','甘肃','青海','内蒙古','新疆','广西','宁夏','西藏','香港','澳门','台湾']
v1=np.random.randint(100,2000,34)#随机生成销量
map=Map("XX家电各省市销售分布图(单位:台)",title_color="#fff", title_pos="center", width=1000,height=600, background_color='#404a59')
map.add("", attr, v1,visual_range=[100, 2000],visual_text_color="#fff",symbol_size=15, is_visualmap=True,is_label_show=True,is_more_utils=True)#添加数据及配置项
map.render('XX家电各省市销售分布图(单位:台).html') #生成本地html
map
(2)高级实战字典方法
from pyecharts import Map, Geo
# 省和直辖市
province_distribution = {'贵州': 55,
'北京': 35.5,
'河北': 23,
'辽宁': 12,
'江西': 6,
'上海': 20,
'安徽': 10,
'江苏': 16,
'湖南': 9,
'浙江': 13,
'海南': 2,
'广东': 22,
'湖北': 8,
'黑龙江': 11,
'澳门': 1,
'陕西': 11,
'四川': 7,
'内蒙古': 3,
'重庆': 3,
'云南': 6,
'河南': 20,
'吉林': 3,
'山西': 12,
'山东': 11,
'福建': 4,
'青海': 1,
'阿优乐扬': 1,
'天津': 1,
'其他': 61}
provice=list(province_distribution.keys())
values=list(province_distribution.values())
# maptype='china' 只显示全国直辖市和省级
# 数据只能是省名和直辖市的名称
map = Map("中国地图",'作者:阿优乐扬', width=1200, height=600)
map.add("", provice, values, visual_range=[0, 50], maptype='china', is_visualmap=True,
visual_text_color='#000')
map
from pyecharts import Map
value = [155,88,66,78, 66, 55]
attr = ['贵阳市', '遵义市','六盘水市','毕节市', '铜仁市','黔西南布依族苗族自治州']
map = Map('贵州地图示例',width = 1200,height = 600)
map.add('',attr,value,maptype = '贵州',
is_visualmap = True,
visual_text_color = '#000',
is_label_show = True
)
map.render('./map02.html')
map
from pyecharts import Map, Geo
# 区县 -- 具体城市内的区县 xx县
quxian = ['兴义市', '贞丰县', '兴仁县', '册亨县', '安龙县', '普安县', '晴隆县', '望谟县']
values3 = [5, 9, 7, 8, 2, 4, 1, 3]
# 贵州黔西南布依族苗族自治州的区县
map3 = Map("黔西南布依族苗族自治州",'作者:阿优乐扬', width=1200, height=600)
map3.add("贵州黔西南布依族苗族自治州县级地图", quxian, values3, visual_range=[1, 10], maptype='黔西南布依族苗族自治州', is_visualmap=True,
visual_text_color='#000')
map3
from pyecharts import Map, Geo
data = [
("海门", 9),("鄂尔多斯", 12),("招远", 12),("舟山", 12),("齐齐哈尔", 14),("盐城", 15),
("赤峰", 16),("青岛", 18),("乳山", 18),("金昌", 19),("泉州", 21),("莱西", 21),
("日照", 21),("胶南", 22),("南通", 23),("拉萨", 24),("云浮", 24),("梅州", 25)]
geo = Geo("全国主要城市空气质量热力图", "data from pm2.5", title_color="#fff", title_pos="center", width=1200, height=600, background_color='#404a59')
attr, value = geo.cast(data)
geo.add("空气质量热力图", attr, value, visual_range=[0, 25], type='heatmap',visual_text_color="#fff", symbol_size=15, is_visualmap=True, is_roam=False)
geo.render(path="./空气质量热力图.html") #保存到本地
geo
from pyecharts import Map, Geo
# 空气质量评分
indexs = ['上海', '北京', '合肥', '哈尔滨', '广州', '成都', '无锡', '杭州', '武汉', '深圳', '西安', '郑州', '重庆', '长沙']
values = [4.07, 1.85, 4.38, 2.21, 3.53, 4.37, 1.38, 4.29, 4.1, 1.31, 3.92, 4.47, 2.40, 3.60]
geo = Geo("全国主要城市空气质量评分", "data from pm2.5", title_color="#fff", title_pos="center", width=1200, height=600, background_color='#404a59')
# type="effectScatter", is_random=True, effect_scale=5 使点具有发散性
geo.add("空气质量评分", indexs, values, type="effectScatter", is_random=True, effect_scale=5, visual_range=[0, 5],visual_text_color="#fff", symbol_size=15, is_visualmap=True, is_roam=False)
geo
# -*- coding: utf-8 -*-
from pyecharts import Map, Geo
# 世界地图数据
value = [95.1, 23.2, 43.3, 66.4, 88.5, 20]
attr= ["China", "Canada", "Brazil", "Russia", "United States", "Algeria"]
world_map = Map("世界地图示例", width=1200, height=600)
world_map.add("世界地图", attr, value, maptype="world", is_visualmap=True, visual_text_color='#000')
world_map
(By:阿优乐扬 2020-03-06 晚上10点夜于 黔西南布依族苗族自治州贞丰县 https://blog.csdn.net/ayouleyang )
学习文章:
[1] Python + PyEcharts——数据可视化
[2] Python3:pyecharts数据可视化插件
[3] python开发之pyecharts
[4] 数据地图
[5] python最全画地图,可视化数据,pyecharts画图<一>
[6] Python模块–PyEcharts