1247 lines
35 KiB
Plaintext
1247 lines
35 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "89fbe897-855c-4208-848b-b411727c8eb9",
|
||
"metadata": {},
|
||
"source": [
|
||
"## 数据可视化详解"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "b5b31dba-6894-4c28-83d6-25bade7f28ed",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"import pandas as pd\n",
|
||
"import matplotlib.pyplot as plt\n",
|
||
"\n",
|
||
"plt.rcParams['font.sans-serif'].insert(0, 'SimHei')\n",
|
||
"plt.rcParams['axes.unicode_minus'] = False\n",
|
||
"get_ipython().run_line_magic('config', \"InlineBackend.figure_format = 'svg'\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "ef91deaa-1007-465c-aa39-4b5198a57d84",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import warnings\n",
|
||
"\n",
|
||
"warnings.filterwarnings('ignore')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "cd7bef85-c7a9-4dad-822b-23bc36922deb",
|
||
"metadata": {},
|
||
"source": [
|
||
"### matplotlib\n",
|
||
"\n",
|
||
"整体架构:\n",
|
||
"1. 渲染层 - 底层的画布,图像的渲染,事件交互\n",
|
||
"2. 组件层 - 各种各样的统计图表\n",
|
||
"3. 脚本层 - 提供编程接口,通过调函数实现图表绘制\n",
|
||
"\n",
|
||
"绘图过程:\n",
|
||
"1. 创建画布 - plt.figure(figsize, dpi) --> Figure\n",
|
||
"2. 创建坐标系 - plt.subplot(nrows, ncols, index)\n",
|
||
"3. 绘制图表\n",
|
||
" - 折线图:plt.plot()\n",
|
||
" - 散点图:plt.scatter()\n",
|
||
" - 柱状图:plt.bar() / plt.barh()\n",
|
||
" - 饼图:plt.pie()\n",
|
||
" - 直方图:plt.hist()\n",
|
||
" - 箱线图:plt.boxplot()\n",
|
||
"4. 保存图表 - plt.savefig()\n",
|
||
"5. 显示图表 - plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "73435be1-71e3-49e7-8703-7479be7f2b29",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"x = np.linspace(-2 * np.pi, 2 * np.pi, 120)\n",
|
||
"y1 = np.sin(x)\n",
|
||
"y2 = np.cos(x)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "1c57ddb9-5338-4008-8878-901f92cf94ba",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# %pip install PyQt5\n",
|
||
"# %pip install PyQt6"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "6ba631ea-11ed-48d3-b06b-5efd93222f07",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 魔法指令 - 将统计图表渲染到Qt窗口\n",
|
||
"# %matplotlib qt"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "4c8fdb5f-18be-4ed5-826a-6b2466630154",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"plt.figure(figsize=(8, 4), dpi=200)\n",
|
||
"plt.subplot(1, 1, 1)\n",
|
||
"plt.plot(x, y1, label='正弦', linewidth=0.5, linestyle='--', color='#D75281')\n",
|
||
"plt.plot(x, y2, label='余弦', marker='.', color='#0096FF')\n",
|
||
"plt.legend(loc='lower center')\n",
|
||
"# plt.savefig('aa.jpg')\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "80c166e9-0a24-4593-bfde-ceb592c5949c",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 魔法指令 - 将统计图表渲染到浏览器\n",
|
||
"%matplotlib inline"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "39fbcb43-325c-4878-a23e-ae5d27ae08e2",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 创建画布(Figure)\n",
|
||
"plt.figure(figsize=(8, 4), dpi=200)\n",
|
||
"# 创建坐标系(Axes)\n",
|
||
"ax = plt.subplot(1, 1, 1)\n",
|
||
"ax.spines['left'].set_position('center')\n",
|
||
"ax.spines['bottom'].set_position('center')\n",
|
||
"ax.spines['top'].set_visible(False)\n",
|
||
"ax.spines['right'].set_visible(False)\n",
|
||
"# 绘制折线图\n",
|
||
"plt.plot(x, y1, label='正弦', color='#D75281', linewidth=2, linestyle='-.')\n",
|
||
"plt.plot(x, y2, label='余弦', color='#0096FF', marker='.')\n",
|
||
"# 定制图表的标题\n",
|
||
"plt.title(r'$sin(\\alpha)$和$cos(\\alpha)$曲线图', fontdict=dict(fontsize=18, color='#FFFFFF', backgroundcolor='#0F3D3E'))\n",
|
||
"# 定制横轴的刻度\n",
|
||
"plt.xticks(\n",
|
||
" np.arange(-2 * np.pi, 2 * np.pi + 0.1, np.pi / 2),\n",
|
||
" labels=[r'$ -2\\pi $', r'$ -\\frac{3\\pi}{2} $', r'$ -\\pi $', r'$ -\\frac{\\pi}{2} $', \n",
|
||
" '0', r'$ \\frac{\\pi}{2} $', r'$ \\pi $', r'$ \\frac{3\\pi}{2} $', r'$ 2\\pi $']\n",
|
||
")\n",
|
||
"# 定制纵轴的刻度\n",
|
||
"plt.yticks(np.arange(-1, 1.5, 0.5))\n",
|
||
"# 添加标注(文字和箭头)\n",
|
||
"plt.annotate(r'$ sin(\\alpha) $', xytext=(0.5, -0.5), xy=(0, 0), color='#EF5B0C', \n",
|
||
" arrowprops=dict(arrowstyle='fancy', connectionstyle='arc3, rad=0.25', color='darkgreen'))\n",
|
||
"# 定制图例\n",
|
||
"plt.legend(loc='lower right')\n",
|
||
"# 保存图表\n",
|
||
"# plt.savefig('aa.jpg')\n",
|
||
"# 显示图表\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "e72a3b87-6cf9-4709-abe1-4c0a67d9fe00",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"x2 = np.linspace(0.1, 10.1, 60)\n",
|
||
"y3 = np.log2(x2)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "cf5cecf5-3316-4f60-ae1c-42ed31467827",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 创建画布(Figure)\n",
|
||
"plt.figure(figsize=(8, 6), dpi=200)\n",
|
||
"# 创建坐标系(Axes)\n",
|
||
"plt.subplot(2, 2, 1)\n",
|
||
"# 绘制折线图\n",
|
||
"plt.plot(x, y1, label='正弦', color='#D75281', linewidth=2, linestyle='-.')\n",
|
||
"# 创建坐标系\n",
|
||
"plt.subplot(2, 2, 2)\n",
|
||
"plt.plot(x, y2, label='余弦', color='#0096FF', marker='.')\n",
|
||
"# 创建坐标系\n",
|
||
"# plt.subplot(2, 2, (3, 4))\n",
|
||
"plt.subplot(2, 1, 2)\n",
|
||
"# 绘制散点图\n",
|
||
"plt.plot(x2, y3, color='darkgreen', marker='*')\n",
|
||
"# 显示图表\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "eaf4e484-6821-4e8c-839f-fae932b68224",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 创建画布(Figure)\n",
|
||
"plt.figure(figsize=(8, 6), dpi=200)\n",
|
||
"# 创建网格对象(GridSpec)\n",
|
||
"grid = plt.GridSpec(2, 3)\n",
|
||
"plt.subplot(grid[:, 0])\n",
|
||
"plt.subplot(grid[0, 1:])\n",
|
||
"plt.subplot(grid[1, 1])\n",
|
||
"plt.subplot(grid[1, 2])\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "b1d3b736-da33-479c-9d0c-a84145dd59e1",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 月收入\n",
|
||
"income = np.fromstring('5550, 7500, 10500, 15000, 20000, 25000, 30000, 40000', sep=',')\n",
|
||
"income"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "cedebe7e-58eb-456a-bb64-4e75d1729c92",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 月网购支出\n",
|
||
"outcome = np.fromstring('800, 1800, 1250, 2000, 1800, 2100, 2500, 3500', sep=',')\n",
|
||
"outcome"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "eab6ea54-8c49-46a8-918c-8ce7aebd521e",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"plt.scatter(income, outcome)\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "ef2ee591-6fa9-4b02-9f10-6f2dc776b8fe",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 网购次数\n",
|
||
"nums = np.array([5, 3, 10, 5, 12, 20, 8, 10])\n",
|
||
"nums"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "239a9331-c6a4-450c-bc13-f4e3c2969f2e",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"plt.get_cmap('rainbow_r')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "9b9abf01-b4a8-4b3e-8de4-8284f6fc3efe",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制散点图 ---> 气泡图(引入第三个变量)\n",
|
||
"plt.scatter(income, outcome, s=nums * 30, c=nums, cmap='rainbow_r')\n",
|
||
"plt.colorbar()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "3a36fd15-7779-4dd5-a205-478f1ae1506c",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"data1 = np.random.randint(100, 500, 4)\n",
|
||
"data2 = np.random.randint(200, 600, 4)\n",
|
||
"data3 = np.random.randint(300, 500, 4)\n",
|
||
"quarter = np.arange(4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "fc760daf-de0c-478d-a84b-d5ed0fd5a5e9",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"errs = np.random.randint(10, 30, 4)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "143a0b7b-a3ff-4a76-9032-fd236826966c",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 柱状图\n",
|
||
"plt.bar(quarter-0.2, data1, label='A组', hatch='//', width=0.2)\n",
|
||
"plt.bar(quarter, data2, label='B组', hatch='xxx', width=0.2)\n",
|
||
"plt.bar(quarter+0.2, data3, label='C组', width=0.2, yerr=errs)\n",
|
||
"plt.xticks(quarter, labels=[f'Q{i}' for i in range(1, 5)])\n",
|
||
"plt.legend()\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "a802edea-f2f7-4265-809f-78318d1c1e57",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 堆叠柱状图\n",
|
||
"plt.bar(quarter, data1, label='A组', width=0.4)\n",
|
||
"plt.bar(quarter, data2, label='B组', width=0.4, bottom=data1)\n",
|
||
"plt.bar(quarter, data3, label='C组', width=0.4, bottom=data1 + data2)\n",
|
||
"plt.xticks(quarter, labels=[f'Q{i}' for i in range(1, 5)])\n",
|
||
"for i in range(quarter.size):\n",
|
||
" plt.text(i, data1[i] // 2, data1[i], ha='center', va='center', color='w')\n",
|
||
" plt.text(i, data1[i] + data2[i] // 2, data2[i], ha='center', color='w')\n",
|
||
" plt.text(i, data1[i] + data2[i] + data3[i] // 2, data3[i], ha='center', color='w')\n",
|
||
"plt.legend()\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "5f01c63f-f1da-42af-a04a-e45d95c2262b",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 计算每组数据的占比\n",
|
||
"temp_df = pd.DataFrame(data={\n",
|
||
" 'A组': data1,\n",
|
||
" 'B组': data2,\n",
|
||
" 'C组': data3,\n",
|
||
"})\n",
|
||
"temp_df"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "ccb6a3f1-ae06-467d-9cc0-00b3eb1b9b84",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"pct_data = temp_df.apply(lambda x: x / temp_df.sum(axis=1))\n",
|
||
"pct_data"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "0ec0bf97-62ed-4ec7-8998-b1277d752098",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import matplotlib.ticker as tkr\n",
|
||
"\n",
|
||
"# 绘制百分比堆叠柱状图\n",
|
||
"data1, data2, data3 = pct_data.A组, pct_data.B组, pct_data.C组\n",
|
||
"plt.bar(quarter, data1, label='A组', width=0.4)\n",
|
||
"plt.bar(quarter, data2, label='B组', width=0.4, bottom=data1)\n",
|
||
"plt.bar(quarter, data3, label='C组', width=0.4, bottom=data1 + data2)\n",
|
||
"plt.xticks(quarter, labels=[f'Q{i}' for i in range(1, 5)])\n",
|
||
"# plt.yticks(np.arange(0, 1.1, 0.2), labels=[f'{i}%' for i in range(0, 101, 20)])\n",
|
||
"plt.gca().yaxis.set_major_formatter(tkr.PercentFormatter(xmax=1, decimals=0))\n",
|
||
"for i in range(quarter.size):\n",
|
||
" plt.text(i, data1[i] / 2, f'{data1[i] * 100:.1f}%', ha='center', color='w')\n",
|
||
" plt.text(i, data1[i] + data2[i] / 2, f'{data2[i] * 100:.1f}%', ha='center', color='w')\n",
|
||
" plt.text(i, data1[i] + data2[i] + data3[i] / 2, f'{data3[i] * 100:.1f}%', ha='center', color='w')\n",
|
||
"plt.legend()\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "98d80c5c-2f1a-41e2-9222-d396bb4a580e",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"labels = ['苹果', '香蕉', '桃子', '荔枝', '石榴', '山竹', '榴莲']\n",
|
||
"data = np.random.randint(100, 500, 7)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "33086285-51e7-40af-b7fe-c775b63beff0",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制饼图\n",
|
||
"plt.figure(figsize=(5, 5), dpi=120)\n",
|
||
"plt.pie(\n",
|
||
" data, \n",
|
||
" labels=labels, # 每块饼对应的标签\n",
|
||
" labeldistance=1.1, # 标签到圆心的距离\n",
|
||
" autopct='%.1f%%', # 自动计算和显示百分比\n",
|
||
" pctdistance=0.88, # 百分比到圆心的距离\n",
|
||
" explode=[0, 0, 0.05, 0, 0, 0, 0.1], # 分离距离(分离饼图)\n",
|
||
" shadow=True, # 阴影效果\n",
|
||
" wedgeprops={'width': 0.25, 'edgecolor': 'w'}, # 楔子属性(环状饼图)\n",
|
||
" textprops={'fontsize': 9, 'color': 'k'} # 文本属性\n",
|
||
")\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "14813133-5f35-4324-b001-17708872d80c",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"x1 = np.random.normal(0, 1, 5000)\n",
|
||
"x2 = np.random.random(100000)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "2607c7d9-77e6-4b01-8d15-1e8b61dc6dba",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制直方图(概率密度)\n",
|
||
"plt.hist(x1, bins=20, density=True, color='darkcyan')\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "3c5b6ed0-0c80-4040-8cdd-51c00083d252",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制直方图(累积分布)\n",
|
||
"plt.hist(x2, bins=10, density=True, cumulative=True)\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "80db5ad6-30e8-47e3-a092-5b2a934f35c8",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"data = x1[::5]\n",
|
||
"data.size"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "a14acac1-6515-4b23-befc-0f28f3193b88",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制箱线图\n",
|
||
"plt.boxplot(data, showmeans=True, notch=True)\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "c8c5c940-7f83-4fc2-803f-b302d78cd30d",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 堆叠折线图(面积图)\n",
|
||
"plt.figure(figsize=(8, 4))\n",
|
||
"days = np.arange(7)\n",
|
||
"sleeping = [7, 8, 6, 6, 7, 8, 10]\n",
|
||
"eating = [2, 3, 2, 1, 2, 3, 2]\n",
|
||
"working = [7, 8, 7, 8, 6, 2, 3]\n",
|
||
"playing = [8, 5, 9, 9, 9, 11, 9]\n",
|
||
"plt.stackplot(days, sleeping, eating, working, playing)\n",
|
||
"plt.legend(['睡觉', '吃饭', '工作', '玩耍'], fontsize=10)\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "66c79bd0-8253-4fa2-b303-696598e952d1",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import matplotlib.pyplot as plt\n",
|
||
"import numpy as np\n",
|
||
"\n",
|
||
"category_names = ['Strongly disagree', 'Disagree',\n",
|
||
" 'Neither agree nor disagree', 'Agree', 'Strongly agree']\n",
|
||
"results = {\n",
|
||
" 'Question 1': [10, 15, 17, 32, 26],\n",
|
||
" 'Question 2': [26, 22, 29, 10, 13],\n",
|
||
" 'Question 3': [35, 37, 7, 2, 19],\n",
|
||
" 'Question 4': [32, 11, 9, 15, 33],\n",
|
||
" 'Question 5': [21, 29, 5, 5, 40],\n",
|
||
" 'Question 6': [8, 19, 5, 30, 38]\n",
|
||
"}\n",
|
||
"\n",
|
||
"\n",
|
||
"def survey(results, category_names):\n",
|
||
" \"\"\"\n",
|
||
" Parameters\n",
|
||
" ----------\n",
|
||
" results : dict\n",
|
||
" A mapping from question labels to a list of answers per category.\n",
|
||
" It is assumed all lists contain the same number of entries and that\n",
|
||
" it matches the length of *category_names*.\n",
|
||
" category_names : list of str\n",
|
||
" The category labels.\n",
|
||
" \"\"\"\n",
|
||
" labels = list(results.keys())\n",
|
||
" data = np.array(list(results.values()))\n",
|
||
" data_cum = data.cumsum(axis=1)\n",
|
||
" category_colors = plt.colormaps['RdYlGn'](\n",
|
||
" np.linspace(0.15, 0.85, data.shape[1]))\n",
|
||
"\n",
|
||
" fig, ax = plt.subplots(figsize=(9.2, 5))\n",
|
||
" ax.invert_yaxis()\n",
|
||
" ax.xaxis.set_visible(False)\n",
|
||
" ax.set_xlim(0, np.sum(data, axis=1).max())\n",
|
||
"\n",
|
||
" for i, (colname, color) in enumerate(zip(category_names, category_colors)):\n",
|
||
" widths = data[:, i]\n",
|
||
" starts = data_cum[:, i] - widths\n",
|
||
" rects = ax.barh(labels, widths, left=starts, height=0.5,\n",
|
||
" label=colname, color=color)\n",
|
||
"\n",
|
||
" r, g, b, _ = color\n",
|
||
" text_color = 'white' if r * g * b < 0.5 else 'darkgrey'\n",
|
||
" ax.bar_label(rects, label_type='center', color=text_color)\n",
|
||
" ax.legend(ncols=len(category_names), bbox_to_anchor=(0, 1),\n",
|
||
" loc='lower left', fontsize='small')\n",
|
||
"\n",
|
||
" return fig, ax\n",
|
||
"\n",
|
||
"\n",
|
||
"survey(results, category_names)\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "36499ccc-a243-4f96-9759-454d3267bb9a",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 雷达图(极坐标折线图)\n",
|
||
"labels = np.array(['速度', '力量', '经验', '防守', '发球', '技术'])\n",
|
||
"malong_values = np.array([93, 95, 98, 92, 96, 97])\n",
|
||
"shuigu_values = np.array([30, 40, 65, 80, 45, 60])\n",
|
||
"angles = np.linspace(0, 2 * np.pi, labels.size, endpoint=False)\n",
|
||
"# 加一条数据让图形闭合\n",
|
||
"malong_values = np.append(malong_values, malong_values[0])\n",
|
||
"shuigu_values = np.append(shuigu_values, shuigu_values[0])\n",
|
||
"angles = np.append(angles, angles[0])\n",
|
||
"\n",
|
||
"# 创建画布\n",
|
||
"plt.figure(figsize=(4, 4), dpi=120)\n",
|
||
"# 创建坐标系\n",
|
||
"ax = plt.subplot(projection='polar')\n",
|
||
"# 绘图和填充\n",
|
||
"plt.plot(angles, malong_values, color='r', linewidth=2, label='马龙')\n",
|
||
"plt.fill(angles, malong_values, color='r', alpha=0.25)\n",
|
||
"plt.plot(angles, shuigu_values, color='g', linewidth=2, label='水谷隼')\n",
|
||
"plt.fill(angles, shuigu_values, color='g', alpha=0.25)\n",
|
||
"# 设置文字和网格线\n",
|
||
"# ax.set_thetagrids(angles[:-1] * 180 / np.pi, labels, fontsize=10)\n",
|
||
"# ax.set_rgrids([0, 20, 40, 60, 80, 100], fontsize=10)\n",
|
||
"ax.legend()\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "e3939fab-09a5-4e21-9bee-0081acddb36e",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 玫瑰图(圆形柱状图)\n",
|
||
"group1 = np.random.randint(20, 50, 4)\n",
|
||
"group2 = np.random.randint(10, 60, 4)\n",
|
||
"x = np.array([f'A组-Q{i}' for i in range(1, 5)] + [f'B组-Q{i}' for i in range(1, 5)])\n",
|
||
"y = np.array(group1.tolist() + group2.tolist())\n",
|
||
"theta = np.linspace(0, 2 * np.pi, x.size, endpoint=False)\n",
|
||
"width = 2 * np.pi / x.size\n",
|
||
"\n",
|
||
"# 产生随机颜色\n",
|
||
"colors = np.random.rand(8, 3)\n",
|
||
"# 将柱状图投影到极坐标\n",
|
||
"ax = plt.subplot(projection='polar')\n",
|
||
"plt.bar(theta, y, width=width, color=colors, bottom=0)\n",
|
||
"ax.set_thetagrids(theta * 180 / np.pi, x, fontsize=10)\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "e0390d2a-a0a9-483a-a4bc-ce1c5059f30b",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# %matplotlib qt"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "d338e6e3-bf98-428b-bac2-693bb1a2a760",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制3D曲面\n",
|
||
"from mpl_toolkits.mplot3d import Axes3D\n",
|
||
"\n",
|
||
"fig = plt.figure(figsize=(8, 4), dpi=120)\n",
|
||
"ax = Axes3D(fig, auto_add_to_figure=False)\n",
|
||
"fig.add_axes(ax)\n",
|
||
"x = np.arange(-2, 2, 0.1)\n",
|
||
"y = np.arange(-2, 2, 0.1)\n",
|
||
"x, y = np.meshgrid(x, y)\n",
|
||
"z = (1 - y ** 5 + x ** 5) * np.exp(-x ** 2 - y ** 2)\n",
|
||
"ax.plot_surface(x, y, z)\n",
|
||
"plt.show()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "6dc5a3e1-d5a0-4f83-b8a6-7f69015a1ba9",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# %matplotlib inline"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "6f1235e1-f300-4ae2-ad7a-b97523bedfa1",
|
||
"metadata": {},
|
||
"source": [
|
||
"### seaborn\n",
|
||
"\n",
|
||
"对matplotlib进行了封装,定制了默认的样式,简化了调用matplotlib函数时需要传入的参数。"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "25a64a4e-bacc-43da-ad92-ec7ae2191b38",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# %pip install seaborn"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "fa94138e-a642-45d5-957e-b094bdabed91",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import seaborn as sns\n",
|
||
"\n",
|
||
"# 设置使用默认主题(样式、配色方案、字体方案、……)\n",
|
||
"sns.set_theme()\n",
|
||
"# sns.set_theme(font_scale=1.2, style='darkgrid', palette='Dark2')\n",
|
||
"\n",
|
||
"plt.rcParams['font.sans-serif'].insert(0, 'SimHei')\n",
|
||
"plt.rcParams['axes.unicode_minus'] = False"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "dabdf3ae-f8dc-42ad-a929-909b0325ca3e",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# import ssl\n",
|
||
"\n",
|
||
"# ssl._create_default_https_context = ssl._create_unverified_context"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "ddf6e866-1115-404d-b548-a6070e4e065e",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# tips_df = sns.load_dataset('tips')\n",
|
||
"# tips_df.info()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "500326a6-ff54-41e0-8c93-8307107ce52d",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"tips_df = pd.read_excel('res/tips.xlsx')\n",
|
||
"tips_df.info()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "de1ae073-3fe9-45a1-9db8-21ed75de45ff",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"tips_df.head()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "5028d1d5-7d7e-4e34-a17b-80bae21f6324",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"sns.histplot(data=tips_df, x='total_bill')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "11b422dd-68ff-4881-9164-2343b7be0375",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制直方图\n",
|
||
"# kde - kernel density estimation - 拟合概率密度曲线\n",
|
||
"sns.histplot(data=tips_df, x='total_bill', kde=True, stat='density')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "b05e0b86-5296-4f05-a64c-34395e9f565a",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# tips_df[['total_bill', 'tip']].corr(method='pearson')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "8eb7d2be-9981-4750-999f-2335824ff619",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制点对图\n",
|
||
"# sns.pairplot(data=tips_df)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "c9f97b00-ecab-4602-8a9a-16cb30db687e",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# tips_df.query('sex == \"Female\"')[['total_bill', 'tip']].corr()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "8d8e63c7-97b6-41b4-bf97-10fc1f14617a",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# tips_df.query('sex == \"Male\"')[['total_bill', 'tip']].corr()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "17ac77e3-adac-4ac9-8e7a-f911b014b598",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"sns.color_palette('tab10')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "d31d89fd-f0c3-4698-8a2f-85dfb7e852ea",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"sns.pairplot(data=tips_df, hue='sex', palette='tab10')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "e1d78fd7-64d7-4c37-a738-379ca63ab7ac",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"sns.color_palette('winter')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "5097260c-30e6-48b7-997c-a2b815b3eb0a",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"sns.pairplot(data=tips_df, hue='sex', palette='winter')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "145a9efe-30b9-447d-bcf6-c74c2233b572",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制联合分布图\n",
|
||
"sns.jointplot(data=tips_df, x='total_bill', y='tip', hue='sex')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "bdcb5a1a-d27f-41d6-9d91-97fad47c31d6",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制联合分布图\n",
|
||
"sns.jointplot(data=tips_df, x='total_bill', y='tip', hue='smoker')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "f83e9d23-93b3-4d1b-81cd-d7b89613aefa",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制联合分布图\n",
|
||
"sns.jointplot(data=tips_df, x='total_bill', y='tip', hue='time')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "bfc55394-74e6-45d6-9a7a-68d26221a8f2",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制线性回归模型图\n",
|
||
"# lm - linear regression model\n",
|
||
"sns.lmplot(data=tips_df, x='total_bill', y='tip', hue='smoker')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "ef565a16-3847-466b-a22f-9ecc42265c31",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制箱线图\n",
|
||
"sns.boxplot(data=tips_df, x='day', y='total_bill')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "8a36af19-38af-4f53-9c9a-f0a6ef8f02d3",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 绘制小提琴图\n",
|
||
"sns.violinplot(data=tips_df, x='day', y='total_bill')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "27067b07-bf54-4905-a7a3-0fc9f91885f8",
|
||
"metadata": {},
|
||
"source": [
|
||
"### pyecharts\n",
|
||
"\n",
|
||
"对Apache的echarts库用Python语言进行了封装,可以绘制美观且交互性极佳的统计图表。"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "afecad24-897c-4729-9034-640f4d38ef7e",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# %pip install -U pyecharts"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "7fab295e-0e60-4fd4-9852-77f23bea19fc",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 配置Notebook的类型是JupyterLab\n",
|
||
"from pyecharts.globals import CurrentConfig, NotebookType\n",
|
||
"\n",
|
||
"CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "1e12a132-50d5-40a9-9b3f-e67b224ef7cf",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from pyecharts.charts import Bar\n",
|
||
"\n",
|
||
"a = np.random.randint(10, 50, 6)\n",
|
||
"b = np.random.randint(20, 40, 6)\n",
|
||
"\n",
|
||
"bar = Bar()\n",
|
||
"bar.add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
|
||
"bar.add_yaxis(\"商家A\", a.tolist())\n",
|
||
"bar.add_yaxis(\"商家B\", b.tolist())\n",
|
||
"bar.load_javascript()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "baaaab27-4cc2-4584-904f-9e7bc6181d95",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"bar.render_notebook()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "617bd760-447e-4b6f-b6a4-41dc126be5d8",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from pyecharts.charts import Bar\n",
|
||
"from pyecharts import options as opts\n",
|
||
"from pyecharts.globals import ThemeType\n",
|
||
"\n",
|
||
"# 创建柱状图对象\n",
|
||
"bar = Bar(init_opts=opts.InitOpts(width='640px', height='480px', theme=ThemeType.LIGHT))\n",
|
||
"# 修改全局配置项\n",
|
||
"bar.set_global_opts(\n",
|
||
" # 定制标题\n",
|
||
" title_opts=opts.TitleOpts(\n",
|
||
" title='2022年各品类销售额',\n",
|
||
" title_link='http://www.qfedu.com',\n",
|
||
" pos_left='center'\n",
|
||
" ),\n",
|
||
" # 定制图例\n",
|
||
" legend_opts=opts.LegendOpts(\n",
|
||
" is_show=True,\n",
|
||
" pos_top='bottom'\n",
|
||
" ),\n",
|
||
" # 定制工具箱\n",
|
||
" toolbox_opts=opts.ToolboxOpts(\n",
|
||
" is_show=True,\n",
|
||
" pos_left='right',\n",
|
||
" pos_top='center',\n",
|
||
" orient='vertical'\n",
|
||
" )\n",
|
||
")\n",
|
||
"# 添加横轴的数据\n",
|
||
"bar.add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
|
||
"# 添加纵轴的数据\n",
|
||
"bar.add_yaxis(\"商家A\", [5, 20, 36, 10, 45, 20])\n",
|
||
"bar.add_yaxis(\"商家B\", [15, 22, 23, 18, 37, 40])\n",
|
||
"# 让浏览器加载JS文件\n",
|
||
"bar.load_javascript()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "5734c4da-1a6d-418a-a2d4-80dfb196da8b",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 渲染图表\n",
|
||
"bar.render_notebook()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "6ce15768-7810-4699-9795-51188525ca83",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from pyecharts import options as opts\n",
|
||
"from pyecharts.charts import Funnel\n",
|
||
"\n",
|
||
"steps = ['曝光', '点击', '加购', '下单', '支付']\n",
|
||
"vdata = [10000, 5000, 2000, 1200, 880]\n",
|
||
"\n",
|
||
"f = Funnel()\n",
|
||
"f.add('转化', [(step, data) for step, data in zip(steps, vdata)])\n",
|
||
"f.set_global_opts(\n",
|
||
" title_opts=opts.TitleOpts(\n",
|
||
" title='转化漏斗',\n",
|
||
" pos_left='10%',\n",
|
||
" title_textstyle_opts=opts.TextStyleOpts(\n",
|
||
" font_family='微软雅黑',\n",
|
||
" font_size=28\n",
|
||
" )\n",
|
||
" )\n",
|
||
")\n",
|
||
"f.load_javascript()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "78f6113c-ad73-4a25-a20b-b88b0b8fbd58",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"f.render_notebook()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "b2add1c4-9ab9-493e-b312-a972795c6f3c",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 注意:pyecharts库只接受原生Python数据类型(不支持numpy的数组,pandas的Series或DataFrame)\n",
|
||
"import pyecharts.options as opts\n",
|
||
"from pyecharts.charts import Pie\n",
|
||
"\n",
|
||
"x_data = [\"直接访问\", \"邮件营销\", \"联盟广告\", \"视频广告\", \"搜索引擎\"]\n",
|
||
"y_data = [335, 310, 234, 135, 1548]\n",
|
||
"\n",
|
||
"pie_chart = Pie(init_opts=opts.InitOpts(\n",
|
||
" width='640px',\n",
|
||
" height='480px'\n",
|
||
"))\n",
|
||
"pie_chart.add(\n",
|
||
" series_name=\"访问来源\",\n",
|
||
" data_pair=[list(z) for z in zip(x_data, y_data)],\n",
|
||
" radius=[\"50%\", \"70%\"],\n",
|
||
" label_opts=opts.LabelOpts(is_show=False, position=\"center\"),\n",
|
||
")\n",
|
||
"pie_chart.set_global_opts(legend_opts=opts.LegendOpts(pos_left=\"legft\", orient=\"vertical\"))\n",
|
||
"pie_chart.set_series_opts(\n",
|
||
" label_opts=opts.LabelOpts(formatter=\"{b}: {d}%\")\n",
|
||
")\n",
|
||
"pie_chart.load_javascript()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "1b478655-7774-43ad-a0ca-1fd7082d5a4d",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"pie_chart.render_notebook()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "be870fdd-71c9-4b98-82bf-dc5f7c81503a",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"baidu = pd.read_excel('res/2022年股票数据.xlsx', sheet_name='BIDU', index_col='Date')\n",
|
||
"baidu = baidu[::-1][['Open', 'Close', 'Low', 'High']].round(2)\n",
|
||
"baidu"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "e5eec52d-7ba1-4c30-8fcf-582971528ac1",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from pyecharts import options as opts\n",
|
||
"from pyecharts.charts import Kline\n",
|
||
"\n",
|
||
"x_data = baidu.index.strftime('%m月%d日').values.tolist()\n",
|
||
"y_data = baidu.values.tolist()\n",
|
||
"\n",
|
||
"kline_chart = Kline()\n",
|
||
"kline_chart.add_xaxis(x_data)\n",
|
||
"kline_chart.add_yaxis('', y_data)\n",
|
||
"kline_chart.set_global_opts(\n",
|
||
" xaxis_opts=opts.AxisOpts(is_scale=True),\n",
|
||
" yaxis_opts=opts.AxisOpts(\n",
|
||
" is_scale=True,\n",
|
||
" splitarea_opts=opts.SplitAreaOpts(\n",
|
||
" is_show=True,\n",
|
||
" areastyle_opts=opts.AreaStyleOpts(opacity=1)\n",
|
||
" ),\n",
|
||
" ),\n",
|
||
" datazoom_opts=[\n",
|
||
" opts.DataZoomOpts(\n",
|
||
" pos_bottom='2%',\n",
|
||
" range_start=40,\n",
|
||
" range_end=60,\n",
|
||
" )\n",
|
||
" ],\n",
|
||
")\n",
|
||
"kline_chart.load_javascript()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "26dfb364-9edd-4a0a-8ac1-4b7146971e44",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"kline_chart.render_notebook()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "0ce7fde5-2f11-48f3-a4fc-2d9f3303a13b",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# 安装地图数据\n",
|
||
"# %pip uninstall -y echarts-countries-pypkg\n",
|
||
"# %pip uninstall -y echarts-china-provinces-pypkg\n",
|
||
"# %pip uninstall -y echarts-china-cities-pypkg\n",
|
||
"# %pip uninstall -y echarts-china-counties-pypkg"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "97d8b5ca-6c66-4500-a74a-8fa521a51549",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from pyecharts import options as opts\n",
|
||
"from pyecharts.charts import Map\n",
|
||
"\n",
|
||
"data = [\n",
|
||
" ('广东省', 594), ('浙江省', 438), ('四川省', 316), ('北京市', 269), ('山东省', 248),\n",
|
||
" ('江苏省', 234), ('湖南省', 196), ('福建省', 166), ('河南省', 153), ('辽宁省', 152),\n",
|
||
" ('上海市', 138), ('河北省', 86), ('安徽省', 79), ('湖北省', 75), ('黑龙江省', 70), \n",
|
||
" ('陕西省', 63), ('吉林省', 59), ('江西省', 56), ('重庆市', 46), ('贵州省', 39),\n",
|
||
" ('山西省', 37), ('云南省', 33), ('广西壮族自治区', 28), ('天津市', 22), ('新疆维吾尔自治区', 24),\n",
|
||
" ('海南省', 18), ('台湾省', 11), ('甘肃省', 7), ('青海省', 3), ('内蒙古自治区', 17), \n",
|
||
" ('宁夏回族自治区', 1), ('西藏自治区', 1), ('香港特别行政区', 12), ('澳门特别行政区', 2)\n",
|
||
"]\n",
|
||
"\n",
|
||
"map_chart = Map(init_opts=opts.InitOpts(width='1000px', height='1000px'))\n",
|
||
"map_chart.add('', data, 'china', is_roam=False)\n",
|
||
"map_chart.load_javascript()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "cb71a3f2-f08b-435a-ad75-a4b6a6be6f91",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"map_chart.render_notebook()"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3 (ipykernel)",
|
||
"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.11.7"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|