@@ -3301,7 +3301,7 @@ def setup_axes():
3301
3301
main()
3302
3302
```
3303
3303
3304
- # ### 11 绘制仪表盘
3304
+ # ### 11 pyecharts绘制仪表盘
3305
3305
3306
3306
使用pip install pyecharts 安装,版本为 v1.6,pyecharts绘制仪表盘,只需要几行代码:
3307
3307
@@ -3321,6 +3321,257 @@ print('ok')
3321
3321

3322
3322
3323
3323
3324
+
3325
+ # ### 12 pyecharts漏斗图
3326
+
3327
+ ```python
3328
+ from pyecharts import options as opts
3329
+ from pyecharts.charts import Funnel, Page
3330
+ from random import randint
3331
+
3332
+ def funnel_base() -> Funnel:
3333
+ c = (
3334
+ Funnel()
3335
+ .add(" 豪车" , [list (z) for z in zip ([' 宝马' , ' 法拉利' , ' 奔驰' , ' 奥迪' , ' 大众' , ' 丰田' , ' 特斯拉' ],
3336
+ [randint(1 , 20 ) for _ in range (7 )])])
3337
+ .set_global_opts(title_opts = opts.TitleOpts(title = " 豪车漏斗图" ))
3338
+ )
3339
+ return c
3340
+ funnel_base().render(' ./img/car_fnnel.html' )
3341
+ ```
3342
+
3343
+ 以7 种车型及某个属性值绘制的漏斗图,属性值大越靠近漏斗的大端。
3344
+
3345
+ 
3346
+
3347
+
3348
+
3349
+ # ### 13 pyecharts日历图
3350
+
3351
+ ```python
3352
+ import datetime
3353
+ import random
3354
+ from pyecharts import options as opts
3355
+ from pyecharts.charts import Calendar
3356
+
3357
+ def calendar_interval_1() -> Calendar:
3358
+ begin = datetime.date(2019 , 1 , 1 )
3359
+ end = datetime.date(2019 , 12 , 27 )
3360
+ data = [
3361
+ [str (begin + datetime.timedelta(days = i)), random.randint(1000 , 25000 )]
3362
+ for i in range (0 , (end - begin).days + 1 , 2 ) # 隔天统计
3363
+ ]
3364
+ calendar = (
3365
+ Calendar(init_opts = opts.InitOpts(width = " 1200px" )).add(
3366
+ " " , data, calendar_opts = opts.CalendarOpts(range_ = " 2019" ))
3367
+ .set_global_opts(
3368
+ title_opts = opts.TitleOpts(title = " Calendar-2019年步数统计" ),
3369
+ visualmap_opts = opts.VisualMapOpts(
3370
+ max_ = 25000 ,
3371
+ min_ = 1000 ,
3372
+ orient = " horizontal" ,
3373
+ is_piecewise = True ,
3374
+ pos_top = " 230px" ,
3375
+ pos_left = " 100px" ,
3376
+ ),
3377
+ )
3378
+ )
3379
+ return calendar
3380
+
3381
+ calendar_interval_1().render(' ./img/calendar.html' )
3382
+ ```
3383
+
3384
+ 绘制2019 年1 月1 日到12 月27 日的步行数,官方给出的图形宽度`900px ` 不够,只能显示到9 月份,本例使用`opts.InitOpts(width = " 1200px" )` 做出微调,并且`visualmap` 显示所有步数,每隔一天显示一次:
3385
+
3386
+ 
3387
+
3388
+ # ### 14 pyecharts绘制graph图
3389
+
3390
+ ```python
3391
+ import json
3392
+ import os
3393
+ from pyecharts import options as opts
3394
+ from pyecharts.charts import Graph, Page
3395
+
3396
+ def graph_base() -> Graph:
3397
+ nodes = [
3398
+ {" name" : " cus1" , " symbolSize" : 10 },
3399
+ {" name" : " cus2" , " symbolSize" : 30 },
3400
+ {" name" : " cus3" , " symbolSize" : 20 }
3401
+ ]
3402
+ links = []
3403
+ for i in nodes:
3404
+ if i.get(' name' ) == ' cus1' :
3405
+ continue
3406
+ for j in nodes:
3407
+ if j.get(' name' ) == ' cus1' :
3408
+ continue
3409
+ links.append({" source" : i.get(" name" ), " target" : j.get(" name" )})
3410
+ c = (
3411
+ Graph()
3412
+ .add(" " , nodes, links, repulsion = 8000 )
3413
+ .set_global_opts(title_opts = opts.TitleOpts(title = " customer-influence" ))
3414
+ )
3415
+ return c
3416
+ ```
3417
+
3418
+ 构建图,其中客户点1 与其他两个客户都没有关系(`link` ),也就是不存在有效边:
3419
+
3420
+ 
3421
+
3422
+ # ### 15 pyecharts水球图
3423
+
3424
+ ```python
3425
+ from pyecharts import options as opts
3426
+ from pyecharts.charts import Liquid, Page
3427
+ from pyecharts.globals import SymbolType
3428
+
3429
+ def liquid() -> Liquid:
3430
+ c = (
3431
+ Liquid()
3432
+ .add(" lq" , [0.67 , 0.30 , 0.15 ])
3433
+ .set_global_opts(title_opts = opts.TitleOpts(title = " Liquid" ))
3434
+ )
3435
+ return c
3436
+
3437
+ liquid().render(' ./img/liquid.html' )
3438
+ ```
3439
+
3440
+ 水球图的取值`[0.67 , 0.30 , 0.15 ]` 表示下图中的`三个波浪线` ,一般代表三个百分比:
3441
+
3442
+ 
3443
+
3444
+ # ### 16 pyecharts饼图
3445
+
3446
+ ```python
3447
+ from pyecharts import options as opts
3448
+ from pyecharts.charts import Pie
3449
+ from random import randint
3450
+
3451
+ def pie_base() -> Pie:
3452
+ c = (
3453
+ Pie()
3454
+ .add(" " , [list (z) for z in zip ([' 宝马' , ' 法拉利' , ' 奔驰' , ' 奥迪' , ' 大众' , ' 丰田' , ' 特斯拉' ],
3455
+ [randint(1 , 20 ) for _ in range (7 )])])
3456
+ .set_global_opts(title_opts = opts.TitleOpts(title = " Pie-基本示例" ))
3457
+ .set_series_opts(label_opts = opts.LabelOpts(formatter = " {b} : {c} " ))
3458
+ )
3459
+ return c
3460
+
3461
+ pie_base().render(' ./img/pie_pyecharts.html' )
3462
+ ```
3463
+
3464
+ # ### 17 pyecharts极坐标图
3465
+
3466
+ ```python
3467
+ import random
3468
+ from pyecharts import options as opts
3469
+ from pyecharts.charts import Page, Polar
3470
+
3471
+ def polar_scatter0() -> Polar:
3472
+ data = [(alpha, random.randint(1 , 100 )) for alpha in range (101 )] # r = random.randint(1, 100)
3473
+ print (data)
3474
+ c = (
3475
+ Polar()
3476
+ .add(" " , data, type_ = " bar" , label_opts = opts.LabelOpts(is_show = False ))
3477
+ .set_global_opts(title_opts = opts.TitleOpts(title = " Polar" ))
3478
+ )
3479
+ return c
3480
+
3481
+ polar_scatter0().render(' ./img/polar.html' )
3482
+ ```
3483
+
3484
+ 极坐标表示为`(夹角,半径)` ,如(6 ,94 )表示夹角为6 ,半径94 的点:
3485
+ 
3486
+
3487
+ # ### 18 pyecharts词云图
3488
+
3489
+ ```python
3490
+ from pyecharts import options as opts
3491
+ from pyecharts.charts import Page, WordCloud
3492
+ from pyecharts.globals import SymbolType
3493
+
3494
+ words = [
3495
+ (" Python" , 100 ),
3496
+ (" C++" , 80 ),
3497
+ (" Java" , 95 ),
3498
+ (" R" , 50 ),
3499
+ (" JavaScript" , 79 ),
3500
+ (" C" , 65 )
3501
+ ]
3502
+
3503
+ def wordcloud() -> WordCloud:
3504
+ c = (
3505
+ WordCloud()
3506
+ # word_size_range: 单词字体大小范围
3507
+ .add(" " , words, word_size_range = [20 , 100 ], shape = ' cardioid' )
3508
+ .set_global_opts(title_opts = opts.TitleOpts(title = " WordCloud" ))
3509
+ )
3510
+ return c
3511
+
3512
+ wordcloud().render(' ./img/wordcloud.html' )
3513
+ ```
3514
+
3515
+ `(" C" ,65 )` 表示在本次统计中C语言出现65 次
3516
+
3517
+ 
3518
+
3519
+ # ### 19 pyecharts系列柱状图
3520
+
3521
+ ```python
3522
+ from pyecharts import options as opts
3523
+ from pyecharts.charts import Bar
3524
+ from random import randint
3525
+
3526
+ def bar_series() -> Bar:
3527
+ c = (
3528
+ Bar()
3529
+ .add_xaxis([' 宝马' , ' 法拉利' , ' 奔驰' , ' 奥迪' , ' 大众' , ' 丰田' , ' 特斯拉' ])
3530
+ .add_yaxis(" 销量" , [randint(1 , 20 ) for _ in range (7 )])
3531
+ .add_yaxis(" 产量" , [randint(1 , 20 ) for _ in range (7 )])
3532
+ .set_global_opts(title_opts = opts.TitleOpts(title = " Bar的主标题" , subtitle = " Bar的副标题" ))
3533
+ )
3534
+ return c
3535
+
3536
+ bar_series().render(' ./img/bar_series.html' )
3537
+ ```
3538
+
3539
+ 
3540
+
3541
+
3542
+
3543
+ # ### 20 pyecharts热力图
3544
+
3545
+ ```python
3546
+ import random
3547
+ from pyecharts import options as opts
3548
+ from pyecharts.charts import HeatMap
3549
+
3550
+ def heatmap_car() -> HeatMap:
3551
+ x = [' 宝马' , ' 法拉利' , ' 奔驰' , ' 奥迪' , ' 大众' , ' 丰田' , ' 特斯拉' ]
3552
+ y = [' 中国' ,' 日本' ,' 南非' ,' 澳大利亚' ,' 阿根廷' ,' 阿尔及利亚' ,' 法国' ,' 意大利' ,' 加拿大' ]
3553
+ value = [[i, j, random.randint(0 , 100 )]
3554
+ for i in range (len (x)) for j in range (len (y))]
3555
+ c = (
3556
+ HeatMap()
3557
+ .add_xaxis(x)
3558
+ .add_yaxis(" 销量" , y, value)
3559
+ .set_global_opts(
3560
+ title_opts = opts.TitleOpts(title = " HeatMap" ),
3561
+ visualmap_opts = opts.VisualMapOpts(),
3562
+ )
3563
+ )
3564
+ return c
3565
+
3566
+ heatmap_car().render(' ./img/heatmap_pyecharts.html' )
3567
+ ```
3568
+
3569
+ 热力图描述的实际是三维关系,x轴表示车型,y轴表示国家,每个色块的颜色值代表销量,颜色刻度尺显示在左下角,颜色越红表示销量越大。
3570
+
3571
+ 
3572
+
3573
+
3574
+
3324
3575
# ## 七、Python实战
3325
3576
3326
3577
0 commit comments