{
"metadata": {
"name": "",
"signature": "sha256:62086092cb881dd61e4f97d6d5bb811b40f6d2b0e572601e0191f2ba03f4fc14"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#V-[Matplotlib](http://matplotlib.org/)\n",
"\n",
"\n",
"Lecturer:*Jos\u00e9 Pedro Silva*[1](http://www-num.math.uni-wuppertal.de/en/amna/people/jose-pedro-silva.html) - [silva_at_math.uni-wuppertal.de](mailto:silva_at_math.uni-wuppertal.de)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###Index\n",
"\n",
"- [Simple Plot](#simple_plot)\n",
"- [Colormap](#colormap)\n",
"- [3D](#3d)\n",
"- [Histogram](#histogram)\n",
"- [Animation](#animation)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib as mpl\n",
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "heading",
"level": 5,
"metadata": {},
"source": [
"Simple Plot"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = linspace(0,1,100)\n",
"y = x**2"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plot(y)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"[]"
]
},
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"figure()\n",
"plot(x,y)\n",
"xlabel('x')\n",
"ylabel('y')\n",
"title('Quadratic function')\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"figure()\n",
"plot(x,y)\n",
"xlabel('$x$')\n",
"ylabel('$y$')\n",
"title('$y=x^2$')\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fig = figure()\n",
"\n",
"axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)\n",
"axes.plot(x, y, 'r')\n",
"axes.set_xlabel('x')\n",
"axes.set_ylabel('y')\n",
"axes.set_title('title');"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can change the appearance, similar to MATLAB"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = linspace(0,1,10)\n",
"y1 = x**2\n",
"y2 = x**2 + 0.1\n",
"y3 = x**2 + 0.2"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"figure()\n",
"plot(x, y1, color='blue', linestyle='dashed')\n",
"plot(x, y2, color='red')\n",
"plot(x, y3, color='green', linestyle='.',marker='*')\n",
"xlabel('$x$')\n",
"ylabel('$y$')\n",
"title('$y=x^2$')\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"figure()\n",
"plot(x,y1,'--',x,y2,'r',x,y3,'*')\n",
"xlabel('$x$')\n",
"ylabel('$y$')\n",
"title('$y=x^2$')\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or more definition based"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"figure()\n",
"plot(x, y1, color='green', linestyle='dashed')\n",
"plot(x, y2, color='blue', linestyle='.', marker='o', markerfacecolor='blue', markersize=10, alpha = 0.3)\n",
"xlabel('$x$')\n",
"ylabel('$y$')\n",
"title('$y=x^2$')\n",
"show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fig, ax = subplots(2,2,figsize=(12,6))\n",
"\n",
"ax[0,0].plot(x, x+1, color=\"blue\", linewidth=0.25)\n",
"ax[0,0].plot(x, x+2, color=\"blue\", linewidth=0.50)\n",
"ax[0,0].plot(x, x+3, color=\"blue\", linewidth=1.00) # default\n",
"ax[0,0].plot(x, x+4, color=\"blue\", linewidth=2.00)\n",
"\n",
"# possible linestype options \u2018-\u2018, \u2018\u2013\u2019, \u2018-.\u2019, \u2018:\u2019, \u2018.\u2019, \u2018steps\u2019\n",
"ax[0,0].plot(x, x+5, color=\"red\", lw=2, linestyle='-')\n",
"ax[0,0].plot(x, x+6, color=\"red\", lw=2, ls='--')\n",
"ax[0,0].plot(x, x+7, color=\"red\", lw=2, ls='-.')\n",
"ax[0,0].plot(x, x+8, color=\"red\", lw=2, ls=':')\n",
"ax[0,0].plot(x, x+9, color=\"red\", lw=2, ls='.')\n",
"\n",
"# custom\n",
"line, = ax[0,0].plot(x, x+10, color=\"black\", lw=1.50)\n",
"line.set_dashes([5, 10, 15, 10]) # format: line length, space length, ...\n",
"\n",
"# Marker Symbols\n",
"ax[1,0].plot(x, x, lw=2, ls='*', marker='+')\n",
"ax[1,0].plot(x, x+1, lw=2, ls='*', marker='o')\n",
"ax[1,0].plot(x, x+2, lw=2, ls='*', marker='v')\n",
"ax[1,0].plot(x, x+3, lw=2, ls='*', marker='^')\n",
"ax[1,0].plot(x, x+4, lw=2, ls='*', marker='<')\n",
"ax[1,0].plot(x, x+5, lw=2, ls='*', marker='>')\n",
"ax[1,0].plot(x, x+6, lw=2, ls='*', marker='1')\n",
"ax[1,0].plot(x, x+7, lw=2, ls='*', marker='2')\n",
"ax[1,0].plot(x, x+8, lw=2, ls='*', marker='3')\n",
"ax[1,0].plot(x, x+9, lw=2, ls='*', marker='4')\n",
"ax[1,0].plot(x, x+10, lw=2, ls='*', marker='s')\n",
"ax[1,0].plot(x, x+11, lw=2, ls='*', marker='p')\n",
"ax[1,0].plot(x, x+12, lw=2, ls='*', marker='*')\n",
"ax[1,0].plot(x, x+13, lw=2, ls='*', marker='h')\n",
"ax[1,0].plot(x, x+14, lw=2, ls='*', marker='H')\n",
"ax[1,0].plot(x, x+15, lw=2, ls='*', marker='+')\n",
"ax[1,0].plot(x, x+16, lw=2, ls='*', marker='x')\n",
"ax[1,0].plot(x, x+17, lw=2, ls='*', marker='D')\n",
"ax[1,0].plot(x, x+18, lw=2, ls='*', marker='d')\n",
"\n",
"\n",
"# marker size and color\n",
"ax[0,1].plot(x, x+4, color=\"purple\", lw=1, ls='-', marker='o', markersize=2)\n",
"ax[0,1].plot(x, x+5, color=\"purple\", lw=1, ls='-', marker='o', markersize=4)\n",
"ax[0,1].plot(x, x+6, color=\"green\", lw=1, ls='-', marker='o', markersize=8, markerfacecolor=\"red\")\n",
"ax[0,1].plot(x, x+7, color=\"blue\", lw=1, ls='-', marker='s', markersize=8, \n",
" markerfacecolor=\"yellow\", markeredgewidth=2, markeredgecolor=\"green\");"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Subplots"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fig, ax = plt.subplots(2, 3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fig, ax = plt.subplots(2, 3)\n",
"fig.tight_layout()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"\n"
],
"text": [
""
]
}
],
"prompt_number": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Colormap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"cmap with _r is a reversed one"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data = random.randn(50,50)\n",
"fig, ax = subplots(1,3)\n",
"\n",
"plt1 = ax[0].imshow(data,cmap='Greens')\n",
"fig.colorbar(plt1,ax=ax[0],fraction=0.05)\n",
"plt2 = ax[1].imshow(data,cmap='BuGn')\n",
"fig.colorbar(plt2,ax=ax[1],fraction=0.1)\n",
"plt3 = ax[2].imshow(data,cmap='Blues')\n",
"fig.colorbar(plt3,ax=ax[2])\n",
"fig.tight_layout()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"svg": [
"\n",
"\n",
"\n",
"