├── .gitignore ├── README.md ├── LICENSE ├── World-of-Jupyter.md ├── 1--The Notebook.ipynb └── 2--The Python world of science and data.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .ipynb_checkpoints/1--The Notebook-checkpoint.ipynb 3 | 4 | .ipynb_checkpoints/2--The Python world of science and data-checkpoint.ipynb 5 | 6 | .ipynb_checkpoints/3--Jupyter like a pro-checkpoint.ipynb 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jupyter Tutorial 2 | 3 | [![Binder](http://mybinder.org/badge.svg)](http://mybinder.org:/repo/barbagroup/jupyter-tutorial) 4 | 5 | * [**"The world of Jupyter"**](https://github.com/barbagroup/jupyter-tutorial/blob/master/World-of-Jupyter.md)—a tutorial held in Huazhong University of Science and Technology (HUST) in Wuhan, China (June 2016). 6 | 7 | --- 8 | 9 | ### License 10 | 11 | The content of this repository is shared under a Creative Commons Attribution License CC-BY 4.0. All code is under MIT License. 12 | 13 | (c) 2016 Barba group (Lorena A. Barba and co-authors, as indicated) 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Barba group 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /World-of-Jupyter.md: -------------------------------------------------------------------------------- 1 | # The world of Jupyter 2 | ## An environment to think, code, explore data and communicate science 3 | 4 | At the center of the Jupyter world are *notebooks*: documents containing a mix of rich text elements—headings, paragraph text, hyperlinks, mathematical symbols and embedded figures—and interactive code elements. The document uses "cells" to divide up the text and code elements: text is formatted using markdown, and code can be executed using the IPython kernel or others (like Julia or R language). 5 | 6 | You can use the notebooks to annotate ideas around code, to explain your thought process, or to report on research process and results. Having interactive code right there in the document empowers you to "think with code," trying things out and exploring data as you go. The notebook has also become a very popular platform for teaching and learning, with a growing community of users publishing lessons, even books, using notebooks. 7 | 8 | To work on notebooks, you run the Jupyter Notebook App and edit them from your favorite browser. The app gives you a dashboard on a new browser window that connects to the computational engine (called kernel) behind the scenes. You can install Jupyter locally on your computer, or use it on the cloud on paid or free services. You can also share your finished notebooks online as a static web page, via the free [`nbviewer`](http://nbviewer.jupyter.org) service. 9 | 10 | Everything in Project Jupyter is free and open source. Via the IPython kernel, you can access the whole ecosystem of Python with its libraries for numerical computing, visualization, data analysis, machine learning and more. Jupyter is a world of sharing code and content, and a productive environment to get things done with code and data. 11 | 12 | Get started with Jupyter in this tutorial, and then follow some of the many more available online. 13 | 14 | You can try Jupyter right now on this free service: [https://try.jupyter.org](https://try.jupyter.org). Click on the "Welcome to Python" link there: this demo is a temporary notebook with a single code cell that reads some data and draws a plot. Or you can instead click on the "New" button (top right) and choose Python 3 from the options, to get a blank notebook (also temporary). To try a notebook that you can save on the cloud, use the new free service by Azure: [https://notebooks.azure.com](https://notebooks.azure.com). If you want everything installed in your own laptop computer, download the Anaconda distribution (free) from: [https://www.continuum.io/downloads](https://www.continuum.io/downloads) 15 | 16 | ### Jupyter tutorial in Wuhan, June 2016 17 | 18 | For the Jupyter tutorial at **Huazhong University of Science and Technology** (Wuhan, China), by Prof. Lorena Barba, students should prepare by doing one of these: 19 | 20 | 1. Create an account on Microsoft **Azure** and test the free cloud notebooks at [https://notebooks.azure.com](https://notebooks.azure.com) — you need an account to be able to save notebooks for later. If you only access the free demos, your work will not be saved. 21 | 2. Install **Anaconda** in your laptop computer. Downloading from the [official website](https://www.continuum.io/downloads) could take a long time, but there is a mirror at Tsinghua University, according to the post in: [http://www.tuicool.com/articles/vyyA7rB](http://www.tuicool.com/articles/vyyA7rB) 22 | We found an error in the URL provided in that post, though. The actual location of the Tsinghua mirror is [https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/](https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/) 23 | Download the latest version for your operating system (we'll be using Python 3). 24 | After installing, from a terminal, run `conda update conda` and then run `conda update jupyter numpy sympy scipy matplotlib` 25 | 3. If your internet is slow or you want to save disk space, you can instead install **Miniconda** from the [official site](http://conda.pydata.org/miniconda.html) or from the [mirror](https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/) in Tsinghua University. Then, you should run three commands from the terminal: 26 | `conda update conda` 27 | `conda install jupyter` 28 | `conda install numpy scipy sympy matplotlib` 29 | 30 | ## Contents 31 | 32 | (Links to notebooks rendered by **nbviewer**.) 33 | 34 | 1. [The Notebook](http://nbviewer.jupyter.org/github/barbagroup/jupyter-tutorial/blob/master/1--The%20Notebook.ipynb) 35 | 2. [The Python world of science and data](http://nbviewer.jupyter.org/github/barbagroup/jupyter-tutorial/blob/master/2--The%20Python%20world%20of%20science%20and%20data.ipynb) 36 | 3. [Jupyter like a pro](http://nbviewer.jupyter.org/github/barbagroup/jupyter-tutorial/blob/master/3--Jupyter%20like%20a%20pro.ipynb) 37 | 38 | 39 | ### Interact with the notebooks on Azure cloud: 40 | 41 | You can interact with the notebooks right now, without downloading anything—thanks to the new cloud notebooks by Azure—by following this link: 42 | [https://notebooks.azure.com/library/Jbzl8X0qhHw](https://notebooks.azure.com/library/Jbzl8X0qhHw) -------------------------------------------------------------------------------- /1--The Notebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Notebook\n", 8 | "\n", 9 | "This is a _notebook_: a document that can contain rich text elements—headings, paragraph text, hyperlinks, mathematical symbols and embedded figures—and interactive code elements. The document uses \"cells\" to divide up the text and code elements: text is formatted using markdown, and code can be executed using the IPython kernel.\n", 10 | "\n", 11 | "[Markdown](https://en.wikipedia.org/wiki/Markdown) is an easy way to format text to be displayed on a browser. You can format headings, bold face, italics, add a horizonal line, hyperlink some text, add bulleted or numbered lists. It's so easy, you can learn it in minutes! The \"Daring Fireball\" (by John Gruber) [markdown syntax](https://daringfireball.net/projects/markdown/syntax) page is a nice starting point or reference." 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "## Nbviewer\n", 19 | "\n", 20 | "If you came to view this notebook following the link on the tutorial description, [\"The world of Jupyter\"](https://github.com/barbagroup/jupyter-tutorial/blob/master/World-of-Jupyter.md) (itself, a markdown file), then you are seeing it rendered as a static webpage on the free [**nbviewer**](http://nbviewer.jupyter.org) service. It is the simplest way to share a notebook with anyone: just host the notebook file (`.ipynb` extension) online and enter the public URL to the file on the **nbviewer** _Go!_ box. The notebook will be rendered like a static webpage: visitors can read everything, but they cannot interact with the code. \n", 21 | "\n", 22 | "To interact with a notebook, you need the Jupyter Notebook App, either locally installed on your computer or on some cloud service." 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "## Jupyter Notebook App\n", 30 | "\n", 31 | "If you want to see what the Jupyter Notebook App looks like, you can try it right now on this free service: [https://try.jupyter.org](https://try.jupyter.org). You should see the _Dashboard_, which shows a list of available files, like this:\n", 32 | "\n", 33 | "![dashboard](http://jupyter.readthedocs.io/en/latest/_images/tryjupyter_file.png)\n", 34 | "\n", 35 | "Click on the _New_ button on the top right, and choose \"Python 3\" from the pull-down options to get a notebook that is connected to the IPython kernel. You should get an empty notebook with a single empty code cell labeled `In[ ]`, with a flashing cursor inside. This is a code cell. You can start by trying any simple mathematical operation (like a calculator), and type `[shift] + [enter]` to execute it.\n", 36 | "\n", 37 | "You can try any of the arithmetic operators in Python:\n", 38 | "```python\n", 39 | " + - * / ** % //\n", 40 | "```\n", 41 | "The last three operators above are _exponent_ (raise to the power of), _modulo_ (divide and return remainder) and _floor division_. Be careful that the division operator behaves differently in legacy Python 2: it rounds to an integer when the operands are integers. We use Python 3 and don't worry about this, because it gives the expected result, e.g., `1/2` gives `0.5` (not zero!).\n", 42 | "\n", 43 | "Here's a simple example calculation in a code cell:" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 1, 49 | "metadata": { 50 | "collapsed": false 51 | }, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "24" 57 | ] 58 | }, 59 | "execution_count": 1, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "2 * 12" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "Typing `[shift] + [enter]` will execute the cell and give you the output in a new line, labeled `Out[1]` (the numbering increases each time you execute a cell). If you're at the end of the notebook, you will also get a new blank cell, where you can try more arithmetic operations, if you want. Or, you can change the type of cell from _Code_ to _Markdown_ with the button on the menu bar. See if you find that now, and then type some text into a markdwon cell. Try some [markdown syntax](https://daringfireball.net/projects/markdown/syntax)!\n", 73 | "\n", 74 | "In a code cell, you can enter any valid Python code, and type `[shift] + [enter]` to execute it. For example, the simplest code example just prints the message _Hello World!_, like this:" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 2, 80 | "metadata": { 81 | "collapsed": false 82 | }, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "Hello World!\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "print(\"Hello World!\")" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "You can also define a variable (say `x`) and assign it a value (say, 2), then use that variable in some code:" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 3, 106 | "metadata": { 107 | "collapsed": true 108 | }, 109 | "outputs": [], 110 | "source": [ 111 | "x = 2" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 4, 117 | "metadata": { 118 | "collapsed": false 119 | }, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "Hello World!Hello World!\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "print(x * \"Hello World!\")" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 5, 136 | "metadata": { 137 | "collapsed": false 138 | }, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "24" 144 | ] 145 | }, 146 | "execution_count": 5, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "x * 12" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "## More Jupyter cloud services\n", 160 | "\n", 161 | "If you followed the instructions above, you launched the Jupyter Notebook App on a cloud service called **`tmpnb`**. As the prefix `tmp` indicates, it gives you a temporary demo: as soon as you close the browser tab (or after a few minutes of inactivity), the kernel dies and the content you wrote is not saved anywhere. This is a free service, sponsored by the company Rackspace, just for trying something out.\n", 162 | "\n", 163 | "You can work on notebooks and save your work using other cloud services. A long-running cloud offering is [SageMathCloud](https://cloud.sagemath.com). And most recently (in June 2016), Microsoft announced [notebooks hosted on Azure](https://notebooks.azure.com) cloud. In both cases, you need to create a free account to be able to save your work." 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "## The world of Jupyter, so far\n", 171 | "\n", 172 | "You learned about: \n", 173 | "\n", 174 | "* the notebook, \n", 175 | "* code cells and markdown cells, \n", 176 | "* **nbviewer**, \n", 177 | "* the Jupyter Notebook App, \n", 178 | "* `[shift] + [enter]` to execute, \n", 179 | "* **`tmpnb`**, and\n", 180 | "* cloud notebook services." 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "## Optional next step: local installation\n", 188 | "\n", 189 | "Cloud notebook services (especially free ones!) have their limitations. As you start using notebooks more, you'll want to **install Jupyter** on your personal computer. For that, we recommend the free [Anaconda](https://www.continuum.io/downloads) distribution, which includes _all_ the Python packages you may need (more than 700 packages are included!).\n", 190 | "\n", 191 | "If you prefer a light installation (e.g., if your internet is slow), we can recommend [Miniconda](http://conda.pydata.org/miniconda.html). This will include only `conda` (the package manager) and its dependencies, and Python. You will have to install Jupyter separately, and also other basic Python libraries for numerical mathematics and data analysis, using the `conda install` command.\n", 192 | "\n", 193 | "We also recommend that you start with Python 3 right away—Python 2.7 is the _legacy_ Python, and you should only need to use it if you have a lot of code written in the past that you need to work with.\n" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "# Next\n", 201 | "\n", 202 | "* Get started with a foundation on Python for numerical mathematics and data analysis: [The Python world of science and data](http://nbviewer.jupyter.org/github/barbagroup/jupyter-tutorial/blob/master/2--The%20Python%20world%20of%20science%20and%20data.ipynb)" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "---\n", 210 | "\n", 211 | "

(c) 2016 Lorena A. Barba. Free to use under Creative Commons Attribution CC-BY 4.0 License. This notebook was written for the tutorial \"The world of Jupyter\" at the Huazhong University of Science and Technology (HUST), Wuhan, China.\n", 212 | "

" 213 | ] 214 | } 215 | ], 216 | "metadata": { 217 | "kernelspec": { 218 | "display_name": "Python 3", 219 | "language": "python", 220 | "name": "python3" 221 | }, 222 | "language_info": { 223 | "codemirror_mode": { 224 | "name": "ipython", 225 | "version": 3 226 | }, 227 | "file_extension": ".py", 228 | "mimetype": "text/x-python", 229 | "name": "python", 230 | "nbconvert_exporter": "python", 231 | "pygments_lexer": "ipython3", 232 | "version": "3.5.1" 233 | } 234 | }, 235 | "nbformat": 4, 236 | "nbformat_minor": 0 237 | } 238 | -------------------------------------------------------------------------------- /2--The Python world of science and data.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The Python world of science and data\n", 8 | "\n", 9 | "The [Jupyter notebook](http://nbviewer.jupyter.org/github/barbagroup/jupyter-tutorial/blob/master/1--The%20Notebook.ipynb) is a nice way to write and share some content online, but you'll get superpowers from Python libraries for science and data!\n", 10 | "\n", 11 | "We already used code cells to do simple calculations via the arithmetic operators in Python:\n", 12 | "```python\n", 13 | " + - * / ** % //\n", 14 | "```\n", 15 | "\n", 16 | "\n", 17 | "\n", 18 | "In addition to arithmetics, you can do comparisons with operators that return Boolean values (`True` or `False`). These are:\n", 19 | "```python\n", 20 | " == != < > <= >=\n", 21 | "```\n", 22 | "\n", 23 | "On top of those, you have assignment operators, bitwise operators, logical operators, membership operators, and identity operators. You can find online several \"cheat sheets\" for Python operators. For example: [\"Operators and expressions\"](http://pymbook.readthedocs.io/en/latest/operatorsexpressions.html) in the online book _\"Python for You and Me.\"_\n", 24 | "\n", 25 | "Go ahead and experiment with various Python operators until you're satisfied. You can open a new, empty notebook and experiment in code cells, taking notes of the things that you find interesting in markdown cells. Or, if you have _this_ notebook open in the Jupyter Notebook App, you can add a new cell by clicking the plus button: , and work right here. Remember to type `[shift] + [enter]` to exectue any cell.\n", 26 | "\n", 27 | "Next, let's learn about the Python world of science and data." 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "## Two libraries that made Python useful for science: NumPy and Matplotlib\n", 35 | "\n", 36 | "Python is a general-purpose language: you can use it to create websites, to write programs that crawl the web, to support you in scientific research or data analysis, etc. Because it can be used in so many fields, the core language is supported by many libraries (not everyone needs to have every functionality). In science, two libraries made Python really useful: [**NumPy**](http://www.numpy.org) and [**Matplotlib**](http://matplotlib.org).\n", 37 | "\n", 38 | "**NumPy** gives you access to numerical mathematics on arrays (like vectors and matrices). **Matplotlib** gives you a catalog of plotting functions for visualizing data." 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 1, 44 | "metadata": { 45 | "collapsed": true 46 | }, 47 | "outputs": [], 48 | "source": [ 49 | "import numpy" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "The command `import` followed by the name of a library will extend your Python session with all of the functions in that library. After executing the code cell above, we have all of **NumPy** available to us. If you have this notebook open in the Jupyter Notebook App, make sure to execute the cell above by clicking on it and typing ``[shift] + [enter]``.\n", 57 | "\n", 58 | "Now, to use one of **NumPy**'s functions, we prepend `numpy.` (with the dot) to the function name. For example:" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 2, 64 | "metadata": { 65 | "collapsed": false 66 | }, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "array([ 0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222,\n", 72 | " 2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])" 73 | ] 74 | }, 75 | "execution_count": 2, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | "numpy.linspace(0, 5, 10)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "The **NumPy** function [`linspace()`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html) creates an array with equally spaced numbers between a start and end. It's a very useful function! In the code cell above, we create an array of 10 numbers from 0 to 5. Go ahead and try it with different argument values.\n", 89 | "\n", 90 | "To be able to do something with this array later, we normally want to give it a name. Like," 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 3, 96 | "metadata": { 97 | "collapsed": true 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "xarray = numpy.linspace(0, 5, 10)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "Now, we can use **NumPy** to do computations with the array. Like take its square:" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 4, 114 | "metadata": { 115 | "collapsed": false 116 | }, 117 | "outputs": [ 118 | { 119 | "data": { 120 | "text/plain": [ 121 | "array([ 0. , 0.30864198, 1.2345679 , 2.77777778,\n", 122 | " 4.9382716 , 7.71604938, 11.11111111, 15.12345679,\n", 123 | " 19.75308642, 25. ])" 124 | ] 125 | }, 126 | "execution_count": 4, 127 | "metadata": {}, 128 | "output_type": "execute_result" 129 | } 130 | ], 131 | "source": [ 132 | "xarray ** 2" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "In **NumPy**, the square of an array of numbers takes the square of each element. You will likely want to give your result a name, too. So let's do this again, and also take the cube, and the square root of the array at the same time." 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 5, 145 | "metadata": { 146 | "collapsed": true 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "yarray = xarray ** 2\n", 151 | "zarray = xarray ** 3\n", 152 | "warray = numpy.sqrt(xarray)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "You notice that **NumPy** knows how to take the power of an array, and it has a built-in function for the square-root. Now, you may want to draw a plot of these results with the original array on the x-axis. For that we need the module `pyplot` from **Matplotlib**." 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 6, 165 | "metadata": { 166 | "collapsed": true 167 | }, 168 | "outputs": [], 169 | "source": [ 170 | "from matplotlib import pyplot\n", 171 | "%matplotlib notebook" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "The command `%matplotlib notebook` is there to get our plots inside the notebook (instead of a pop-up window, which is the default behavior of `pyplot`). Let's try a line plot now! We use the `pyplot.plot()` function, specifying the line color (`'k'` for black) and line style (`'-'`, `'--'` and `':'` for continuous, dashed and dotted line), and giving each line a label. Note that the values for `color`, `linestyle` and `label` are given in quotes." 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 7, 184 | "metadata": { 185 | "collapsed": false 186 | }, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "application/javascript": [ 191 | "/* Put everything inside the global mpl namespace */\n", 192 | "window.mpl = {};\n", 193 | "\n", 194 | "mpl.get_websocket_type = function() {\n", 195 | " if (typeof(WebSocket) !== 'undefined') {\n", 196 | " return WebSocket;\n", 197 | " } else if (typeof(MozWebSocket) !== 'undefined') {\n", 198 | " return MozWebSocket;\n", 199 | " } else {\n", 200 | " alert('Your browser does not have WebSocket support.' +\n", 201 | " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", 202 | " 'Firefox 4 and 5 are also supported but you ' +\n", 203 | " 'have to enable WebSockets in about:config.');\n", 204 | " };\n", 205 | "}\n", 206 | "\n", 207 | "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", 208 | " this.id = figure_id;\n", 209 | "\n", 210 | " this.ws = websocket;\n", 211 | "\n", 212 | " this.supports_binary = (this.ws.binaryType != undefined);\n", 213 | "\n", 214 | " if (!this.supports_binary) {\n", 215 | " var warnings = document.getElementById(\"mpl-warnings\");\n", 216 | " if (warnings) {\n", 217 | " warnings.style.display = 'block';\n", 218 | " warnings.textContent = (\n", 219 | " \"This browser does not support binary websocket messages. \" +\n", 220 | " \"Performance may be slow.\");\n", 221 | " }\n", 222 | " }\n", 223 | "\n", 224 | " this.imageObj = new Image();\n", 225 | "\n", 226 | " this.context = undefined;\n", 227 | " this.message = undefined;\n", 228 | " this.canvas = undefined;\n", 229 | " this.rubberband_canvas = undefined;\n", 230 | " this.rubberband_context = undefined;\n", 231 | " this.format_dropdown = undefined;\n", 232 | "\n", 233 | " this.image_mode = 'full';\n", 234 | "\n", 235 | " this.root = $('
');\n", 236 | " this._root_extra_style(this.root)\n", 237 | " this.root.attr('style', 'display: inline-block');\n", 238 | "\n", 239 | " $(parent_element).append(this.root);\n", 240 | "\n", 241 | " this._init_header(this);\n", 242 | " this._init_canvas(this);\n", 243 | " this._init_toolbar(this);\n", 244 | "\n", 245 | " var fig = this;\n", 246 | "\n", 247 | " this.waiting = false;\n", 248 | "\n", 249 | " this.ws.onopen = function () {\n", 250 | " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", 251 | " fig.send_message(\"send_image_mode\", {});\n", 252 | " fig.send_message(\"refresh\", {});\n", 253 | " }\n", 254 | "\n", 255 | " this.imageObj.onload = function() {\n", 256 | " if (fig.image_mode == 'full') {\n", 257 | " // Full images could contain transparency (where diff images\n", 258 | " // almost always do), so we need to clear the canvas so that\n", 259 | " // there is no ghosting.\n", 260 | " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", 261 | " }\n", 262 | " fig.context.drawImage(fig.imageObj, 0, 0);\n", 263 | " };\n", 264 | "\n", 265 | " this.imageObj.onunload = function() {\n", 266 | " this.ws.close();\n", 267 | " }\n", 268 | "\n", 269 | " this.ws.onmessage = this._make_on_message_function(this);\n", 270 | "\n", 271 | " this.ondownload = ondownload;\n", 272 | "}\n", 273 | "\n", 274 | "mpl.figure.prototype._init_header = function() {\n", 275 | " var titlebar = $(\n", 276 | " '
');\n", 278 | " var titletext = $(\n", 279 | " '
');\n", 281 | " titlebar.append(titletext)\n", 282 | " this.root.append(titlebar);\n", 283 | " this.header = titletext[0];\n", 284 | "}\n", 285 | "\n", 286 | "\n", 287 | "\n", 288 | "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", 289 | "\n", 290 | "}\n", 291 | "\n", 292 | "\n", 293 | "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", 294 | "\n", 295 | "}\n", 296 | "\n", 297 | "mpl.figure.prototype._init_canvas = function() {\n", 298 | " var fig = this;\n", 299 | "\n", 300 | " var canvas_div = $('
');\n", 301 | "\n", 302 | " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", 303 | "\n", 304 | " function canvas_keyboard_event(event) {\n", 305 | " return fig.key_event(event, event['data']);\n", 306 | " }\n", 307 | "\n", 308 | " canvas_div.keydown('key_press', canvas_keyboard_event);\n", 309 | " canvas_div.keyup('key_release', canvas_keyboard_event);\n", 310 | " this.canvas_div = canvas_div\n", 311 | " this._canvas_extra_style(canvas_div)\n", 312 | " this.root.append(canvas_div);\n", 313 | "\n", 314 | " var canvas = $('');\n", 315 | " canvas.addClass('mpl-canvas');\n", 316 | " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", 317 | "\n", 318 | " this.canvas = canvas[0];\n", 319 | " this.context = canvas[0].getContext(\"2d\");\n", 320 | "\n", 321 | " var rubberband = $('');\n", 322 | " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", 323 | "\n", 324 | " var pass_mouse_events = true;\n", 325 | "\n", 326 | " canvas_div.resizable({\n", 327 | " start: function(event, ui) {\n", 328 | " pass_mouse_events = false;\n", 329 | " },\n", 330 | " resize: function(event, ui) {\n", 331 | " fig.request_resize(ui.size.width, ui.size.height);\n", 332 | " },\n", 333 | " stop: function(event, ui) {\n", 334 | " pass_mouse_events = true;\n", 335 | " fig.request_resize(ui.size.width, ui.size.height);\n", 336 | " },\n", 337 | " });\n", 338 | "\n", 339 | " function mouse_event_fn(event) {\n", 340 | " if (pass_mouse_events)\n", 341 | " return fig.mouse_event(event, event['data']);\n", 342 | " }\n", 343 | "\n", 344 | " rubberband.mousedown('button_press', mouse_event_fn);\n", 345 | " rubberband.mouseup('button_release', mouse_event_fn);\n", 346 | " // Throttle sequential mouse events to 1 every 20ms.\n", 347 | " rubberband.mousemove('motion_notify', mouse_event_fn);\n", 348 | "\n", 349 | " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", 350 | " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", 351 | "\n", 352 | " canvas_div.on(\"wheel\", function (event) {\n", 353 | " event = event.originalEvent;\n", 354 | " event['data'] = 'scroll'\n", 355 | " if (event.deltaY < 0) {\n", 356 | " event.step = 1;\n", 357 | " } else {\n", 358 | " event.step = -1;\n", 359 | " }\n", 360 | " mouse_event_fn(event);\n", 361 | " });\n", 362 | "\n", 363 | " canvas_div.append(canvas);\n", 364 | " canvas_div.append(rubberband);\n", 365 | "\n", 366 | " this.rubberband = rubberband;\n", 367 | " this.rubberband_canvas = rubberband[0];\n", 368 | " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", 369 | " this.rubberband_context.strokeStyle = \"#000000\";\n", 370 | "\n", 371 | " this._resize_canvas = function(width, height) {\n", 372 | " // Keep the size of the canvas, canvas container, and rubber band\n", 373 | " // canvas in synch.\n", 374 | " canvas_div.css('width', width)\n", 375 | " canvas_div.css('height', height)\n", 376 | "\n", 377 | " canvas.attr('width', width);\n", 378 | " canvas.attr('height', height);\n", 379 | "\n", 380 | " rubberband.attr('width', width);\n", 381 | " rubberband.attr('height', height);\n", 382 | " }\n", 383 | "\n", 384 | " // Set the figure to an initial 600x600px, this will subsequently be updated\n", 385 | " // upon first draw.\n", 386 | " this._resize_canvas(600, 600);\n", 387 | "\n", 388 | " // Disable right mouse context menu.\n", 389 | " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", 390 | " return false;\n", 391 | " });\n", 392 | "\n", 393 | " function set_focus () {\n", 394 | " canvas.focus();\n", 395 | " canvas_div.focus();\n", 396 | " }\n", 397 | "\n", 398 | " window.setTimeout(set_focus, 100);\n", 399 | "}\n", 400 | "\n", 401 | "mpl.figure.prototype._init_toolbar = function() {\n", 402 | " var fig = this;\n", 403 | "\n", 404 | " var nav_element = $('
')\n", 405 | " nav_element.attr('style', 'width: 100%');\n", 406 | " this.root.append(nav_element);\n", 407 | "\n", 408 | " // Define a callback function for later on.\n", 409 | " function toolbar_event(event) {\n", 410 | " return fig.toolbar_button_onclick(event['data']);\n", 411 | " }\n", 412 | " function toolbar_mouse_event(event) {\n", 413 | " return fig.toolbar_button_onmouseover(event['data']);\n", 414 | " }\n", 415 | "\n", 416 | " for(var toolbar_ind in mpl.toolbar_items) {\n", 417 | " var name = mpl.toolbar_items[toolbar_ind][0];\n", 418 | " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", 419 | " var image = mpl.toolbar_items[toolbar_ind][2];\n", 420 | " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", 421 | "\n", 422 | " if (!name) {\n", 423 | " // put a spacer in here.\n", 424 | " continue;\n", 425 | " }\n", 426 | " var button = $('