pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。本文将为你阐述pyecharts的使用细则。
安装
代码语言:javascript复制pip install pyecharts
# 建议安装插件
pip install echarts-themes-pypkg
绘制柱形图
代码语言:javascript复制from pyecharts import Bar
x = ["毛衣", "袜子", "手套", "裤子", "高跟鞋"]
y1 = [5, 8, 9, 10, 15]
y2 = [10, 10, 8, 10, 20]
bar = Bar(title = "产品月销量",width = 600,height = 420)
bar.add(name = "商家A", x_axis = x, y_axis = y1)
bar.add(name = "商家B", x_axis = x, y_axis = y2,is_xaxis_boundarygap =True)
# 导出绘图html文件,可直接用浏览器打开
bar.render('柱形图示范.html')
折线图
代码语言:javascript复制from pyecharts import Line
x = ['2019-{:0>2d}'.format(s) for s in range(1,13)]
y1 = [5,10,26,10,20,30,20,24,40,46,40,50]
y2 = [8,20,24,36,40,36,40,25,30,33,48,42]
line = Line(title = "月销售总额",width = 600,height = 420)
line.add(name = "商家A", x_axis = x, y_axis = y1,
line_width = 3,line_color = 'red')
line.add(name = "商家B", x_axis = x, y_axis = y2,
yaxis_min = 0,yaxis_max = 50,is_xaxis_boundarygap = True,
is_datazoom_show =True,line_width = 2,line_color = 'cyan')
line.render('折线图示范.html')
line
饼图
代码语言:javascript复制# 饼图
from pyecharts import Pie
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
value = [11, 12, 13, 10, 10, 10]
pie = Pie("销售额占比",width = 800,height = 520)
pie.add("", attr, value, is_label_show=True)
pie
箱型图
代码语言:javascript复制# 箱型图
from pyecharts import Boxplot
x =['1班','2班','3班','4班']
y1=[78, 98, 56, 78, 90.0, 45, 78, 20, 87, 86, 74, 89, 94]
y2=[89, 82, 45, 67, 68, 78.0, 79, 98, 71, 56, 78, 81, 80]
y3=[90, 80, 60, 89, 76, 73.0, 72, 92, 89, 87, 65, 66, 76]
y4=[82, 72, 55, 100, 90.0, 78, 69, 67, 87, 66, 78, 71, 82]
box = Boxplot(title = '考试成绩箱型图',width = 600,height = 420)
# 预处理数据计算最大值,最小值,中位数以及上下四分位数
y_prepared = box.prepare_data([y1,y2,y3,y4])
box.add(name = '',x_axis = x,y_axis = y_prepared)
词云图
代码语言:javascript复制# 词云图
from pyecharts import WordCloud
words = ['python','jupyter','numpy','pandas','matplotlib','sklearn',
'xgboost','lightGBM','simpy','keras','tensorflow',
'hive','hadoop','spark']
counts = [100,90,65,95,50,60,70,70,20,70,80,80,60,60]
cloud = WordCloud(title = 'python常用工具',width = 600,height = 420)
cloud.add(name = 'utils',attr = words,value = counts,
shape = "circle",word_size_range = (10,50))
cloud
全国省份地图
代码语言:javascript复制#全国省份地图
from pyecharts import Map
value = [155, 10, 66, 78, 44, 38, 88, 50, 20]
attr = ["福建","山东","北京","上海","江西","新疆","内蒙古","云南","重庆"]
m = Map("全国省份地图", width=600, height=400)
m.add("", attr, value, maptype='china',
is_visualmap=True,
is_piecewise=True,
visual_text_color="#000",
visual_range_text=["", ""],
pieces=[
{"max": 160, "min": 81, "label": "高"},
{"max": 80, "min": 51, "label": "中"},
{"max": 50, "min": 0, "label": "低"},
])
m
'''
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。
'''
世界地图
代码语言:javascript复制from pyecharts import Map
value = [95.1, 23.2, 43.3, 66.4, 88.5]
attr= ["China", "Canada", "Brazil", "Russia", "United States"]
map = Map("世界地图", width=1200, height=600)
map.add("", attr, value, maptype="world", is_visualmap=True, visual_text_color='#000')
map
全国城市地图示例
代码语言:javascript复制# 全国城市地图示例
from pyecharts import Geo
data = [
("海门", 9),("鄂尔多斯", 12),("招远", 12),("舟山", 12),("齐齐哈尔", 14),("盐城", 15),
("惠州", 37),("江阴", 37),("蓬莱", 37),("韶关", 38),("嘉峪关", 38),("广州", 38),
("张家港", 52),("三门峡", 53),("锦州", 54),("南昌", 54),("柳州", 54),("三亚", 54),
("呼和浩特", 58),("成都", 58),("大同", 58),("镇江", 59),("桂林", 59),("张家界", 59),
("北京", 79),("徐州", 79),("衡水", 80),("包头", 80),("绵阳", 80),("乌鲁木齐", 84),
("菏泽", 194),("合肥", 229),("武汉", 273),("大庆", 279)]
geo = Geo(
"全国部分城市空气质量",
title_color="#fff",
title_pos="center",
width=800,
height=600,
background_color="#404a59",
)
attr, value = geo.cast(data)
geo.add(
"",
attr,
value,
visual_range=[0, 200],
visual_text_color="#fff",
symbol_size=15,
is_visualmap=True,
)
geo
3D 散点图
代码语言:javascript复制import random
from pyecharts import Page,Scatter3D
data = [
[random.randint(0, 100),
random.randint(0, 100),
random.randint(0, 100)] for _ in range(80)
]
range_color = [
'#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',
'#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
scatter3D = Scatter3D("3D 散点图示例", width= 500, height=300)
scatter3D.add("", data, is_visualmap=True, visual_range_color=range_color)
scatter3D
参考:http://pyecharts.herokuapp.com/
为了地图上的数据能显示完全,先加载好需要的城市地理坐标数据。
代码语言:javascript复制pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
pip install echarts-china-counties-pypkg
pip install echarts-china-misc-pypkg
pip install echarts-united-kingdom-pypkg