有一个web+flask项目需要可视化数据分析结果,检索后发现,pyecharts工具包非常对口。
Echarts 是一个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。而 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,pyecharts 诞生了。
pyecharts中文文档有详细的说明,这里记录了个人更感兴趣的部分和对应的使用结果。
整体说明
pyecharts绘图的步骤可以简化成:
新建合适的图表对象,常见的有:
Pie: 饼图
Bar: 柱状图/条状图
Boxplot: 箱形图
HeatMap: 热力图
Line: 折线图/面积图
Scatter: 散点图
特别的,可以把多个图合在一起的 Overlap: 层叠多图
更详细的可以参考官方文档-图表类型
1
|
bar = Bar() |
后续的操作都是利用这个对象的方法进行。
为图表对象增加数据,比如 增加x轴(.add_xaxis)、y轴数(.add_yaxis)
1
2
|
bar.add_xaxis([ "衬衫" , "羊毛衫" , "雪纺衫" , "裤子" , "高跟鞋" , "袜子" ]) bar.add_yaxis( "商家A" , [ 5 , 20 , 36 , 10 , 75 , 90 ]) |
全局配置项:所有的内容都是通过.set_global_opts方法添加.set_global_opts()
1
|
bar.set_global_opts(title_opts = opts.TitleOpts(title = "主标题" , subtitle = "副标题" )) |
常用的有
TitleOpts:标题配置项
LegendOpts:图例配置项
VisualMapOpts:视觉映射配置项
AxisLineOpts: 坐标轴轴线配置项
AxisTickOpts: 坐标轴刻度配置项
AxisPointerOpts: 坐标轴指示器配置项
AxisOpts:坐标轴配置项
SingleAxisOpts:单轴配置项
详见官方文档配置项-全局配置项
例子
Boxplot
箱型图,一种比较简洁的统计值可视化方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import random from pyecharts import options as opts from pyecharts.charts import Boxplot import numpy as np # 离线资源,有网络下可以不管 from pyecharts. globals import CurrentConfig CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8889/assets/" # 长度为1的str list x_label = [ '随机数' ] data = np.random.randint( 1000 , size = 100 ) # 这里data应为2维数组,长度和x_label相同的 list list data = [data.tolist()] boxplot = Boxplot() boxplot.add_xaxis(x) # 调用自带的函数,计算箱型图需要的数据 y_value = boxplot.prepare_data(y_value) boxplot.add_yaxis('', y_value) boxplot.set_global_opts(title_opts = opts.TitleOpts(title = 'box plot demo' )) boxplot.render() |
Bar
Bar比较简单,适合入门,设定一个x轴,一个y轴,就可以render了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# -*- coding: utf-8 -*- from pyecharts.charts import Bar from pyecharts import options as opts import numpy as np # 离线资源,有网络下可以不管 from pyecharts. globals import CurrentConfig CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8889/assets/" # 随机数组,0~255的数字,10000个 x = np.random.randint( 255 , size = 1000 ) # 统计直方图 sum = np.zeros( 256 , dtype = np.int32) for cur_x in x: sum [cur_x] + = 1 # 绘图 bar = Bar() # x轴 0~255 x_label = [ str (label) for label in list ( range ( 256 ))] bar.add_xaxis(x_label) # y轴 频数, 这里的list一定要是标准int,不能为 np.int,所有 y_axis=list(sum)的话是不可以的 bar.add_yaxis(series_name = '频数' , y_axis = sum .tolist()) # 设置标题 bar.set_global_opts(title_opts = opts.TitleOpts(title = '直方图统计' )) # 生成网页,会在当前目录下生成一个render.html bar.render() |
HeatMap
热力图
这篇已经叙述的很好了,以下为引用
注,引文中的代码是用链式写的,官方是这么推荐的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import random from pyecharts import options as opts from pyecharts.charts import HeatMap from pyecharts.faker import Faker # 离线资源,有网络下可以不管 from pyecharts. globals import CurrentConfig CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8889/assets/" value = [[i, j, random.randint( 0 , 50 )] for i in range ( 24 ) for j in range ( 7 )] heatmap = ( HeatMap() .add_xaxis(Faker.clock) .add_yaxis( "", Faker.week, value, label_opts = opts.LabelOpts(is_show = True , position = "inside" ), ) .set_global_opts( title_opts = opts.TitleOpts(title = "基础热力图" ), visualmap_opts = opts.VisualMapOpts(), ) ) heatmap.render() |
以上就是python可视化数据分析pyecharts初步尝试的详细内容,更多关于python可视化数据分析pyecharts的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/yushengchn/p/15909367.html