Commit ecb3f8cc authored by Inigo Aldazabal's avatar Inigo Aldazabal

Split scipy and matplotlib notebooks

parent 666f47c2
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![Matplotlib](images/matplotlib_logo-s.png)\n",
"\n",
"# Matplotlib"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's take a quick look at the *matplotlib* essentials.\n",
"\n",
"First let's import the neccesary modules and tell the notebook to generate the figures inline:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `inline` is important for the notebook, so that plots are displayed\n",
"in the notebook and not in a new window.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can use (note that you have to use `show` explicitly if you don't use `%matplotlib inline`):\n",
"\n",
"```\n",
" plt.plot(x,y) # line plot\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"And now let's go with some examples."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic matplotlib visualization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* **1D plotting**:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.linspace(0, 4.*np.pi, 100)\n",
"y = np.sin(x)\n",
"plt.plot(x, y) # line plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot(x, y, 'o') # dot plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot(x, y)\n",
"plt.plot(x, y, 'o')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* **2D arrays** (such as images):\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image = np.random.rand(30, 30)\n",
"plt.imshow(image, cmap=plt.cm.hot)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise: Simple visualizations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Plot some simple arrays / functions.\n",
"\n",
"* Try using the `gray` colormap.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1D plot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A tangent between 0 and 4$\\pi$.\n",
"\n",
"$$y = \\tan(x), \\; x\\in[0., 4\\pi]$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.linspace( 0., 4.*np.pi, num=200 )\n",
"y = np.tan(x)\n",
"\n",
"plt.plot(x, y)\n",
"\n",
"plt.xlabel(r'$x$') #using LaTeX syntax\n",
"plt.ylabel(r'$\\tan(x)$')\n",
"plt.grid(True)\n",
"#plt.savefig('tan.png')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1D polar plot "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$\\theta(\\rho) = 2\\pi \\rho, \\; \\rho \\in [0, 2]$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r = np.arange(0, 2., 0.01)\n",
"theta = 2 * np.pi * r\n",
"\n",
"plt.polar(theta, r, color='r', linewidth=3)\n",
"plt.grid(True)\n",
"\n",
"plt.title(\"A line plot on a polar axis\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot the function \n",
"\n",
"$$y = \\frac{\\sin(x)}{x}, \\; x\\in[0.1, 6\\pi]$$\n",
"\n",
"and try to \n",
"\n",
"* set the figure *legend*\n",
"* change the *line style*\n",
"\n",
"hint: checkout the [matplotlib examples](http://matplotlib.org/examples/index.html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %load files/sol_sinc.py"
]
}
],
"metadata": {
"celltoolbar": "Raw Cell Format",
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![Matplotlib](images/matplotlib_logo-s.png)\n",
"\n",
"# Matplotlib"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's take a quick look at the matplotlib essentials.\n",
"\n",
"First let's import the neccesary modules:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"And now let's go with some examples."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1D plot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A tangent between 0 and 4$\\pi$.\n",
"\n",
"$$y = \\tan(x), \\; x\\in[0., 4\\pi]$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = np.linspace( 0., 4.*np.pi, num=200 )\n",
"y = np.tan(x)\n",
"\n",
"plt.plot(x, y)\n",
"\n",
"plt.xlabel(r'$x$') #using LaTeX syntax\n",
"plt.ylabel(r'$\\tan(x)$')\n",
"plt.grid(True)\n",
"#plt.savefig('tan.png')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1D polar plot "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$\\theta(\\rho) = 2\\pi \\rho, \\; \\rho \\in [0, 2]$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"r = np.arange(0, 2., 0.01)\n",
"theta = 2 * np.pi * r\n",
"\n",
"plt.polar(theta, r, color='r', linewidth=3)\n",
"plt.grid(True)\n",
"\n",
"plt.title(\"A line plot on a polar axis\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot the function \n",
"\n",
"$$y = \\frac{\\sin(x)}{x}, \\; x\\in[0.1, 6\\pi]$$\n",
"\n",
"and try to \n",
"\n",
"* set the figure *legend*\n",
"* change the *line style*\n",
"\n",
"hint: checkout the [matplotlib examples](http://matplotlib.org/examples/index.html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# %load files/sol_sinc.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment