{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Matplotlib 基础"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"在这节课中,我们将会学习到 Matplotlib 的相关内容。\n",
"\n",
"通过 Matplotlib,我们可以仅编写几行代码,就生成直方图,折现图,散点图等,高效美观的展示我们的数据。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"导入 `matplotlib` 和 `numpy`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from numpy import *\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## plot 二维图"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"plt.plot(y)\n",
"plt.plot(x, y)\n",
"plt.plot(x, y, format_string)\n",
"```\n",
"\n",
"只给定 `y` 值,默认以下标为 `x` 轴:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = linspace(0, 2 * pi, 50)\n",
"plt.plot(sin(x));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"给定 `x` 和 `y` 值:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.plot(x, sin(x));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"多条数据线:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.plot(x, sin(x),\n",
" x, sin(2 * x));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"使用字符串,给定线条参数:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.plot(x, sin(x), 'r-^');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"多线条:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.plot(x, sin(x), 'b-o',\n",
" x, sin(2 * x), 'r-^');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px> **编程练习**\n",
"\n",
"在同一幅图中绘制 sin(x), sin(2\\*x), sin(3\\*x) 三条曲线。颜色分别为蓝色,红色,黄色。\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# 请编写你的答案\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from numpy import *\n",
"%matplotlib inline\n",
"x = linspace(0, 2 * pi, 50)\n",
"plt.plot(x, sin(x), 'b', x, sin(2 * x), 'r', x, sin(3 * x), 'y');\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## scatter 散点图"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"scatter(x, y)\n",
"scatter(x, y, size)\n",
"scatter(x, y, size, color)\n",
"```\n",
"\n",
"假设我们想画二维散点图:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.plot(x, sin(x), 'bo');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"可以使用 `scatter` 达到同样的效果:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.scatter(x, sin(x));"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = random.rand(200)\n",
"y = random.rand(200)\n",
"size = random.rand(200) * 30\n",
"color = random.rand(200)\n",
"plt.scatter(x, y, size, color)\n",
"# 显示颜色条\n",
"plt.colorbar();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px> **编程练习**\n",
"\n",
"要求:使用 scatter 绘制 sin(x)的二维散点图,把颜色设置为红色,散点形式设置为三角形."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 请编写你的答案\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"x = linspace(0, 10, 30)\n",
"plt.scatter(x, sin(x), marker='^', color='r');\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 多图"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"使用figure()命令产生新的图像:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"t = linspace(0, 2*pi, 50)\n",
"x = sin(t)\n",
"y = cos(t)\n",
"plt.figure()\n",
"plt.plot(x)\n",
"plt.figure()\n",
"plt.plot(y);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"或者使用 `subplot` 在一幅图中画多幅子图:\n",
"\n",
" subplot(row, column, index)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.subplot(1, 2, 1)\n",
"plt.plot(x)\n",
"plt.subplot(1, 2, 2)\n",
"plt.plot(y);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px> **编程练习**\n",
"\n",
"要求:在上下排布的2个子图中,显示sin(x)和cos(x)的图像,请编写代码实现。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 请编写你的答案\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure()\n",
"plt.subplot(2, 1, 1)\n",
"plt.plot(x)\n",
"plt.subplot(2, 1, 2)\n",
"plt.plot(y);\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 向图中添加数据"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"默认多次 `plot` 会叠加:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.plot(x)\n",
"plt.plot(y);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 标签"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"可以在 `plot` 中加入 `label` ,使用 `legend` 加上图例:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.plot(x, label='sin')\n",
"plt.plot(y, label='cos')\n",
"plt.legend();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"或者直接在 `legend`中加入:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.plot(x)\n",
"plt.plot(y)\n",
"plt.legend(['sin', 'cos']);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 坐标轴,标题,网格"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"可以设置坐标轴的标签和标题:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = linspace(0, 2 * pi, 50)\n",
"plt.plot(x, sin(x))\n",
"plt.xlabel('radians')\n",
"# 可以设置字体大小\n",
"plt.ylabel('amplitude', fontsize='large')\n",
"plt.title('Sin(x)');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"用 'grid()' 来显示网格:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.plot(x, sin(x))\n",
"plt.xlabel('radians')\n",
"plt.ylabel('amplitude', fontsize='large')\n",
"plt.title('Sin(x)')\n",
"plt.grid();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px> **编程练习**\n",
"\n",
"要求:绘制 cos(x) 的图,包含坐标轴、标题和网格。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 请编写你的答案\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot(x, cos(x))\n",
"plt.xlabel('radians')\n",
"plt.ylabel('amplitude', fontsize='large')\n",
"plt.title('Cos(x)')\n",
"plt.grid();\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 直方图"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"从高斯分布随机生成1000个点得到的直方图:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"plt.hist(np.random.randn(1000));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"更多例子请参考下列网站:\n",
"\n",
"http://matplotlib.org/gallery.html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px> **编程练习**\n",
"\n",
"要求: 已知某商店出售 3 种水果,当天的营业额分别为苹果:100,香蕉:200,葡萄:250,请绘制饼图,显示三种水果的占比。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 请编写你的答案\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"labels = [u'苹果',u'香蕉',u'葡萄'] #定义标签\n",
"sizes = [100,200,300,] #每块值\n",
"plt.pie(sizes, labels=labels, autopct = '%3.2f%%');\n"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}