{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6.1 散点图/气泡图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.faker import Faker"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 散点图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引入相关包\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Scatter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备数据\n",
    "data = [\n",
    "    [10.0, 8.04],\n",
    "    [8.0, 6.95],\n",
    "    [13.0, 7.58],\n",
    "    [9.0, 8.81],\n",
    "    [11.0, 8.33],\n",
    "    [14.0, 9.96],\n",
    "    [6.0, 7.24],\n",
    "    [4.0, 4.26],\n",
    "    [12.0, 10.84],\n",
    "    [7.0, 4.82],\n",
    "    [5.0, 5.68],\n",
    "]\n",
    "\n",
    "# data.sort(key=lambda x: x[0])\n",
    "x_data = [d[0] for d in data]\n",
    "y_data = [d[1] for d in data]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(\n",
    "    Scatter()\n",
    "    .add_xaxis(x_data)\n",
    "    .add_yaxis('', y_data, symbol_size=30)\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='基本散点图'),\n",
    "        xaxis_opts=opts.AxisOpts(type_=\"value\"),\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 气泡图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(\n",
    "    Scatter()\n",
    "    .add_xaxis(Faker.days_attrs)\n",
    "    .add_yaxis('', Faker.days_values, label_opts=opts.LabelOpts(is_show=False))\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='基本气泡图'),\n",
    "        visualmap_opts=opts.VisualMapOpts(type_=\"size\", max_=35, min_=0)\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(\n",
    "    Scatter()\n",
    "    .add_xaxis(Faker.choose())\n",
    "    .add_yaxis(\"组A\", Faker.values())\n",
    "    .add_yaxis(\"组B\", Faker.values())\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"多组气泡图\"),\n",
    "        visualmap_opts=opts.VisualMapOpts(type_=\"size\", max_=150, min_=20),\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6.2 热力图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成二维数据\n",
    "import random\n",
    "\n",
    "value = [\n",
    "    [i, j, random.randint(0, 50)] \n",
    "        for i in range(24) \n",
    "            for j in range(7)\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import HeatMap\n",
    "\n",
    "(\n",
    "    HeatMap()\n",
    "    .add_xaxis(Faker.clock)\n",
    "    .add_yaxis(\n",
    "        '',\n",
    "        Faker.week,\n",
    "        value,\n",
    "        label_opts=opts.LabelOpts(is_show=True, position=\"inside\")\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"热力图\"),\n",
    "        visualmap_opts=opts.VisualMapOpts(min_=0, max_=50)\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6.3 其他相关性图表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 特效散点图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import EffectScatter\n",
    "from pyecharts.globals import SymbolType\n",
    "\n",
    "(\n",
    "    EffectScatter()\n",
    "    .add_xaxis(Faker.choose())\n",
    "    .add_yaxis(\"\", \n",
    "               Faker.values(), \n",
    "               symbol=SymbolType.ARROW\n",
    "      )\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title='特效散点图'))\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 桑基图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备数据\n",
    "nodes = [\n",
    "    {\"name\": \"category1\"},\n",
    "    {\"name\": \"category2\"},\n",
    "    {\"name\": \"category3\"},\n",
    "    {\"name\": \"category4\"},\n",
    "    {\"name\": \"category5\"},\n",
    "    {\"name\": \"category6\"},\n",
    "]\n",
    "\n",
    "links = [\n",
    "    {\"source\": \"category1\", \"target\": \"category2\", \"value\": 10},\n",
    "    {\"source\": \"category2\", \"target\": \"category3\", \"value\": 15},\n",
    "    {\"source\": \"category3\", \"target\": \"category4\", \"value\": 20},\n",
    "    {\"source\": \"category5\", \"target\": \"category6\", \"value\": 25},\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Sankey\n",
    "\n",
    "(\n",
    "    Sankey()\n",
    "    .add(\n",
    "        \"sankey\",\n",
    "        nodes,\n",
    "        links,\n",
    "        linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color=\"source\"),\n",
    "        label_opts=opts.LabelOpts(position=\"right\"),\n",
    "    )\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"桑基图\"))\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 树状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据准备\n",
    "tree_data = [\n",
    "    {\n",
    "        # 节点A\n",
    "        \"name\": \"A\",  \n",
    "        # 节点A的子节点\n",
    "        \"children\": [\n",
    "            # 节点A.B\n",
    "            {\"name\": \"B\"},\n",
    "            # 节点A.C\n",
    "            {\n",
    "                \"name\": \"C\",\n",
    "                # 节点A.C的子节点\n",
    "                \"children\": [\n",
    "                    # 节点A.C.E\n",
    "                    {\"name\": \"E\"}, \n",
    "                    # 节点A.C.F\n",
    "                    {\"name\": \"F\"},\n",
    "                ]\n",
    "            },\n",
    "            {\n",
    "                # 节点A.D\n",
    "                \"name\": \"D\",\n",
    "                # 节点A.D子节点\n",
    "                \"children\": [\n",
    "                    # 节点A.D.G\n",
    "                    {\"name\": \"G\"}\n",
    "                ],\n",
    "                \n",
    "            },\n",
    "        ],\n",
    "        \n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Tree\n",
    "\n",
    "(\n",
    "    Tree()\n",
    "    .add(\"\", tree_data)\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"树状图\"))\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "# 加载树形数据\n",
    "with open(\"flare.json\", \"r\", encoding=\"utf-8\") as f:\n",
    "    j = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(\n",
    "    Tree()\n",
    "    .add(\n",
    "        \"\",\n",
    "        [j],\n",
    "#         orient=\"BT\",  # 从下往上\n",
    "        orient=\"TB\",  # 从上往下\n",
    "#         orient=\"LR\",  # 从左往右\n",
    "#         orient=\"RL\",  # 从右往左\n",
    "        label_opts=opts.LabelOpts(\n",
    "            horizontal_align=\"right\",\n",
    "            vertical_align=\"middle\",\n",
    "            rotate=-90,\n",
    "        ),\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "data_anal_env",
   "language": "python",
   "name": "data_anal_env"
  },
  "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
