Ipython Interactive Computing and Visualization Cookbook Cyrille Rossant download Z-Library. Download books for free.
Jupyter notebook tutorial on how to install, run, and use Jupyter for interactive matplotlib plotting, data analysis, and publishing code
I tried converting the notebook to HTML but the results were horrible. Nikola and pelican (with plugin) support ipython notebook but I love hugo and would really love to use the notebooks on it. 次の行をIPythonノートブック(OS 10.8.2、Google Chromeブラウザ)のマークダウンセルにレンダリングします:$$ fracddxg(x)= f(x)$$通常のズームレベルでは、これで.
Plotly is a free and open-source graphing library for Python. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.
Introduction¶
Jupyter has a beautiful notebook that lets you write and execute code, analyze data, embed content, and share reproducible work. Jupyter Notebook (previously referred to as IPython Notebook) allows you to easily share your code, data, plots, and explanation in a sinle notebook. Publishing is flexible: PDF, HTML, ipynb, dashboards, slides, and more. Code cells are based on an input and output format. For example:
Installation¶
There are a few ways to use a Jupyter Notebook:
- Install with
pip
. Open a terminal and type:$ pip install jupyter
. - Windows users can install with
setuptools
. - Anaconda and Enthought allow you to download a desktop version of Jupyter Notebook.
- nteract allows users to work in a notebook enviornment via a desktop application.
- Microsoft Azure provides hosted access to Jupyter Notebooks.
- Domino Data Lab offers web-based Notebooks.
- tmpnb launches a temporary online Notebook for individual users.
Getting Started¶
Once you've installed the Notebook, you start from your terminal by calling $ jupyter notebook
. This will open a browser on a localhost to the URL of your Notebooks, by default http://127.0.0.1:8888. Windows users need to open up their Command Prompt. You'll see a dashboard with all your Notebooks. You can launch your Notebooks from there. The Notebook has the advantage of looking the same when you're coding and publishing. You just have all the options to move code, run cells, change kernels, and use Markdown when you're running a NB.
Helpful Commands¶
- Tab Completion: Jupyter supports tab completion! You can type object_name.<TAB>
to view an object’s attributes. For tips on cell magics, running Notebooks, and exploring objects, check out the Jupyter docs.
- Help: provides an introduction and overview of features.
- Quick Reference: open quick reference by running:
- Keyboard Shortcuts:Shift-Enter
will run a cell, Ctrl-Enter
will run a cell in-place, Alt-Enter
will run a cell and insert another below. See more shortcuts here.
Languages¶
The bulk of this tutorial discusses executing python code in Jupyter notebooks. You can also use Jupyter notebooks to execute R code. Skip down to the [R section] for more information on using IRkernel with Jupyter notebooks and graphing examples.
Package Management¶
When installing packages in Jupyter, you either need to install the package in your actual shell, or run the !
prefix, e.g.:
You may want to reload submodules if you've edited the code in one. IPython comes with automatic reloading magic. You can reload all changed modules before executing a new line.
Some useful packages that we'll use in this tutorial include:
- Pandas: import data via a url and create a dataframe to easily handle data for analysis and graphing. See examples of using Pandas here: https://plotly.com/pandas/.
- NumPy: a package for scientific computing with tools for algebra, random number generation, integrating with databases, and managing data. See examples of using NumPy here: https://plotly.com/numpy/.
- SciPy: a Python-based ecosystem of packages for math, science, and engineering.
- Plotly: a graphing library for making interactive, publication-quality graphs. See examples of statistic, scientific, 3D charts, and more here: https://plotly.com/python.
Import Data¶
You can use pandas read_csv()
function to import data. In the example below, we import a csv hosted on github and display it in a table using Plotly:
Use dataframe.column_title
to index the dataframe:
Most pandas functions also work on an entire dataframe. For example, calling std()
calculates the standard deviation for each column.
Plotting Inline¶
You can use Plotly's python API to plot inside your Jupyter Notebook by calling plotly.plotly.iplot()
or plotly.offline.iplot()
if working offline. Plotting in the notebook gives you the advantage of keeping your data analysis and plots in one place. Now we can do a bit of interactive plotting. Head to the Plotly getting started page to learn how to set your credentials. Calling the plot with iplot
automaticallly generates an interactive version of the plot inside the Notebook in an iframe. See below:
Plotting multiple traces and styling the chart with custom colors and titles is simple with Plotly syntax. Additionally, you can control the privacy with sharing
set to public
, private
, or secret
.
Now we have interactive charts displayed in our notebook. Hover on the chart to see the values for each bar, click and drag to zoom into a specific section or click on the legend to hide/show a trace.
Plotting Interactive Maps¶
Plotly is now integrated with Mapbox. In this example we'll plot lattitude and longitude data of nuclear waste sites. To plot on Mapbox maps with Plotly you'll need a Mapbox account and a Mapbox Access Token which you can add to your Plotly settings.
3D Plotting¶
Using Numpy and Plotly, we can make interactive 3D plots in the Notebook as well.
Animated Plots¶
Checkout Plotly's animation documentation to see how to create animated plots inline in Jupyter notebooks like the Gapminder plot displayed below:
Plot Controls & IPython widgets¶
Add sliders, buttons, and dropdowns to your inline chart:
Additionally, IPython widgets allow you to add sliders, widgets, search boxes, and more to your Notebook. See the widget docs for more information. For others to be able to access your work, they'll need IPython. Or, you can use a cloud-based NB option so others can run your work.
Executing R Code¶
IRkernel, an R kernel for Jupyter, allows you to write and execute R code in a Jupyter notebook. Checkout the IRkernel documentation for some simple installation instructions. Once IRkernel is installed, open a Jupyter Notebook by calling $ jupyter notebook
and use the New dropdown to select an R notebook.
See a full R example Jupyter Notebook here: https://plotly.com/~chelsea_lyn/14069
Additional Embed Features¶
We've seen how to embed Plotly tables and charts as iframes in the notebook, with IPython.display
we can embed additional features, such a videos. For example, from YouTube:
LaTeX¶
We can embed LaTeX inside a Notebook by putting a $$
around our math, then run the cell as a Markdown cell. For example, the cell below is $$c = sqrt{a^2 + b^2}$$
, but the Notebook renders the expression.
Or, you can display output from Python, as seen here.
Exporting & Publishing Notebooks¶
We can export the Notebook as an HTML, PDF, .py, .ipynb, Markdown, and reST file. You can also turn your NB into a slideshow. You can publish Jupyter Notebooks on Plotly. Simply visit plot.ly and select the + Create
button in the upper right hand corner. Select Notebook and upload your Jupyter notebook (.ipynb) file!The notebooks that you upload will be stored in your Plotly organize folder and hosted at a unique link to make sharing quick and easy.See some example notebooks:
Publishing Dashboards¶
Users publishing interactive graphs can also use Plotly's dashboarding tool to arrange plots with a drag and drop interface. These dashboards can be published, embedded, and shared.
Publishing Dash Apps¶
For users looking to ship and productionize Python apps, dash is an assemblage of Flask, Socketio, Jinja, Plotly and boiler plate CSS and JS for easily creating data visualization web-apps with your Python data analysis backend.
Jupyter Gallery¶
For more Jupyter tutorials, checkout Plotly's python documentation: all documentation is written in jupyter notebooks that you can download and run yourself or checkout these user submitted examples!
The IPython Notebook is a web-based graphical interface to Notebook documents.This Quick Start describes how to:
- start the IPython Notebook
- load sample data from a netCDF file
- draw the data into a map, using various projections
Starting IPython¶
Let’s look at two ways to use IPython Notebook: local (the default); andremote using a web browser on another computer. The second option isuseful when running OSGeo Live in a Virtual Machine (VM).
Local IPython Notebook¶
Use the application launch menu in the lower left corner.
- Choose Geospatial -> Spatial Tools -> Start IPython Notebook
A terminal window will open and a startup log will begin to scroll.Next
- Choose Geospatial -> Spatial Tools -> IPython Notebook
A web browser is automatically launched pointing to the localhost URL atthe IPython port number (you should see the Notebook main page).When IPython is launched, the current directory is read for Notebooks.Any sub-directory is shown in the listing (“tree”). The user cannavigate up and down the tree by clicking on a directory, or open a Notebooklisted by clicking on it. On the computer, Notebook files are just a single textfile ending in suffix .ipynb, and are displayed by their filename.Renaming a Notebook simply changes the filename. Creating a new directory will automatically become visible in the IPython Notebook tree browser.
Remote IPython Notebook¶
- open a terminal window by clicking on the green terminal icon
- execute ipython notebook —ip=* —matplotlib=inline —no-browser –port=8883
- determine the IP address of your computer or VM, for example using:ifconfig -a | grep 192
Use a web browser from another computer and enter the IP address from above,along with the IPython Notebook port ‘:8888’. e.g. 192.168.1.13:8883
Note: the default http port number for IPython Notebook is 8888. On theOSGeo-Live the Notebook port is set to 8883 to prevent address conflictswith other web applications that use this common number.
Using the IPython Notebook¶
With the IPython Notebook main page open, you should see the directory tree browser interface, including a directory called OSGeo-live
, and a Notebook called cartopy-simple.ipynb
.Click on the link titled cartopy_simple.ipynb
. A new tab should open in theweb browser, showing the Notebook interface. Try moving the mouse overeach button in the toolbar and hold it there, to see the tooltip titles. Alsotake a moment to click on the menus to begin to become familiar with their contents.
Take particular note of the Help menu. You can try the clever User Interface Tour and open the Keyboard Shortcutscheatsheet, however you will need an active Internet connection to use the other Help topics.
The Notebook interface is a list of cells. Each cell has a type, forexample a heading or python code, and consists of one or more lines of text, code, inline image tags or other script. Any cell of type code can be executed by making it active and then clicking on the run button, or using the Run keyboard shortcut Shift-Return.
Try clicking on the first code cell, which includes Import numpy as npand executing it. You should see no error messages, and the next cell downwardsbecomes active.
To start this Quickstart, it is useful to clear all the stored output in the Notebook. Choose Cell -> All Output -> Clear . All output in the Notebook is now removed.
Importing Data with Python netCDF4¶
The first Notebook code cell imports important libraries to the active Python interpreter. The second cell assigns a string pathname into variable my_example_nc_file
, and then initializes a netCDF4 Dataset object fh
.Finally, the third cell reads three Dataset object variables into localnumpy ndarrays. For more information about numpy, see http://www.numpy.org/ .
When you click in the cell with the heading Import Cartopy, Display Example Maps notice the cell type in the toolbar area changes to Markdown.
Ipython Display Markdown
Ipython Markdown New Line
Display Data using Cartopy¶
Ipython Markdown Image
Click or navigate to the next code cell, which begins with %matplotlibinline
.This is an IPython Notebook Cell “Magic” - special commands interpreted by the Notebookto change the environment or behavior of the Notebook. This directive tells theNotebook to place matplotlib graphics directly in the window contents, instead of creating a new window. Execute the cell to import matplotlib and cartopy.
In the rest of the Notebook, each cell will draw the sample netCDF data in a different mapping projection. Try executing each cell, and see the results projected in different ways.