{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Pyecharts 可视化教程" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Echarts 是一个由百度开源的数据可视化javascript库,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。而 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,pyecharts 诞生了。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**注意,我们安装的是pyecharts的0.5.11的版本,不是最新版本!**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting pyecharts==0.5.11\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/52/b8/e46a41c44176cd247cb1fd437e38e179ea604de28c91755c30be1046772c/pyecharts-0.5.11-py2.py3-none-any.whl (122kB)\n", "\u001b[K 100% |████████████████████████████████| 122kB 366kB/s ta 0:00:01\n", "\u001b[?25hCollecting lml==0.0.2 (from pyecharts==0.5.11)\n", " Downloading https://files.pythonhosted.org/packages/8a/6f/0eb22694df07072ea467effdbb8e313222a21788ea7914773f59fd2bb857/lml-0.0.2-py2.py3-none-any.whl\n", "Requirement already satisfied: pillow in /Users/liangyun/anaconda3/lib/python3.7/site-packages (from pyecharts==0.5.11) (5.4.1)\n", "Requirement already satisfied: future in /Users/liangyun/anaconda3/lib/python3.7/site-packages (from pyecharts==0.5.11) (0.17.1)\n", "Requirement already satisfied: jinja2 in /Users/liangyun/anaconda3/lib/python3.7/site-packages (from pyecharts==0.5.11) (2.10)\n", "Collecting jupyter-echarts-pypkg==0.1.2 (from pyecharts==0.5.11)\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/46/71/839e9397d924d577aa44e78ac06a813c761b39a6eab8cd3ba3821b4c894b/jupyter-echarts-pypkg-0.1.2.tar.gz (520kB)\n", "\u001b[K 100% |████████████████████████████████| 522kB 714kB/s ta 0:00:011\n", "\u001b[?25hCollecting pyecharts-javascripthon==0.0.6 (from pyecharts==0.5.11)\n", " Downloading https://files.pythonhosted.org/packages/6f/68/23644a394341fef4cbcb48a3fa4b2816408a13a1fa3281cf9030b2ac2d67/pyecharts_javascripthon-0.0.6-py2.py3-none-any.whl\n", "Requirement already satisfied: MarkupSafe>=0.23 in /Users/liangyun/anaconda3/lib/python3.7/site-packages (from jinja2->pyecharts==0.5.11) (1.1.1)\n", "Requirement already satisfied: pyecharts-jupyter-installer==0.0.3 in /Users/liangyun/anaconda3/lib/python3.7/site-packages (from jupyter-echarts-pypkg==0.1.2->pyecharts==0.5.11) (0.0.3)\n", "Collecting javascripthon>=0.10; python_version > \"3.4\" (from pyecharts-javascripthon==0.0.6->pyecharts==0.5.11)\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/ea/ef/b46401e181768b3738660f5dc1f7ea0b334ed17fff685a61503d2989e77e/javascripthon-0.10.tar.gz (538kB)\n", "\u001b[K 100% |████████████████████████████████| 542kB 614kB/s ta 0:00:01\n", "\u001b[?25hRequirement already satisfied: setuptools in /Users/liangyun/anaconda3/lib/python3.7/site-packages (from javascripthon>=0.10; python_version > \"3.4\"->pyecharts-javascripthon==0.0.6->pyecharts==0.5.11) (40.8.0)\n", "Collecting dukpy (from javascripthon>=0.10; python_version > \"3.4\"->pyecharts-javascripthon==0.0.6->pyecharts==0.5.11)\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/0e/d4/7ebef46a9fe8c5fb0227531b08867b7ab4447259fbb1c29efd41e2c1b184/dukpy-0.2.2.tar.gz (2.0MB)\n", "\u001b[K 100% |████████████████████████████████| 2.0MB 320kB/s ta 0:00:01\n", "\u001b[?25hCollecting macropy3==1.1.0b2 (from javascripthon>=0.10; python_version > \"3.4\"->pyecharts-javascripthon==0.0.6->pyecharts==0.5.11)\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/8c/b7/a61f9d9bd7de80ca51ef362db5bb6434ea21484c907d2a5f396d97b0274c/macropy3-1.1.0b2.tar.gz (44kB)\n", "\u001b[K 100% |████████████████████████████████| 51kB 377kB/s ta 0:00:011\n", "\u001b[?25hBuilding wheels for collected packages: jupyter-echarts-pypkg, javascripthon, dukpy, macropy3\n", " Building wheel for jupyter-echarts-pypkg (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /Users/liangyun/Library/Caches/pip/wheels/62/5c/66/e79276e15e377da9854521c19718ac5a23d827a1b554cf0091\n", " Building wheel for javascripthon (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /Users/liangyun/Library/Caches/pip/wheels/f0/a0/6e/5cac90791fefe9c2961f9d5b2fc9b71da8b9b2f927f4012918\n", " Building wheel for dukpy (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /Users/liangyun/Library/Caches/pip/wheels/75/36/c3/6b6172234b2926013669c9bfead9e2147113a3134ff0719d52\n", " Building wheel for macropy3 (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /Users/liangyun/Library/Caches/pip/wheels/ef/ea/95/65744a6a720d5b0b57779da564c68a3e1b221ea998542a78b3\n", "Successfully built jupyter-echarts-pypkg javascripthon dukpy macropy3\n", "Installing collected packages: lml, jupyter-echarts-pypkg, dukpy, macropy3, javascripthon, pyecharts-javascripthon, pyecharts\n", "Successfully installed dukpy-0.2.2 javascripthon-0.10 jupyter-echarts-pypkg-0.1.2 lml-0.0.2 macropy3-1.1.0b2 pyecharts-0.5.11 pyecharts-javascripthon-0.0.6\n", "Collecting pyecharts_snapshot\n", " Downloading https://files.pythonhosted.org/packages/85/e8/f0a14cc92d89d43e52efb48bca5053c24b53e06e93b43579071476ed0cc6/pyecharts_snapshot-0.2.0-py2.py3-none-any.whl\n", "Collecting pyppeteer>=0.0.25 (from pyecharts_snapshot)\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/b0/16/a5e8d617994cac605f972523bb25f12e3ff9c30baee29b4a9c50467229d9/pyppeteer-0.0.25.tar.gz (1.2MB)\n", "\u001b[K 100% |████████████████████████████████| 1.2MB 673kB/s ta 0:00:01\n", "\u001b[?25hRequirement already satisfied: pillow in /Users/liangyun/anaconda3/lib/python3.7/site-packages (from pyecharts_snapshot) (5.4.1)\n", "Collecting pyee (from pyppeteer>=0.0.25->pyecharts_snapshot)\n", " Downloading https://files.pythonhosted.org/packages/ad/d8/5608d571ffad3d7de0192b0b3099fe3f38d87c0817ebff3cee19264f0bc2/pyee-6.0.0-py2.py3-none-any.whl\n", "Collecting websockets (from pyppeteer>=0.0.25->pyecharts_snapshot)\n", "\u001b[?25l Downloading https://files.pythonhosted.org/packages/8b/6b/dcbafe10a1b889f3d31bef7048dbfb23973d4b56e8fb47c9158c47fa5643/websockets-8.0.2-cp37-cp37m-macosx_10_6_intel.whl (65kB)\n", "\u001b[K 100% |████████████████████████████████| 71kB 72kB/s ta 0:00:011\n", "\u001b[?25hCollecting appdirs (from pyppeteer>=0.0.25->pyecharts_snapshot)\n", " Downloading https://files.pythonhosted.org/packages/56/eb/810e700ed1349edde4cbdc1b2a21e28cdf115f9faf263f6bbf8447c1abf3/appdirs-1.4.3-py2.py3-none-any.whl\n", "Requirement already satisfied: urllib3 in /Users/liangyun/anaconda3/lib/python3.7/site-packages (from pyppeteer>=0.0.25->pyecharts_snapshot) (1.24.1)\n", "Requirement already satisfied: tqdm in /Users/liangyun/anaconda3/lib/python3.7/site-packages (from pyppeteer>=0.0.25->pyecharts_snapshot) (4.31.1)\n", "Building wheels for collected packages: pyppeteer\n", " Building wheel for pyppeteer (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /Users/liangyun/Library/Caches/pip/wheels/34/e0/5d/070e22eceecf7ecd5fa4b86bbc18c1c7d0b90e02e9b57f35eb\n", "Successfully built pyppeteer\n", "Installing collected packages: pyee, websockets, appdirs, pyppeteer, pyecharts-snapshot\n", "Successfully installed appdirs-1.4.3 pyecharts-snapshot-0.2.0 pyee-6.0.0 pyppeteer-0.0.25 websockets-8.0.2\n" ] } ], "source": [ "# 安装pyecharts\n", "!pip install pyecharts==0.5.11\n", "\n", "# pyecharts_snapshot 提供图片导出功能\n", "!pip install pyecharts_snapshot " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 一,基本图表" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "pyecharts支持30多种不同的图表。我们只对其中最常用的几种类型进行基本范例的介绍。\n", "\n", "下面是pyecharts主要类的继承关系。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](data/class-relationship-diagram.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**1,柱形图示范**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "柱形图适合表现几组数据之间的对比关系,柱形图的数据的数量一般不宜太多。" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "