﻿ Python数据可视化 pyecharts实现各种统计图表过程详解 - 军军小站|张军博客

# Python数据可视化 pyecharts实现各种统计图表过程详解

1、pyecharts介绍

Echarts是一款由百度公司开发的开源数据可视化JS库，pyecharts是一款使用python调用echarts生成数据可视化的类库，可实现柱状图，折线图，饼状图，地图等统计图表。

2、柱状图

```
from pyecharts import Bar
fruits = ['苹果','香蕉','凤梨','桔子','橙','桃子']
sales = [18888,20023,30989,8873,29876,5409]
bar = Bar('水果销售情况')
(bar.render())
```

```
from pyecharts import Bar
fruits = ['苹果','香蕉','凤梨','桔子','橙','桃子']
shop1_sales = [8888,3323,6989,8873,3876,15409]
bar = Bar('水果销售情况')
shop2_sales = [4888,7023,3989,5873,8876,6409]
bar.render()
```

```
from pyecharts import Bar
fruits = ['苹果','香蕉','凤梨','桔子','橙','桃子']
shop1_sales = [8888,3323,6989,8873,3876,15409]
bar = Bar('水果销售情况')
shop2_sales = [4888,7023,3989,5873,8876,6409]
bar.render()
```

(2)is_more_utils=True 参数来设置最右侧工具栏，对生成的图进行更多的操作，如将柱形图更改为折线图等

(3)标记的使用：mark_point=[‘average']标记点，平均值；mark_line=[‘min','max','average']标记线，最大值、最小值和平均值

(4)横向柱形图：is_convert=True，标识交换X轴和Y轴

3、折线图

```
# 普通折线图
fruits = ['苹果','香蕉','凤梨','桔子','橙','桃子']
shop1_sales = [8888,3323,6989,8873,3876,15409]
shop2_sales = [4888,7023,3989,5873,8876,6409]

line = Line('折线图')
line.show_config()
line.render()
```

line()方法中有个is_smooth的参数，将参数的值设置为True,折线图的线条会以圆滑的趋势变化，不像上图那样以直线的方式变化。

```
# 普通折线图
fruits = ['苹果','香蕉','凤梨','桔子','橙','桃子']
shop1_sales = [8888,3323,6989,8873,3876,15409]
shop2_sales = [4888,7023,3989,5873,8876,6409]

line = Line('折线图')
line.show_config()
line.render()
```

```
fruits = ['苹果','香蕉','凤梨','桔子','橙','桃子']
shop2_sales = [4888,7023,3989,5873,8876,6409]

line = Line('折线图')
line.show_config()
line.render()
```

```
fruits = ['苹果','香蕉','凤梨','桔子','橙','桃子']
shop2_sales = [4888,7023,3989,5873,8876,6409]
shop1_sales = [8888,3323,6989,8873,3876,15409]
line3 =Line("面积折线图")
line3.add("商家A", fruits, shop1_sales, is_fill=True, line_opacity=0.2,  area_opacity=0.4, symbol=None, mark_point=['max'])
line3.add("商家B", fruits, shop2_sales, is_fill=True, area_color='#a3aed5', area_opacity=0.3, is_smooth=True)
line3.show_config()
line3.render()
```

```
from pyecharts import Bar, Line, Overlap
fruits = ['苹果','香蕉','凤梨','桔子','橙','桃子']

shop1_sales = [8888,3323,6989,8873,3876,15409]
shop2_sales = [4888,7023,3989,5873,8876,6409]

bar = Bar("柱形图-折线图")
line = Line()

overlap = Overlap()
overlap.show_config()
overlap.render()
```

4、饼图

```
from pyecharts import Pie
fruits = ['苹果','香蕉','凤梨','桔子','橙','桃子']
shop1_sales = [8888,3323,6989,8873,3876,15409]
pie = Pie('饼图')
pie.show_config()
pie.render()
```

```
pie2 = Pie("饼图-玫瑰图示例", title_pos='center', width=900)
pie2.show_config()
pie2.render()
```

5、散点图

```
from pyecharts import Scatter
scatter =Scatter("散点图示例")
scatter.show_config()
scatter.render()
```

```
from pyecharts import EffectScatter
v1 =[5, 20, 36, 10, 10, 100]
v2 =[55, 60, 16, 20, 15, 80]

# 动态散点图
es =EffectScatter("动态散点图")

# v1 x坐标 v2 y坐标
es.show_config()
es.render()
```

```
from pyecharts import EffectScatter
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.show_config()
es.render()
```

QQ号联系： 360901061

【本文对您有帮助就好】