{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "63C1DBF82CD746099782739244B67E7C",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案,也可以和图形工具包一起使用。\n",
"\n",
"------\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "B92E8770D93C43A69DEF21EB72E6E418",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## 一、导入\n",
"1.导入matplotlib库简写为plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "C1C27426C46446E98C8086547ED6AB91",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "32583C823FED4A179EC8640A195F3D56",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## 二、基本图表\n",
"2.用plot方法画出x=(0,10)间sin的图像"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "FE6254DA8B2B4971970C4952A04C8CAE",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0, 10, 30)\r\n",
"plt.plot(x, np.sin(x));"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0D93F2D62A1B4CB18E7329710674AB3B",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"3.用点加线的方式画出x=(0,10)间sin的图像"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "465DC813859F433AB0E8ACEA991109C3",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"plt.plot(x, np.sin(x), '-o');"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CB2AB6597C6149FC807E3D21E1A41DA6",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"4.用scatter方法画出x=(0,10)间sin的点图像"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "544B52B75A4E40AB8701BAF29F6C76A9",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"plt.scatter(x, np.sin(x));"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7303231ADDB44F7A81557AD049714AF8",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"5.用饼图的面积及颜色展示一组4维数据"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "E67D1E05A9404579A603BC6738B7BF5E",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"rng = np.random.RandomState(0)\r\n",
"x = rng.randn(100)\r\n",
"y = rng.randn(100)\r\n",
"colors = rng.rand(100)\r\n",
"sizes = 1000 * rng.rand(100)\r\n",
"\r\n",
"plt.scatter(x, y, c=colors, s=sizes, alpha=0.3,\r\n",
" cmap='viridis')\r\n",
"plt.colorbar(); # 展示色阶"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4D33CABD132D4A6095C6E2D53473A1C4",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"6.绘制一组误差为±0.8的数据的误差条图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "EFE17F2619B4404881FBCA8046DC6027",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0, 10, 50)\r\n",
"dy = 0.8\r\n",
"y = np.sin(x) + dy * np.random.randn(50)\r\n",
"\r\n",
"plt.errorbar(x, y, yerr=dy, fmt='.k')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "031206548B3D4EA3885876E6FCAC9AB5",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"7.绘制一个柱状图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "0E8A3F01CED5432C9DCBF340A31F95AB",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = [1,2,3,4,5,6,7,8]\r\n",
"y = [3,1,4,5,8,9,7,2]\r\n",
"label=['A','B','C','D','E','F','G','H']\r\n",
"\r\n",
"plt.bar(x,y,tick_label = label);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "F04E8C2D902C49D1852EEE3BA2CEF4FD",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"8.绘制一个水平方向柱状图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "41E75E7D3A074437ABE1753070B4B5AB",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = [1,2,3,4,5,6,7,8]\r\n",
"y = [3,1,4,5,8,9,7,2]\r\n",
"label=['A','B','C','D','E','F','G','H']\r\n",
"\r\n",
"plt.barh(x,y,tick_label = label);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "637E2824542E45D6B291900368BC5809",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"9.绘制1000个随机值的直方图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "B620FFF40CCA4EA18EE34465CDAA68E3",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"data = np.random.randn(1000)\r\n",
"plt.hist(data);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "98B073D0DF92435C89CCE9EA1B7563C9",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"10.设置直方图分30个bins,并设置为频率分布"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "C0DC8DDD8C704BED9AD895A640B1E25D",
"jupyter": {},
"mdEditEnable": false,
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"plt.hist(data, bins=30,histtype='stepfilled', density=True)\r\n",
"plt.show();"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FA1EAEBA238147E78FECCCE44275E543",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"11.在一张图中绘制3组不同的直方图,并设置透明度"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "753E2A3E4CC54EB8970314CDD38E6BAA",
"jupyter": {},
"mdEditEnable": false,
"scrolled": true,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x1 = np.random.normal(0, 0.8, 1000)\r\n",
"x2 = np.random.normal(-2, 1, 1000)\r\n",
"x3 = np.random.normal(3, 2, 1000)\r\n",
"\r\n",
"kwargs = dict(alpha=0.3, bins=40, density = True)\r\n",
"\r\n",
"plt.hist(x1, **kwargs);\r\n",
"plt.hist(x2, **kwargs);\r\n",
"plt.hist(x3, **kwargs);\r\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "99972BC9C0FF46709A34085EB4F33AB1",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"12.绘制一张二维直方图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "508E4096CCD044279CA7C03D037696C7",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"mean = [0, 0]\r\n",
"cov = [[1, 1], [1, 2]]\r\n",
"x, y = np.random.multivariate_normal(mean, cov, 10000).T\r\n",
"plt.hist2d(x, y, bins=30);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "D329FDF4DFEC45EA802A5A5FA22AEFEB",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"13.绘制一张设置网格大小为30的六角形直方图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "1C135E782C474A558A456B9E6C5E08FB",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"plt.hexbin(x, y, gridsize=30);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1FABCC9EA26F4A1889445617965D24D9",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## 三、自定义图表元素\n",
"14.绘制x=(0,10)间sin的图像,设置线性为虚线"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "6DF02E3ACED94177927FE9F71B20AEC1",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0,10,100)\r\n",
"plt.plot(x,np.sin(x),'--');"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0126190DC66D4711853622259AD65D08",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"15.设置y轴显示范围为(-1.5,1.5) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "7F00C68E804A43F8B56A34D64A916BB3",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0,10,100)\r\n",
"plt.plot(x,np.sin(x))\r\n",
"plt.ylim(-1.5,1.5);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7A85F7DAF87F401F83CEEF71D30E59E6",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"16.设置x,y轴标签variable x,value y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "ACEE41A4F61C41DA8CFE6BFBFB2E96CE",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0.05, 10, 100)\r\n",
"y = np.sin(x)\r\n",
"plt.plot(x, y, label='sin(x)')\r\n",
"plt.xlabel('variable x');\r\n",
"plt.ylabel('value y');\r\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "09D4622BE48E4B2B81BAFA4EAFB6B5A4",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"17.设置图表标题“三角函数”"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "CF85FF7D8D8C4F0E985896A2EB123BF3",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0.05, 10, 100)\r\n",
"y = np.sin(x)\r\n",
"plt.plot(x, y, label='sin(x)')\r\n",
"plt.title('三角函数');"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0BB29E2AA96044B4805F08EF64D2BD77",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"18.显示网格"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "3E18420438AC4DA689D70A36876AE01D",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0.05, 10, 100)\n",
"y = np.sin(x)\n",
"plt.plot(x, y)\n",
"plt.grid()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "E7CA9FADFFF043348665612A9B29D3F9",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"19.绘制平行于x轴y=0.8的水平参考线"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "379B3F9A570F48D58FDF4D9085B78BA1",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0.05, 10, 100)\r\n",
"y = np.sin(x)\r\n",
"plt.plot(x, y)\r\n",
"plt.axhline(y=0.8, ls='--', c='r')\r\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AD0268B993224BF6850F558D7EDD5D12",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"20.绘制垂直于x轴x<4 and x>6的参考区域,以及y轴y<0.2 and y>-0.2的参考区域"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "270C1A8864194203912C218307715CE0",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0.05, 10, 100)\n",
"y = np.sin(x)\n",
"plt.plot(x, y)\n",
"plt.axvspan(xmin=4, xmax=6, facecolor='r', alpha=0.3) # 垂直x轴\n",
"plt.axhspan(ymin=-0.2, ymax=0.2, facecolor='y', alpha=0.3); # 垂直y轴"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8EF93EE2EB8F490C99C255DD99A3F1A7",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"21.添加注释文字sin(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "54ECB9E9A3474126993B4BE32A83F644",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0.05, 10, 100)\n",
"y = np.sin(x)\n",
"plt.plot(x, y)\n",
"plt.text(3.2, 0, 'sin(x)', weight='bold', color='r');\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "62D56E2FE4754C258EFC26771CC868F3",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"22.用箭头标出第一个峰值"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "E20671BC86304E988241076DA4D3DCE5",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0.05, 10, 100)\n",
"y = np.sin(x)\n",
"plt.plot(x, y)\n",
"plt.annotate('maximum',xy=(np.pi/2, 1),xytext=(np.pi/2+1, 1),\n",
" weight='bold',\n",
" color='r',\n",
" arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='r'));"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "62B327ECDA9C41AB92BD1D6ED0085557",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## 四、自定义图例\n",
"23.在一张图里绘制sin,cos的图形,并展示图例"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "1556CD999FEA4EDAA51AC935ECB78D30",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0, 10, 1000)\r\n",
"fig, ax = plt.subplots()\r\n",
"\r\n",
"ax.plot(x, np.sin(x), label='sin')\r\n",
"ax.plot(x, np.cos(x), '--', label='cos')\r\n",
"ax.legend();\r\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7452E5EDA4134AA28F7E2231EDEA0CE5",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"24.调整图例在左上角展示,且不显示边框"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "D1564F66B2F7432688394DC03ADD75E6",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"ax.legend(loc='upper left', frameon=False);\n",
"fig"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "35EF0B0E1E5D404F82781C908A02E644",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"25.调整图例在画面下方居中展示,且分成2列"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "D8041B84218E4AF8959F03EF10407AE1",
"jupyter": {},
"scrolled": true,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"ax.legend(frameon=False, loc='lower center', ncol=2)\r\n",
"fig"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AE251699DC51481E84E8312B96B7B980",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"26.绘制的图像$sin(x),sin(x+\\pi /2),sin(x+\\pi)$的图像,并只显示前2者的图例"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "E39E8B1A3C9F4DD8B7DFCEC7FAFAC40C",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"y = np.sin(x[:, np.newaxis] + np.pi * np.arange(0, 2, 0.5))\r\n",
"lines = plt.plot(x, y)\r\n",
"\r\n",
"# lines 是 plt.Line2D 类型的实例的列表\r\n",
"plt.legend(lines[:2], ['first', 'second']);\r\n",
"\r\n",
"# 第二个方法\r\n",
"#plt.plot(x, y[:, 0], label='first')\r\n",
"#plt.plot(x, y[:, 1], label='second')\r\n",
"#plt.plot(x, y[:, 2:])\r\n",
"#plt.legend(framealpha=1, frameon=True);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "B473C113D2E8421BBF5CA2275C9E544A",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"27.将图例分不同的区域展示"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "503CB6BEAC3943B981F18CFD65FA05CD",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"\n",
"lines = []\n",
"styles = ['-', '--', '-.', ':']\n",
"x = np.linspace(0, 10, 1000)\n",
"\n",
"for i in range(4):\n",
" lines += ax.plot(x, np.sin(x - i * np.pi / 2),styles[i], color='black')\n",
"ax.axis('equal')\n",
"\n",
"# 设置第一组标签\n",
"ax.legend(lines[:2], ['line A', 'line B'],\n",
" loc='upper right', frameon=False)\n",
"\n",
"# 创建第二组标签\n",
"from matplotlib.legend import Legend\n",
"leg = Legend(ax, lines[2:], ['line C', 'line D'],\n",
" loc='lower right', frameon=False)\n",
"ax.add_artist(leg);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FB25F1D29A6D486F87D2983F383B2951",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## 五、自定义色阶\n",
"28.展示色阶"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "0D10C6B5EAC2482DA3AED4C3DEBA7525",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = np.linspace(0, 10, 1000)\r\n",
"I = np.sin(x) * np.cos(x[:, np.newaxis])\r\n",
"\r\n",
"plt.imshow(I)\r\n",
"plt.colorbar();"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DB6A203070D744319E092006C13EE15A",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"29.改变配色为`'gray'`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "5E46D6F2CB0B455E8C4DFB51FDCD266D",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"plt.imshow(I, cmap='gray');"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "585BE54B25534CF1A376A269D37DBCEE",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"30.将色阶分成6个离散值显示"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "E967DB8F54FE4F848A6A4D36DCA96885",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"plt.imshow(I, cmap=plt.cm.get_cmap('Blues', 6))\r\n",
"plt.colorbar()\r\n",
"plt.clim(-1, 1);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1DACC0BC5778443A944F32F404CBE809",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## 六、多子图\n",
"31.在一个10\\*10的画布中,(0.65,0.65)的位置创建一个0.2\\*0.2的子图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "4AD309B16FEA4C9FB1D0C8A3C05F69B2",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"ax1 = plt.axes()\r\n",
"ax2 = plt.axes([0.65, 0.65, 0.2, 0.2])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "C3224B6E11F3427FA6202E610CAA090A",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"32.在2个子图中,显示sin(x)和cos(x)的图像"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "EF78AB2FA4DF49CD825D71D0EC8A84F7",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"fig = plt.figure()\r\n",
"ax1 = fig.add_axes([0.1, 0.5, 0.8, 0.4], ylim=(-1.2, 1.2))\r\n",
"ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.4], ylim=(-1.2, 1.2))\r\n",
"\r\n",
"x = np.linspace(0, 10)\r\n",
"ax1.plot(np.sin(x));\r\n",
"ax2.plot(np.cos(x));\r\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FC60C071EDF0497289C37BDA0BAA956D",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"33.用`for`创建6个子图,并且在图中标识出对应的子图坐标"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "5B7BAAFC08B5483CBB23267F8F4DF238",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"for i in range(1, 7):\r\n",
" plt.subplot(2, 3, i)\r\n",
" plt.text(0.5, 0.5, str((2, 3, i)),fontsize=18, ha='center')\r\n",
" \r\n",
"# 方法二\r\n",
"# fig = plt.figure()\r\n",
"# fig.subplots_adjust(hspace=0.4, wspace=0.4)\r\n",
"# for i in range(1, 7):\r\n",
"# ax = fig.add_subplot(2, 3, i)\r\n",
"# ax.text(0.5, 0.5, str((2, 3, i)),fontsize=18, ha='center')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "D3193A54E3AC4FB2891D40869B351D68",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"34.设置相同行和列共享x,y轴"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "01847EFA5B564C4189E1EBC547514236",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"fig, ax = plt.subplots(2, 3, sharex='col', sharey='row')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "F6129FE0C9EA4FF59009B5B70EC31513",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"35.用`[]`的方式取出每个子图,并添加子图座标文字"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "C06A141D25B7444B8D28B59E713AF837",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"for i in range(2):\r\n",
" for j in range(3):\r\n",
" ax[i, j].text(0.5, 0.5, str((i, j)),fontsize=18, ha='center')\r\n",
"fig"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BE371B33A8A04CB688B5148319DD560D",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"36.组合绘制大小不同的子图,样式如下\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "403892DB5D994FDE98D83C372272EF44",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"grid = plt.GridSpec(2, 3, wspace=0.4, hspace=0.3)\r\n",
"plt.subplot(grid[0, 0])\r\n",
"plt.subplot(grid[0, 1:])\r\n",
"plt.subplot(grid[1, :2])\r\n",
"plt.subplot(grid[1, 2]);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "C6C2FE66D6CC41EB85D89D5D520AAEE9",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"37.显示一组二维数据的频度分布,并分别在x,y轴上,显示该维度的数据的频度分布"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "FD5FD4186BF5465581770F8680520FC6",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"mean = [0, 0]\r\n",
"cov = [[1, 1], [1, 2]]\r\n",
"x, y = np.random.multivariate_normal(mean, cov, 3000).T\r\n",
"\r\n",
"# Set up the axes with gridspec\r\n",
"fig = plt.figure(figsize=(6, 6))\r\n",
"grid = plt.GridSpec(4, 4, hspace=0.2, wspace=0.2)\r\n",
"main_ax = fig.add_subplot(grid[:-1, 1:])\r\n",
"y_hist = fig.add_subplot(grid[:-1, 0], xticklabels=[], sharey=main_ax)\r\n",
"x_hist = fig.add_subplot(grid[-1, 1:], yticklabels=[], sharex=main_ax)\r\n",
"\r\n",
"# scatter points on the main axes\r\n",
"main_ax.scatter(x, y,s=3,alpha=0.2)\r\n",
"\r\n",
"# histogram on the attached axes\r\n",
"x_hist.hist(x, 40, histtype='stepfilled',\r\n",
" orientation='vertical')\r\n",
"x_hist.invert_yaxis()\r\n",
"\r\n",
"y_hist.hist(y, 40, histtype='stepfilled',\r\n",
" orientation='horizontal')\r\n",
"y_hist.invert_xaxis()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "F9D94228A31D461BB0EEFA18836E02B4",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## 七、三维图像\n",
"38.创建一个三维画布"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "971935355E9541119A24FB646B674157",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"from mpl_toolkits import mplot3d\r\n",
"fig = plt.figure()\r\n",
"ax = plt.axes(projection='3d')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "08E58FD685F94F9CB6752134EBA9B9AA",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"39.绘制一个三维螺旋线"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "16D5DEC385374959842B552150C227F3",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"ax = plt.axes(projection='3d')\r\n",
"\r\n",
"# Data for a three-dimensional line\r\n",
"zline = np.linspace(0, 15, 1000)\r\n",
"xline = np.sin(zline)\r\n",
"yline = np.cos(zline)\r\n",
"ax.plot3D(xline, yline, zline);"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "936285ADF58649308D21F348838797C1",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"40.绘制一组三维点"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "2ADFD2B400E647E78E0B81DD5D7EAC9A",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"ax = plt.axes(projection='3d')\r\n",
"zdata = 15 * np.random.random(100)\r\n",
"xdata = np.sin(zdata) + 0.1 * np.random.randn(100)\r\n",
"ydata = np.cos(zdata) + 0.1 * np.random.randn(100)\r\n",
"ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens');"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "8486E6E999B04E249534A02CD140E712",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"import pandas as pd\n",
"df = pd.read_csv('Pokemon.csv')\n",
"df.head()\n",
"\n",
"plt.style.use('ggplot')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1AD3BC70D06145908EDCDCFE20963A7F",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"41.展示前5个宝可梦的`Defense,Attack,HP`的堆积条形图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "01D3EE0E2AFE4F659555D3B7916E6EA8",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"pokemon = df['Name'][:5]\r\n",
"hp = df['HP'][:5]\r\n",
"attack = df['Attack'][:5]\r\n",
"defense = df['Defense'][:5]\r\n",
"ind = [x for x, _ in enumerate(pokemon)]\r\n",
"\r\n",
"plt.figure(figsize=(10,10))\r\n",
"plt.bar(ind, defense, width=0.8, label='Defense', color='blue', bottom=attack+hp)\r\n",
"plt.bar(ind, attack, width=0.8, label='Attack', color='gold', bottom=hp)\r\n",
"plt.bar(ind, hp, width=0.8, label='Hp', color='red')\r\n",
"\r\n",
"plt.xticks(ind, pokemon)\r\n",
"plt.ylabel(\"Value\")\r\n",
"plt.xlabel(\"Pokemon\")\r\n",
"plt.legend(loc=\"upper right\")\r\n",
"plt.title(\"5 Pokemon Defense & Attack & Hp\")\r\n",
"\r\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3057D1FAD8A24A1C9B2CDA0E437C34AB",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"42.展示前5个宝可梦的`Attack,HP`的簇状条形图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "C53D0272339F46C084247E2F5BECF81E",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"N = 5\r\n",
"pokemon_hp = df['HP'][:5]\r\n",
"pokemon_attack = df['Attack'][:5]\r\n",
"\r\n",
"ind = np.arange(N) \r\n",
"width = 0.35 \r\n",
"plt.bar(ind, pokemon_hp, width, label='HP')\r\n",
"plt.bar(ind + width, pokemon_attack, width,label='Attack')\r\n",
"\r\n",
"plt.ylabel('Values')\r\n",
"plt.title('Pokemon Hp & Attack')\r\n",
"\r\n",
"plt.xticks(ind + width / 2, (df['Name'][:5]),rotation=45)\r\n",
"plt.legend(loc='best')\r\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "B9BA406B4813420ABB8E0F443DDECB05",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"43.展示前5个宝可梦的`Defense,Attack,HP`的堆积图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "A042FEC242C14205853A61FDBB5D234A",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = df['Name'][:4]\r\n",
"y1 = df['HP'][:4]\r\n",
"y2 = df['Attack'][:4]\r\n",
"y3 = df['Defense'][:4]\r\n",
"\r\n",
"labels = [\"HP \", \"Attack\", \"Defense\"]\r\n",
"\r\n",
"fig, ax = plt.subplots()\r\n",
"ax.stackplot(x, y1, y2, y3)\r\n",
"ax.legend(loc='upper left', labels=labels)\r\n",
"plt.xticks(rotation=90)\r\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "276678D3B96D419D8CC5D86465CA4242",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"44.公用x轴,展示前5个宝可梦的`Defense,Attack,HP`的折线图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "9670132591CC48679498823E46424ABB",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = df['Name'][:5]\r\n",
"y1 = df['HP'][:5]\r\n",
"y2 = df['Attack'][:5]\r\n",
"y3 = df['Defense'][:5]\r\n",
"\r\n",
"# Create two subplots sharing y axis\r\n",
"fig, (ax1, ax2,ax3) = plt.subplots(3, sharey=True)\r\n",
"\r\n",
"ax1.plot(x, y1, 'ko-')\r\n",
"ax1.set(title='3 subplots', ylabel='HP')\r\n",
"\r\n",
"ax2.plot(x, y2, 'r.-')\r\n",
"ax2.set(xlabel='Pokemon', ylabel='Attack')\r\n",
"\r\n",
"ax3.plot(x, y3, ':')\r\n",
"ax3.set(xlabel='Pokemon', ylabel='Defense')\r\n",
"\r\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "D9F126979B124B599A42BCC9016C8B6C",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"45.展示前15个宝可梦的Attack,HP的折线图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "26E32604016944158BC602F8C3EBFE99",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"plt.plot(df['HP'][:15], '-r',label='HP')\r\n",
"plt.plot(df['Attack'][:15], ':g',label='Attack')\r\n",
"plt.legend();"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5C75152B180643B48AC3B16EBE10890F",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"46.用scatter的x,y,c属性,展示所有宝可梦的Defense,Attack,HP数据"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "3C4C0CEFB62B49F88EED5E9D295E6D8C",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = df['Attack']\n",
"y = df['Defense']\n",
"colors = df['HP']\n",
"\n",
"plt.scatter(x, y, c=colors, alpha=0.5)\n",
"plt.title('Scatter plot')\n",
"plt.xlabel('Attack')\n",
"plt.ylabel('Defense')\n",
"plt.colorbar(); "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "17F92C93781E497E8230064B5FDEA32A",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"47.展示所有宝可梦的攻击力的分布,bins=10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "3F4FC2D4903C44848557F8D1D8AF111A",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"x = df['Attack']\r\n",
"num_bins = 10\r\n",
"n, bins, patches = plt.hist(x, num_bins, facecolor='blue', alpha=0.5)\r\n",
"plt.title('Histogram')\r\n",
"plt.xlabel('Attack')\r\n",
"plt.ylabel('Value')\r\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "59079D33B0C04FAB8C81689AF9098BBA",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"48.展示所有宝可梦Type 1的饼图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "B7E1CE8DB07D476D998BC0F6D27320D9",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"plt.figure(1, figsize=(8,8))\r\n",
"df['Type 1'].value_counts().plot.pie(autopct=\"%1.1f%%\")\r\n",
"plt.legend()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "93031233A69B41DE8B1300BC29C1637B",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"49.展示所有宝可梦Type 1的柱状图"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "93AA7B2C057E4FA793133EB05C634911",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"\r\n",
"ax = df['Type 1'].value_counts().plot.bar(figsize = (12,6),fontsize = 14)\r\n",
"ax.set_title(\"Pokemon Type 1 Count\", fontsize = 20)\r\n",
"ax.set_xlabel(\"Pokemon Type 1\", fontsize = 20)\r\n",
"ax.set_ylabel(\"Value\", fontsize = 20)\r\n",
"\r\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "1DC97A890E8B434EB2CE278A806A2495",
"jupyter": {},
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"top_10_pokemon=df.sort_values(by='Total',ascending=False).head(10)\n",
"corr=top_10_pokemon.corr()\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CF2E596DCEF544D18AAA510FE9D00FD2",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"50.展示综合评分最高的10只宝可梦的系数间的相关系数矩阵"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "603D2E40950E466B885FBAD3549F8580",
"jupyter": {},
"mdEditEnable": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## “宝可梦数据集”可视化\n",
"展示综合评分最高的10只宝可梦的系数间的相关系数矩阵\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"id": "FDB996FCEAA84BCF9A3B90EF172E4002",
"jupyter": {},
"scrolled": false,
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"outputs": [],
"source": [
"import seaborn as sns\n",
"\n",
"fig, ax=plt.subplots(figsize=(10, 6))\n",
"sns.heatmap(corr,annot=True)\n",
"ax.set_ylim(9, 0)\n",
"plt.show()"
]
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}