pyecharts模块的几个经典案例(python经典编程案例)

简介: 文章提供了多个使用pyecharts模块创建数据可视化的Python编程案例,展示如何生成各种类型的图表并进行定制化设置。

一. pyecharts概述

pyecharts是基于ECharts图表库开发的python第三方模块。
ECharts是一个纯JavaScript的商业级图表库,兼容当前绝大部分浏览器,能够创建类型丰富,精美生动,可交互,可高度个性化定制的数据可视化效果。
安装:pip3 install pyecharts

二. 案例

2.1 图表配置项

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType

x = ['连衣裙', '短裤', '运动套装', '牛仔裤', '针织衫', '半身裙', '衬衫', '阔腿裤', '打底裤']
y1 = [36, 56, 60, 78, 90, 20, 50, 70, 10]
y2 = [16, 30, 50, 90, 45, 10, 60, 54, 40]
chart = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
chart.add_xaxis(x)
chart.add_yaxis('分店A', y1)
chart.add_yaxis('分店B', y2)
chart.set_global_opts(title_opts=opts.TitleOpts(title='产品销售额对比图', pos_left='left'),
                      yaxis_opts=opts.AxisOpts(name='销售业绩(元)', name_location='end'),
                      xaxis_opts=opts.AxisOpts(name='产品', name_location='end'),
                      tooltip_opts=opts.TooltipOpts(is_show=True, formatter='{a}<br/>{b}:{c}', background_color='black', border_width=15),
                      legend_opts=opts.LegendOpts(is_show=False),
                      toolbox_opts=opts.ToolboxOpts(is_show=True, orient='horizontal'),
                      visualmap_opts=opts.VisualMapOpts(is_show=True, type_='color', min_=0, max_=100, orient='vertical'),
                      datazoom_opts=opts.DataZoomOpts(is_show=True, type_='slider'))
chart.render('图表配置项.html')

2.2 绘制漏斗图

import pyecharts.options as opts
from pyecharts.charts import Funnel
x = ['浏览商品', '放入购物车', '生成订单', '支付订单', '完成交易']
y = [1000, 900, 400, 360, 320]
data = [i for i in zip(x, y)]
chart = Funnel()
chart.add(series_name='人数', data_pair=data, label_opts=opts.LabelOpts(is_show=True, position='inside'), tooltip_opts=opts.TooltipOpts(trigger='item', formatter='{a}:{c}'))
chart.set_global_opts(title_opts=opts.TitleOpts(title='电商网站流量转化漏斗图', pos_left='center'), legend_opts=opts.LegendOpts(is_show=False))
chart.render('漏斗图.html')

import pyecharts.options as opts
from pyecharts.charts import Funnel
x = ['浏览商品', '放入购物车', '生成订单', '支付订单', '完成交易']
y = [1000, 900, 400, 360, 320]
data = [i for i in zip(x, y)]
chart = Funnel()
chart.add(series_name='人数', data_pair=data, sort_='ascending', gap=15, label_opts=opts.LabelOpts(is_show=True, position='inside'), tooltip_opts=opts.TooltipOpts(trigger='item', formatter='{a}:{c}'))
chart.set_global_opts(title_opts=opts.TitleOpts(title='电商网站流量转化漏斗图', pos_left='center'), legend_opts=opts.LegendOpts(is_show=False))
chart.render('漏斗图.html')

2.3 绘制涟漪特效散点图

import pandas as pd
import pyecharts.options as opts
from pyecharts.charts import EffectScatter
data = pd.read_excel('客户购买力统计表.xlsx')
x = data['年龄'].tolist()
y = data['消费金额(元)'].tolist()
chart = EffectScatter()
chart.add_xaxis(x)
chart.add_yaxis(series_name='年龄,消费金额(元)', y_axis=y,
                label_opts=opts.LabelOpts(is_show=False),
                symbol_size=15)
chart.set_global_opts(title_opts=opts.TitleOpts(title='客户购买力散点图'),
                      yaxis_opts=opts.AxisOpts(type_='value', name='消费金额(元)', name_location='middle', name_gap=40),
                      xaxis_opts=opts.AxisOpts(type_='value', name='年龄', name_location='middle', name_gap=40),
                      tooltip_opts=opts.TooltipOpts(trigger='item', formatter='{a}:{c}'))
chart.render('涟漪特效散点图.html')

2.4 绘制水球图


import pyecharts.options as opts
from pyecharts.charts import Liquid
a = 68
t = 100
chart = Liquid()
chart.add(series_name = '商品A', data = [a / t])
chart.set_global_opts(title_opts = opts.TitleOpts(title = '产品销售业绩达成率', pos_left = 'center'))
chart.render('水球图.html')

import pyecharts.options as opts
from pyecharts.charts import Liquid
a = 68
t = 100
chart = Liquid()
chart.add(series_name = '商品A', data = [a / t], shape = 'rect')
chart.set_global_opts(title_opts = opts.TitleOpts(title = '产品销售业绩达成率', pos_left = 'center'))
chart.render('水球图.html')

import pyecharts.options as opts
from pyecharts.charts import Liquid
a1 = 68
a2 = 120
a3 = 37
t = 100
chart = Liquid()
chart.set_global_opts(title_opts=opts.TitleOpts(title='产品销售业绩达成率', pos_left='center'))
chart.add(series_name='商品A', data=[a1 / t], center=['20%', '50%'])
chart.add(series_name='商品B', data=[a2 / t], center=['50%', '50%'])
chart.add(series_name='商品C', data=[a3 / t], center=['80%', '50%'])
chart.render('水球图.html')

2.5 绘制仪表盘

import pyecharts.options as opts
from pyecharts.charts import Gauge
chart = Gauge()
chart.add(series_name = '业务指标', data_pair = [('完成率', '62.25')], split_number = 10, radius = '80%', title_label_opts = opts.LabelOpts(font_size = 30, color = 'red', font_family = 'Microsoft YaHei'))
chart.set_global_opts(legend_opts = opts.LegendOpts(is_show = False), tooltip_opts = opts.TooltipOpts(is_show = True, formatter = '{a}<br/>{b}:{c}%'))
chart.render('仪表盘.html')

2.6 绘制词云图

import pandas as pd
import pyecharts.options as opts
from pyecharts.charts import WordCloud
data = pd.read_excel('电影票房统计.xlsx')
name = data['电影名称']
value = data['总票房(亿元)']
data1 = [z for z in zip(name, value)]
chart = WordCloud()
chart.add('总票房(亿元)', data_pair = data1, word_size_range = [6, 66])
chart.set_global_opts(title_opts=opts.TitleOpts(title = '电影票房分析', title_textstyle_opts = opts.TextStyleOpts(font_size = 30)), tooltip_opts = opts.TooltipOpts(is_show = True))
chart.render('词云图.html')

import pandas as pd
import pyecharts.options as opts
from pyecharts.charts import WordCloud
data = pd.read_excel('电影票房统计.xlsx')
name = data['电影名称']
value = data['总票房(亿元)']
data1 = [z for z in zip(name, value)]
chart = WordCloud()
chart.add('总票房(亿元)', data_pair = data1, shape = 'star', word_size_range = [6, 66])
chart.set_global_opts(title_opts=opts.TitleOpts(title = '电影票房分析', title_textstyle_opts = opts.TextStyleOpts(font_size = 30)), tooltip_opts = opts.TooltipOpts(is_show = True))
chart.render('词云图.html')

2.7 绘制K线图

import tushare as ts
data = ts.get_k_data('000005', start = '2010-01-01', end = '2020-01-01')
print(data.head())
data.to_excel('股价数据.xlsx', index = False)
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Kline
data = pd.read_excel('股价数据.xlsx')
x = list(data['date'])
open = data['open']
close = data['close']
lowest = data['low']
highest = data['high']
y = [list(z) for z in zip(open, close, lowest, highest)]
chart = Kline()
chart.add_xaxis(x)
chart.add_yaxis('股价', y)
chart.set_global_opts(xaxis_opts = opts.AxisOpts(is_scale = True),
                      yaxis_opts = opts.AxisOpts(is_scale = True,
                                                 splitarea_opts = opts.SplitAreaOpts(is_show = True,
                                                                                     areastyle_opts = opts.AreaStyleOpts(opacity = 1))),
                      datazoom_opts = [opts.DataZoomOpts(type_ = 'inside')],
                      title_opts = opts.TitleOpts(title = '股价走势图'))
chart.render('K线图.html')
相关文章
|
1天前
|
关系型数据库 MySQL Python
pymysql模块,python与MySQL之间的交互
pymysql模块,python与MySQL之间的交互
|
1天前
|
Unix Linux 网络安全
python中连接linux好用的模块paramiko(附带案例)
该文章详细介绍了如何使用Python的Paramiko模块来连接Linux服务器,包括安装配置及通过密码或密钥进行身份验证的示例。
8 1
|
1天前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
8 0
|
1天前
|
调度 数据库 Python
python中APScheduler的使用详解(python3经典编程案例)
文章详细讲解了在Python中使用APScheduler来安排和执行定时任务的方法,包括不同调度器的配置与使用场景。
9 0
|
1天前
|
机器学习/深度学习 开发工具 git
matplotlib各种案例总结(python经典编程案例)
该文章汇总了使用matplotlib绘制不同类型图表的方法和案例,包括条形图、折线图等,并展示了如何调整颜色和线条样式等属性。
8 0
|
1天前
|
数据挖掘 Python
用python的tushare模块分析股票案例(python3经典编程案例)
该文章提供了使用Python的tushare模块分析股票数据的案例,展示了如何获取股票数据以及进行基本的数据分析。
9 0
|
4月前
|
数据采集 JSON JavaScript
Python爬虫案例:抓取猫眼电影排行榜
python爬取猫眼电影排行榜数据分析,实战。(正则表达式,xpath,beautifulsoup)【2月更文挑战第11天】
222 2
Python爬虫案例:抓取猫眼电影排行榜
|
3月前
|
数据采集 前端开发 Java
Python简单爬虫案例
用pyhton从网页中爬取数据,是比较常用的爬虫方式。网页一般由html编写,里面包含大量的标签,我们所需的内容都包含在这些标签之中,除了对python的基础语法有了解之外,还要对html的结构以及标签选择有简单的认知,下面就用爬取fl小说网的案例带大家进入爬虫的世界。
|
3月前
|
数据采集 前端开发 Java
Python简单爬虫案例
用pyhton从网页中爬取数据,是比较常用的爬虫方式。网页一般由html编写,里面包含大量的标签,我们所需的内容都包含在这些标签之中,除了对python的基础语法有了解之外,还要对html的结构以及标签选择有简单的认知,下面就用爬取fl小说网的案例带大家进入爬虫的世界。
|
4月前
|
数据采集 Web App开发 Java
Python 爬虫:Spring Boot 反爬虫的成功案例
Python 爬虫:Spring Boot 反爬虫的成功案例