pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。用 Echarts 生成的图可视化效果非常棒,pyecharts 是为了与 Python 进行对接,方便在 Python 中直接使用数据生成图。
单个条形图
案例_内地电影票房前5的电影和电影票房数据:
假设你获取到了某年内地电影票房前5的电影(列表a)和电影票房数据(列表b),那么如何更加直观的展示该数据?
a = [“战狼2”, “速度与激情8”, “功夫瑜伽”, “西游伏妖篇”, “变形金刚5”]
b = [56.01, 26.94, 17.53, 16.49, 15.45]
代码:
from pyecharts import Bar
movieName = ["战狼2", "速度与激情8", "功夫瑜伽", "西游伏妖篇", "变形金刚5"]
movieMoney = [56.01, 26.94, 17.53, 16.49, 15.45]
bar = Bar(title='内地电影票房前5的电影和电影票房数据',subtitle='子标题')
# 添加图表的数据和配置信息
bar.add('电影信息',movieName,movieMoney)
# 默认情况下会生成一个render.html文件
bar.render()
案例_三天间电影票房数据对比:
假设你知道了列表a中电影分别在2017-09-14(b_14),2017-09-15(b_15),2017-09-16(b_16)三天的票房,为了展示列表中电影本身的票房以及同其他电影的数据对比情况,应该如何更加直观的呈现该数据?
a = ["猩球崛起3:终极之战”,"敦刻尔克”,"蜘蛛侠:英雄归来”,"战狼2”]
b_16 = [15746,312,4497,319]
b_15 = [12357,156,2045,168]
b_14 = [2358,399,2358,362]
代码:
from pyecharts import Bar
movieName = ["猩球崛起3","敦刻尔克","蜘蛛侠","战狼2"]
movieMoney_16 = [15746,312,4497,319]
movieMoney_15 = [12357,156,2045,168]
movieMoney_14 = [2358,399,2358,362]
bar = Bar(title='三天间电影票房数据对比统计图')
bar.add('2017-9-14',movieName,movieMoney_14)
bar.add('2017-9-15',movieName,movieMoney_15)
bar.add('2017-9-16',movieName,movieMoney_16)
bar.render()
统计北京3,10月份每天气温变化
代码:
from pyecharts import Scatter
times = list(range(1, 32))
march_temp = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21, 22,
22, 22, 23]
oct_temp = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11,
13, 12, 13, 6]
scatter = Scatter(title='北京3,10月份每天气温变化散点图',subtitle='子标题')
# symbol_size散点图标记的大小
scatter.add('3月',times,march_temp,symbol_size=15,line_color='red')
scatter.add('10月',times,oct_temp)
scatter.render()
代码:
from pyecharts import Funnel
x_movies_name = ["猩球崛起", "敦刻尔克", "蜘蛛侠", "战狼2"]
y_16 = [20, 40, 60, 80]
funnel = Funnel("电影和电影票房数据")
funnel.add("电影信息", x_movies_name, y_16)
funnel.render()
CPU使用率
代码:
import psutil
from pyecharts import Gauge
cpu_percent = psutil.cpu_percent()
gauge = Gauge('CPU使用率')
gauge.add('cpu','CPU使用率',cpu_percent)
gauge.render()
绘制图标如下:
CPU使用率
代码:
import psutil
from pyecharts import Liquid
cpu_percent = psutil.cpu_percent()
liquid = Liquid('CPU使用率')
liquid.add('cpu',[cpu_percent / 100])
# shape='pin':水球形状
# [0.6,0.5,0.3,0.4]:四条水波纹
liquid.add('cpu',[0.6,0.5,0.3,0.4],shape='pin')
liquid.render()
代码:
from pyecharts import Pie
attr = ['男', '女', '其他']
data = [100, 180, 2]
pie = Pie('男女比例')
# is_label_show:是否直接显示label信息
pie.add('', attr, data, is_label_show=True)
pie.render()
代码:
from pyecharts import Line
# 图表的x轴的数据, 是一个可迭代的数据类型
times = list(range(1,32))
# 图表的y轴的数据, 是一个可迭代的数据类型
march_temp = [11, 17, 16, 11, 12, 11, 12, 6, 6, 7, 8, 9, 12, 15, 14, 17, 18, 21, 16, 17, 20, 14, 15, 15, 15, 19, 21, 22, 22, 22, 23]
oct_temp = [26, 26, 28, 19, 21, 17, 16, 19, 18, 20, 20, 19, 22, 23, 17, 20, 21, 20, 22, 15, 11, 15, 5, 13, 17, 10, 11, 13, 12, 13, 6]
line = Line('3,10月份每天气温变化散点图')
line.add('',times,march_temp,mark_line=['max'],mark_point=['min'])
line.add('',times,oct_temp,mark_line=['max'],mark_point=['min'])
# 折线图---阶梯图
# line.add('',times,march_temp,is_step=True)
# line.add('',times,oct_temp,is_step=True)
# 折线图---面积图(必须设置透明度:area_opacity=0.3)
# line.add('',times,march_temp,is_fill=True,area_color='red',area_opacity=0.3)
# line.add('',times,oct_temp,is_fill=True,area_color='blue',area_opacity=0.3)
line.render()
可以在pyecharts官网copy一个例子试运行: http://pyecharts.org/#/zh-cn/charts_base?id=map(地图)
代码:
from pyecharts import Map
value = [155, 10, 66, 78, 33, 80, 190, 53, 49.6]
attr = [
"福建", "山东", "北京", "上海", "甘肃", "新疆", "河南", "广西", "西藏"
]
map = Map("Map 结合 VisualMap 示例", width=1200, height=600)
map.add(
"",
attr,
value,
maptype="china",
is_visualmap=True,
visual_text_color="#000",
)
map.render()
绘制图标如下: