{ "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": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Bar\n", "\n", "x = [\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\"]\n", "y1 = [5, 20, 36, 10, 75]\n", "y2 = [10, 25, 8, 60, 20]\n", "\n", "bar = Bar(title = \"产品月销量\",width = 600,height = 420)\n", "bar.add(name = \"商家A\", x_axis = x, y_axis = y1)\n", "bar.add(name = \"商家B\", x_axis = x, y_axis = y2,is_xaxis_boundarygap =True)\n", "\n", "bar.render('data/柱形图示范.html')\n", "bar" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2,折线图示范**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "折线图适合描述两个变量之间的函数关系,例如常用它来描述一个变量随时间的变化趋势。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Line\n", "\n", "x = ['2018-{:0>2d}'.format(s) for s in range(1,13)]\n", "y1 = [5,10,26,30,35,30,20,26,40,46,40,50]\n", "y2 = [8,20,24,36,40,36,40,45,50,53,48,58]\n", "\n", "line = Line(title = \"月销售总额\",width = 600,height = 420)\n", "\n", "line.add(name = \"商家A\", x_axis = x, y_axis = y1,\n", " line_width = 3,line_color = 'red')\n", "line.add(name = \"商家B\", x_axis = x, y_axis = y2,\n", " yaxis_min = 0,yaxis_max = 100,is_xaxis_boundarygap = False,\n", " is_datazoom_show =True,line_width = 2,line_color = 'cyan')\n", "\n", "line.render('data/折线图示范.html')\n", "line" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**3,散点图示范**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "散点图适合表现大量样本的多个属性的分布规律。散点图的每个点表示一个样本,每个坐标维度表示一个属性。\n", "\n", "当样本属性维度多于2个时,可以使用点的颜色或大小等方式来表达更多属性维度。" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Scatter\n", "import pandas as pd \n", "\n", "dfboy = pd.DataFrame()\n", "dfboy['weight'] = [56,67,65,70,57,60,80,85,76,64]\n", "dfboy['height'] = [162,170,168,172,168,172,180,176,178,170]\n", "\n", "dfgirl = pd.DataFrame()\n", "dfgirl['weight'] = [50,62,60,70,57,45,62,65,70,56]\n", "dfgirl['height'] = [155,162,165,170,166,158,160,170,172,165]\n", "\n", "scatter = Scatter(title = \"体格数据\",width = 600,height = 420)\n", "scatter.add(name = \"boy\", x_axis = dfboy['weight'], y_axis = dfboy['height'])\n", "scatter.add(name = \"girl\", x_axis = dfgirl['weight'], y_axis = dfgirl['height'],\n", " yaxis_min = 130,yaxis_max = 200,xaxis_min = 30,xaxis_max = 100)\n", "\n", "scatter.render(\"data/散点图示范.html\")\n", "\n", "scatter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "以下示范3个维度数据的散点图展示" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Scatter\n", "import pandas as pd \n", "\n", "def custom_formatter(params):\n", " return (params.value[3] + ':' +\n", " str(params.value[0]) +','\n", " +str(params.value[1]) + ','\n", " +str(params.value[2]))\n", "\n", "df = pd.DataFrame()\n", "df['country'] = [\"中国\",'美国','德国','法国','英国','日本','俄罗斯','印度','澳大利亚','加拿大']\n", "df['life-expectancy'] = [76.9,79.1,81.1,81.9,81.4,83.5,73.13,66.8,81.8,81.7]\n", "df['capita-gdp'] = [13334,53354,44053,37599,38225,36162,23038,5903,44056,43294]\n", "df['population'] = [1376048943,321773631,80688545,64395345,64715810,126573481,143456918,\n", " 1311050527,23968973,35939927]\n", "\n", "scatter = Scatter(title = \"各国发展水平\",width = 600,height = 420)\n", "scatter.add(name = '',\n", " x_axis = df['capita-gdp'], # params.values[0]\n", " y_axis = df['life-expectancy'], # params.values[1]\n", " extra_data = df['population'].values.tolist(), # params.values[2]\n", " extra_name = df['country'].values.tolist(), # params.values[3]\n", " tooltip_formatter=custom_formatter, #自定义提示框格式内容\n", " is_visualmap=True, # 显示\n", " visual_orient=\"horizontal\",\n", " visual_type = 'size', #可以是size或者color\n", " visual_dimension=2,\n", " visual_range=[20000000, 1500000000],\n", " )\n", "scatter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**4,箱型图示范**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "箱型图适合表现一组数据的统计分布规律,它能显示出一组数据的最大值、最小值、中位数、及上下四分位数。\n", "\n", "箱型图的进阶版本是小提琴图,可以展示数据的密度估计曲线,可以用seaborn画出。" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Boxplot\n", "\n", "x =['1班','2班','3班','4班']\n", "y1=[78, 98, 56, 78, 90.0, 45, 78, 20, 87, 86, 74, 89, 94]\n", "y2=[89, 82, 45, 67, 68, 78.0, 79, 98, 71, 56, 78, 81, 80]\n", "y3=[90, 80, 60, 89, 76, 73.0, 72, 92, 89, 87, 65, 66, 76]\n", "y4=[82, 72, 55, 100, 90.0, 78, 69, 67, 87, 66, 78, 71, 82]\n", "\n", "box = Boxplot(title = '考试成绩箱型图',width = 600,height = 420)\n", "\n", "# 预处理数据计算最大值,最小值,中位数以及上下四分位数\n", "y_prepared = box.prepare_data([y1,y2,y3,y4]) \n", "box.add(name = '',x_axis = x,y_axis = y_prepared)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "附:用seaborn 进行小提琴图的绘制" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "import seaborn as sns\n", "%matplotlib inline\n", "%config InlineBackend.figure_format = 'svg'" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "#设置风格\n", "sns.set(style=\"white\", context=\"notebook\")\n", "#处理中文问题\n", "sns.set_style({'font.sans-serif':['simhei', 'Arial']}) " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "dfdata = pd.DataFrame()\n", "dfdata['score'] = y1 + y2 + y3 + y4\n", "dfdata['class'] = ['class1']*len(y1)+['class2']*len(y2)+['class3']*len(y3)+['class4']*len(y4)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ax = sns.violinplot(x= 'class', y = 'score',data = dfdata,\n", " palette = 'hls', # 设置调色板\n", " inner = 'box', # 设置内部显示类型 → “box”, “quartile”, “point”, “stick”, None\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**5,词云图示范**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "词云图适合表现不同关键词的出现频率或重要性程度。" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import WordCloud\n", "\n", "words = ['python','jupyter','numpy','pandas','matplotlib','sklearn',\n", " 'xgboost','lightGBM','simpy','keras','tensorflow',\n", " 'hive','hadoop','spark']\n", "counts = [100,90,65,95,50,60,70,70,20,70,80,80,60,60]\n", "\n", "cloud = WordCloud(title = '数据算法常用工具',width = 600,height = 420)\n", "cloud.add(name = 'utils',attr = words,value = counts,\n", " shape = \"circle\",word_size_range = (10,70))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 二,图表配置" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果要对绘制的图表进行诸如图像尺寸,坐标轴刻度,线的颜色,以及网格线等各个细节方面的修改,我们则需要手动对图表进行配置,而不是依赖于默认配置。\n", "\n", "图表配置有3种主要方式:\n", "\n", "1,修改图表主题风格:利用configure或use_theme指定图表主题风格,对图表整体颜色风格产生影响。 \n", "\n", "2,初始化图表通用属性:创建图表时指定图表height,title等属性,对图像尺寸和标题等通用属性产生影响。\n", "\n", "3,配置特定元素属性:使用add给图表配置xyAxis,datazoom,lineStyle等特定元素属性。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**1,修改图表主题风格**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以使用use_theme修改单个图表主题,也可以用configure修改全局图表风格。" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 默认light主题效果\n", "import random\n", "from pyecharts import Bar\n", "\n", "X_AXIS = [\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"]\n", "bar = Bar(\"light主题效果\", \"这里是副标题\")\n", "bar.add(\"商家A\", X_AXIS, [random.randint(10, 100) for _ in range(6)])\n", "bar.add(\"商家B\", X_AXIS, [random.randint(10, 100) for _ in range(6)])\n", "bar.add(\"商家C\", X_AXIS, [random.randint(10, 100) for _ in range(6)])\n", "bar.add(\"商家D\", X_AXIS, [random.randint(10, 100) for _ in range(6)])\n", "bar" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 使用dark主题\n", "import random\n", "from pyecharts import Bar\n", "\n", "\n", "X_AXIS = [\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"]\n", "bar = Bar(\"dark主题展示\", \"这里是副标题\")\n", "bar.use_theme(\"dark\")\n", "bar.add(\"商家A\", X_AXIS, [random.randint(10, 100) for _ in range(6)])\n", "bar.add(\"商家B\", X_AXIS, [random.randint(10, 100) for _ in range(6)])\n", "bar.add(\"商家C\", X_AXIS, [random.randint(10, 100) for _ in range(6)])\n", "bar.add(\"商家D\", X_AXIS, [random.randint(10, 100) for _ in range(6)])\n", "bar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果要设置某个主题风格应用到所有图表,可以在绘图开始前用configure进行设置。" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "from pyecharts import configure\n", "# 将这行代码置于首部\n", "configure(global_theme='light')" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Pie\n", "attr = [\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"]\n", "value = [11, 12, 13, 10, 10, 10]\n", "pie = Pie(\"销售额占比\",width = 600,height = 420)\n", "pie.add(\"\", attr, value, is_label_show=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2,初始化图表通用属性**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "图表通用属性指的是图表的title,subtitle,height,width,title_pos,title_color,title_text_size,background_color等属性,这些属性对所有类型的图表都适用。\n", "\n", "所有通用属性设置方式详细参见:https://github.com/lyhue1991/pyecharts/blob/master/docs/zh-cn/charts_configure.md" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Pie\n", "attr = [\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"]\n", "value = [11, 12, 13, 10, 10, 10]\n", "\n", "# 初始化图表通用属性\n", "pie = Pie(title = \"销售额占比\",\n", " title_pos = 'center', # 标题居中\n", " title_top = 'bottom', # 标题在底部\n", " title_color = '#0000ff', # 标题颜色设置为蓝色,256位rgb格式\n", " background_color = \"#aee\", # 设置背景颜色,16位rgb格式\n", " width = 600,height = 420)\n", "\n", "pie.add(\"\", attr, value, is_label_show=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**3,配置特定元素属性**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "特定元素属性包括xyAxis,dataZoom,lineStyle,markLine-markPoint,visualMap等元素属性,这些元素只在某些种类的图表中适用。例如xyAxis只在Line、Bar、Scatter、EffectScatter、Kline这几类图表中适用。\n", "\n", "所有特定元素属性设置方式详细参见:https://github.com/lyhue1991/pyecharts/blob/master/docs/zh-cn/charts_configure.md" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 设置xyAxis示范\n", "\n", "from pyecharts import Line\n", "\n", "x = ['2018-{:0>2d}'.format(s) for s in range(1,13)]\n", "y1 = [5,10,26,30,35,30,20,26,40,46,40,50]\n", "y2 = [8,20,24,36,40,36,40,45,50,53,48,58]\n", "\n", "line = Line(title = \"月销售总额\",width = 600,height = 420)\n", "\n", "line.add(name = \"商家A\", x_axis = x, y_axis = y1)\n", "line.add(name = \"商家B\", x_axis = x, y_axis = y2,\n", " #\n", " #=====设置xyAxis=====\n", " yaxis_min = 0,yaxis_max = 100, # 设置y坐标轴刻度范围\n", " xaxis_name = '月份', yaxis_name = '销售额', #x轴名称,y轴名称\n", " xaxis_name_gap = 40, # x轴名称与轴距离\n", " xaxis_rotate = 30, # x轴刻度旋转角度\n", " is_splitline_show = True, # 显示y轴网格线\n", " is_xaxislabel_align = True # x轴刻度和标签是否对齐\n", " )\n", "line" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 设置dataZoom示范\n", "\n", "from pyecharts import Line\n", "\n", "x = ['2018-{:0>2d}'.format(s) for s in range(1,13)]\n", "y1 = [5,10,26,30,35,30,20,26,40,46,40,50]\n", "y2 = [8,20,24,36,40,36,40,45,50,53,48,58]\n", "\n", "line = Line(title = \"月销售总额\",width = 600,height = 420)\n", "\n", "line.add(name = \"商家A\", x_axis = x, y_axis = y1)\n", "line.add(name = \"商家B\", x_axis = x, y_axis = y2,\n", " #\n", " #=====设置xyAxis=====\n", " is_xaxis_boundarygap = False, # x坐标刻度对准数据,而不是作为分类边界\n", " #\n", " #=====设置dataZoom=====\n", " is_datazoom_show = True, #显示 dataZoom控制条\n", " datazoom_type = 'both' # 可以是slider,inside或both\n", " #\n", " #\n", " )\n", "line" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 设置lineStyle示范\n", "\n", "from pyecharts import Line\n", "\n", "x = ['2018-{:0>2d}'.format(s) for s in range(1,13)]\n", "y1 = [5,10,26,30,35,30,20,26,40,46,40,50]\n", "y2 = [8,20,24,36,40,36,40,45,50,53,48,58]\n", "\n", "line = Line(title = \"月销售总额\",width = 600,height = 420)\n", "\n", "line.add(name = \"商家A\", x_axis = x, y_axis = y1,\n", " #\n", " #=====设置lineStyle=====\n", " line_width = 2,\n", " line_opacity = 0.5, # 透明度\n", " line_color = 'red'\n", " )\n", "line.add(name = \"商家B\", x_axis = x, y_axis = y2,\n", " #\n", " #=====设置xyAxis=====\n", " is_xaxis_boundarygap = False, # x坐标刻度对准数据,而不是作为分类边界\n", " #\n", " #=====设置dataZoom=====\n", " is_datazoom_show = True, #显示 dataZoom控制条\n", " #\n", " #=====设置lineStyle=====\n", " line_width = 3,\n", " line_color = '#11ffbb',\n", " line_type = 'dashed', # 线型,可以是solid,dashed,或者dotted\n", " )\n", "line" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 设置markPoint和markLine示范\n", "\n", "from pyecharts import Line\n", "\n", "x = ['2018-{:0>2d}'.format(s) for s in range(1,13)]\n", "y1 = [5,10,26,30,35,30,20,26,40,46,40,50]\n", "y2 = [8,20,24,36,40,36,40,45,50,53,48,58]\n", "\n", "line = Line(title = \"月销售总额\",width = 600,height = 420)\n", "\n", "line.add(name = \"商家A\", x_axis = x, y_axis = y1,\n", " #\n", " #=====设置lineStyle=====\n", " line_width = 2,\n", " line_opacity = 0.5, # 透明度\n", " line_color = 'red',\n", " #\n", " #=====设置markPoint&markLine=====\n", " mark_point = ['min','max'], #标记点\n", " mark_line = ['average'] #标记线\n", " )\n", "line.add(name = \"商家B\", x_axis = x, y_axis = y2,\n", " #\n", " #=====设置xyAxis=====\n", " is_xaxis_boundarygap = False, # x坐标刻度对准数据,而不是作为分类边界\n", " yaxis_min = 0,yaxis_max = 100, # 设置y坐标轴刻度范围\n", " #\n", " #=====设置dataZoom=====\n", " is_datazoom_show = True, #显示 dataZoom控制条\n", " #\n", " #=====设置lineStyle=====\n", " line_width = 3,\n", " line_type = 'dashed', # 线型,可以是solid,dashed,或者dotted\n", " \n", " #=====设置markPoint&markLine=====\n", " mark_point = [{\"coord\": ['2018-09', 60], \"name\": \"2018/09销售目标\"}, \n", " {\"coord\": ['2018-11', 80], \"name\": \"2018/10销售目标\"}] # 自定义标记点\n", " )\n", "line" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果需要反复使用相同的配置,可以使用Style类简化这个过程。" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Pie,Style\n", "\n", "pie = Pie('各类电影中\"好片\"所占的比例', \"数据来自豆瓣\", title_pos='center')\n", "style = Style()\n", "pie_style = style.add(\n", " label_pos=\"center\",\n", " is_label_show=True,\n", " label_text_color=None\n", ")\n", "\n", "pie.add(\n", " \"\", [\"剧情\", \"\"], [25, 75], center=[10, 30], radius=[18, 24], **pie_style\n", ")\n", "pie.add(\n", " \"\", [\"奇幻\", \"\"], [24, 76], center=[30, 30], radius=[18, 24], **pie_style\n", ")\n", "pie.add(\n", " \"\", [\"爱情\", \"\"], [14, 86], center=[50, 30], radius=[18, 24], **pie_style\n", ")\n", "pie.add(\n", " \"\", [\"惊悚\", \"\"], [11, 89], center=[70, 30], radius=[18, 24], **pie_style\n", ")\n", "pie.add(\n", " \"\", [\"冒险\", \"\"], [27, 73], center=[90, 30], radius=[18, 24], **pie_style\n", ")\n", "pie.add(\n", " \"\", [\"动作\", \"\"], [15, 85], center=[10, 70], radius=[18, 24], **pie_style\n", ")\n", "pie.add(\n", " \"\", [\"喜剧\", \"\"], [54, 46], center=[30, 70], radius=[18, 24], **pie_style\n", ")\n", "pie.add(\n", " \"\", [\"科幻\", \"\"], [26, 74], center=[50, 70], radius=[18, 24], **pie_style\n", ")\n", "pie.add(\n", " \"\", [\"悬疑\", \"\"], [25, 75], center=[70, 70], radius=[18, 24], **pie_style\n", ")\n", "pie.add(\n", " \"\", [\"犯罪\", \"\"],[28, 72],center=[90, 70], radius=[18, 24], legend_top=\"bottom\", \n", " **pie_style\n", ")\n", "pie" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 三,图表组合" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果要把多张图作为子图绘制在一张图中,或者要把柱状图和折线图这样不同类型的图表叠加在一起的时候,我们需要使用对基本图表进行一些组合来实现更加丰富的呈现效果。\n", "\n", "在pyecharts 中 对图表进行组合的方式主要有:Grid, Overlap, Page, Timeline这四种方式。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**1,Grid图表组合**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Grid图表组合可以并行显示多张图,类似子图的作用。Grid中的子图可以是Overlap。" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Bar, Line, Grid\n", "\n", "x = [\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"]\n", "y1 = [5, 20, 36, 10, 75, 90]\n", "y2 = [10, 25, 8, 60, 20, 80]\n", "bar = Bar(\"柱状图示例\", height=720)\n", "bar.add(\"商家A\", x, y1, is_stack=True)\n", "bar.add(\"商家B\", x, y2, is_stack=True)\n", "\n", "\n", "line = Line(\"折线图示例\", title_top=\"50%\")\n", "x = [\"周一\", \"周二\", \"周三\", \"周四\", \"周五\", \"周六\", \"周日\"]\n", "line.add(\n", " \"最高气温\",\n", " x,\n", " [11, 11, 15, 13, 12, 13, 10],\n", " mark_point=[\"max\", \"min\"],\n", " mark_line=[\"average\"],\n", ")\n", "line.add(\n", " \"最低气温\",\n", " x,\n", " [1, -2, 2, 5, 3, 2, 0],\n", " mark_point=[\"max\", \"min\"],\n", " mark_line=[\"average\"],\n", " legend_top=\"50%\",\n", ")\n", "\n", "grid = Grid()\n", "#利用grid_bottom,grid_top,grid_left,grid_right四个参数控制子图的相对位置\n", "grid.add(bar, grid_bottom=\"60%\") \n", "grid.add(line, grid_top=\"60%\")\n", "grid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "利用Grid解决dataZoom与X轴标签重叠的问题" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Bar, Grid\n", "\n", "x = [\n", " \"名字很长的x轴1\",\n", " \"名字很长的x轴2\",\n", " \"名字很长的x轴3\",\n", " \"名字很长的x轴4\",\n", " \"名字很长的x轴5\",\n", " \"名字很长的x轴6\",\n", " \"名字很长的x轴7\",\n", " \"名字很长的x轴8\",\n", " \"名字很长的x轴9\",\n", "]\n", "y = [10, 20, 30, 40, 50, 60, 70, 80, 90]\n", "\n", "grid = Grid()\n", "bar = Bar(\"利用 Grid 解决 dataZoom 与 X 轴标签重叠问题\")\n", "bar.add(\"\", x, y, is_datazoom_show=True, xaxis_interval=0, xaxis_rotate=30)\n", "\n", "# 把 bar 加入到 grid 中,并适当调整 grid_bottom 参数,使 bar 图整体上移\n", "grid.add(bar, grid_bottom=\"25%\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2,Overlap图表组合**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Overlap图表组合可以将不同类型的图表画在同一张图上。" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Bar, Line, Overlap\n", "\n", "attr = ['A', 'B', 'C', 'D', 'E', 'F']\n", "v1 = [10, 20, 30, 40, 50, 60]\n", "v2 = [38, 28, 58, 48, 78, 68]\n", "bar = Bar(\"Line - Bar 示例\")\n", "bar.add(\"bar\", attr, v1)\n", "line = Line()\n", "line.add(\"line\", attr, v2)\n", "\n", "overlap = Overlap()\n", "overlap.add(bar)\n", "overlap.add(line)\n", "overlap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Overlap显示双坐标轴" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Line, Bar, Overlap\n", "\n", "attr = [\"{}月\".format(i) for i in range(1, 13)]\n", "v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]\n", "v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]\n", "v3 = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]\n", "\n", "bar = Bar()\n", "bar.add(\"蒸发量\", attr, v1)\n", "bar.add(\"降水量\", attr, v2, yaxis_formatter=\" ml\",\n", " yaxis_interval=50, yaxis_max=250)\n", "\n", "line = Line()\n", "line.add(\"平均温度\", attr, v3, yaxis_formatter=\" °C\", yaxis_interval=5)\n", "\n", "overlap = Overlap(width=800, height=500)\n", "# 默认不新增 x y 轴,并且 x y 轴的索引都为 0\n", "overlap.add(bar)\n", "# 新增一个 y 轴,此时 y 轴的数量为 2,第二个 y 轴的索引为 1(索引从 0 开始),所以设置 yaxis_index = 1\n", "# 由于使用的是同一个 x 轴,所以 x 轴部分不用做出改变\n", "overlap.add(line, yaxis_index=1, is_add_yaxis=True)\n", "overlap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**3,Pages图表组合**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pages可以将多张图表按顺序展示在一张网页中,适合制作图形化报表。Pages中的图表可以是Grid,Overlap或Timeline." ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#coding=utf-8\n", "from __future__ import unicode_literals\n", "from pyecharts import Bar, Scatter3D\n", "from pyecharts import Page\n", "\n", "page = Page() # step 1\n", "\n", "# bar\n", "attr = [\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"]\n", "v1 = [5, 20, 36, 10, 75, 90]\n", "v2 = [10, 25, 8, 60, 20, 80]\n", "bar = Bar(\"柱状图数据堆叠示例\")\n", "bar.add(\"商家A\", attr, v1, is_stack=True)\n", "bar.add(\"商家B\", attr, v2, is_stack=True)\n", "page.add(bar) # step 2\n", "\n", "# scatter3D\n", "import random\n", "data = [\n", " [random.randint(0, 100),\n", " random.randint(0, 100),\n", " random.randint(0, 100)] for _ in range(80)\n", "]\n", "range_color = [\n", " '#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffbf',\n", " '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']\n", "scatter3D = Scatter3D(\"3D 散点图示例\", width=800, height=600)\n", "scatter3D.add(\"\", data, is_visualmap=True, visual_range_color=range_color)\n", "page.add(scatter3D) # step 2\n", "\n", "page # step 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**4,Timeline图表组合**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Timeline可以将多个图表制作成动画。" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyecharts import Bar, Timeline\n", "from random import randint\n", "\n", "attr = [\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"]\n", "bar_1 = Bar(\"2012 年销量\", \"数据纯属虚构\")\n", "bar_1.add(\"春季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_1.add(\"夏季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_1.add(\"秋季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_1.add(\"冬季\", attr, [randint(10, 100) for _ in range(6)])\n", "\n", "bar_2 = Bar(\"2013 年销量\", \"数据纯属虚构\")\n", "bar_2.add(\"春季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_2.add(\"夏季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_2.add(\"秋季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_2.add(\"冬季\", attr, [randint(10, 100) for _ in range(6)])\n", "\n", "bar_3 = Bar(\"2014 年销量\", \"数据纯属虚构\")\n", "bar_3.add(\"春季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_3.add(\"夏季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_3.add(\"秋季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_3.add(\"冬季\", attr, [randint(10, 100) for _ in range(6)])\n", "\n", "bar_4 = Bar(\"2015 年销量\", \"数据纯属虚构\")\n", "bar_4.add(\"春季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_4.add(\"夏季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_4.add(\"秋季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_4.add(\"冬季\", attr, [randint(10, 100) for _ in range(6)])\n", "\n", "bar_5 = Bar(\"2016 年销量\", \"数据纯属虚构\")\n", "bar_5.add(\"春季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_5.add(\"夏季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_5.add(\"秋季\", attr, [randint(10, 100) for _ in range(6)])\n", "bar_5.add(\"冬季\", attr, [randint(10, 100) for _ in range(6)], is_legend_show=True)\n", "\n", "timeline = Timeline(is_auto_play=True,\n", " timeline_bottom=0,\n", " timeline_play_interval=800 # 每800ms播放一张\n", " )\n", "\n", "timeline.add(bar_1, '2012 年')\n", "timeline.add(bar_2, '2013 年')\n", "timeline.add(bar_3, '2014 年')\n", "timeline.add(bar_4, '2015 年')\n", "timeline.add(bar_5, '2016 年')\n", "timeline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }