{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 8.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 Boxplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备数据\n",
    "v = [[850, 740, 900, 1070, 930, 860, 950, 980, 990, 880]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(v[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用numpy计算关键值\n",
    "import numpy as np\n",
    "\n",
    "sorted_v = sorted(v[0])\n",
    "print('排序', sorted_v)\n",
    "print('min:', np.min(v))\n",
    "print('max:', np.max(v))\n",
    "print('median:', np.median(v))\n",
    "print('Q1:', np.quantile(v, q=1/4))\n",
    "print('Q3:', np.quantile(v, q=3/4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "boxplot = Boxplot()\n",
    "# 使用prepare_data计算关键值\n",
    "boxplot_data = boxplot.prepare_data(v)\n",
    "print(boxplot_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(\n",
    "    Boxplot()\n",
    "    .add_xaxis(['测试数据'])\n",
    "    .add_yaxis('', boxplot_data)\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": [
    "import numpy as np\n",
    "\n",
    "a = np.array([22, 87, 5, 43, 56, 73, 55, 54, 11, 20, 51, 5, 79, 31, 27]) \n",
    "hist, bins = np.histogram(a, bins = [0, 20, 40, 60, 80, 100]) \n",
    "\n",
    "print('统计值:', hist)\n",
    "print('分组:', bins)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(hist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Bar\n",
    "\n",
    "(\n",
    "    Bar()\n",
    "    .add_xaxis(['0-20', '20-40', '40-60', '60-80', '80-100'])\n",
    "    .add_yaxis('', hist.tolist(), category_gap=0.2)\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"直方图\"))\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 8.2 3D柱状图 折线图 散点图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3D柱状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "# 生成数据\n",
    "bar3d_data = [(i, j, random.randint(0, 50)) for i in range(7) for j in range(24)]\n",
    "print(bar3d_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Bar3D\n",
    "\n",
    "(\n",
    "    Bar3D()\n",
    "    .add(\n",
    "        \"\",\n",
    "        [[d[1], d[0], d[2]] for d in bar3d_data],\n",
    "        xaxis3d_opts=opts.Axis3DOpts(Faker.clock, type_=\"category\"),\n",
    "        yaxis3d_opts=opts.Axis3DOpts(Faker.week_en, type_=\"category\"),\n",
    "        zaxis3d_opts=opts.Axis3DOpts(type_=\"value\"),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        visualmap_opts=opts.VisualMapOpts(max_=50),\n",
    "        title_opts=opts.TitleOpts(title=\"3D 柱状图\"),\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3D折线图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Line3D\n",
    "\n",
    "(\n",
    "    Line3D()\n",
    "    .add(\n",
    "        \"\",\n",
    "        [[d[1], d[0], d[2]] for d in bar3d_data],\n",
    "        xaxis3d_opts=opts.Axis3DOpts(Faker.clock, type_=\"category\"),\n",
    "        yaxis3d_opts=opts.Axis3DOpts(Faker.week_en, type_=\"category\"),\n",
    "        zaxis3d_opts=opts.Axis3DOpts(type_=\"value\"),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        visualmap_opts=opts.VisualMapOpts(max_=50),\n",
    "        title_opts=opts.TitleOpts(title=\"3D 折线图\"),\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3D 散点图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Scatter3D\n",
    "\n",
    "(\n",
    "    Scatter3D()\n",
    "    .add(\n",
    "        \"\",\n",
    "        [[d[1], d[0], d[2]] for d in bar3d_data],\n",
    "        xaxis3d_opts=opts.Axis3DOpts(Faker.clock, type_=\"category\"),\n",
    "        yaxis3d_opts=opts.Axis3DOpts(Faker.week_en, type_=\"category\"),\n",
    "        zaxis3d_opts=opts.Axis3DOpts(type_=\"value\"),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        visualmap_opts=opts.VisualMapOpts(max_=50),\n",
    "        title_opts=opts.TitleOpts(title=\"3D 散点图\"),\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 其他统计图表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 分组箱型图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据准备\n",
    "\n",
    "# 上午数据\n",
    "v1 = [\n",
    "    [850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980], # A\n",
    "    [960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790],   # B\n",
    "]\n",
    "\n",
    "# 下午数据\n",
    "v2 = [\n",
    "    [890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920],   # A\n",
    "    [890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870],   # B\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = Boxplot()\n",
    "b.add_xaxis([\"A组\", \"B组\"])\n",
    "b.add_yaxis(\"上午\", b.prepare_data(v1))\n",
    "b.add_yaxis(\"下午\", b.prepare_data(v2))\n",
    "b.set_global_opts(title_opts=opts.TitleOpts(title=\"分组箱型图\"))\n",
    "b.render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 叠加图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备数据\n",
    "v1 = [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",
    "v2 = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]  # 温度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Bar, Line\n",
    "\n",
    "bar = (\n",
    "    Bar()\n",
    "    .add_xaxis(Faker.months)\n",
    "    .add_yaxis(\"降水量\", v1)\n",
    "    .extend_axis(\n",
    "        yaxis=opts.AxisOpts(\n",
    "            axislabel_opts=opts.LabelOpts(formatter=\"{value} °C\"), interval=5\n",
    "        )\n",
    "    )\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"叠加图\"),\n",
    "        yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter=\"{value} ml\")),\n",
    "    )\n",
    ")\n",
    "\n",
    "line = (\n",
    "    Line()\n",
    "    .add_xaxis(Faker.months)\n",
    "    .add_yaxis(\"平均温度\", v2, yaxis_index=1)\n",
    ")\n",
    "\n",
    "bar.overlap(line)\n",
    "bar.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
}
