{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting\n", "\n", "There are various plotting/visualisation options in Python:\n", "- [**Matplotlib**](https://matplotlib.org/) *is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments *\n", "- [**seaborn**](https://seaborn.pydata.org/) *is a Python visualization library based on matplotlib that provides a high-level interface for drawing attractive statistical graphics.*\n", "- [**Bokeh**](https://bokeh.pydata.org/en/latest/) *is an interactive visualization library that targets modern web browsers for presentation.*\n", "- [**Plotly**](https://plot.ly/python/) *makes interactive, publication-quality graphs online*\n", "- [**Pygal**](http://pygal.org/en/stable/): *PYthon svg GrAph plotting Library*\n", "- [**ggplot**](http://ggplot.yhathq.com/) *is a plotting system for Python based on R's [ggplot2](http://ggplot2.org/) and the [Grammar of Graphics](https://www.amazon.com/Grammar-Graphics-Statistics-Computing/dp/0387245448).*\n", "\n", "All have pros and cons, and are at varying levels of 'maturity'. For example, Matplotlib is the oldest (2003), most complete and most 'traditional' - i.e. designed for making non-interactive graphs for printing. Matplotlib lacks some of the web-focussed tools of Bokeh and Plotly, which are relative newcomers focussed on generating plots to display electronically and on the web. Pygal and ggplot are more esoteric - the former focussed on producing plots in SVG format, and the latter focussed on emulating the syntax and appearance of the `ggplot2` R plotting package.\n", "\n", "In science we mostly focus on producing publication-quality plots for printing, so we'll focus on Matplotlib here.\n", "\n", "
\n", "\n", "Matplotlib is the most 'mature' and feature-rich Python plotting library. If you can think it, you can plot it... with a little effort. The downside of this flexibility is that it has *a lot* of options, which can be initially intimidating.\n", "\n", "A fantastic resource for getting started is the [Matplotlib Gallery](https://matplotlib.org/gallery.html) - a collection of plot examples showing different types of plots, and the code behind them.\n", "\n", "The part of matplotlib you'll interact with most is `pyplot`, which provides a MATLAB-like plotting interface. To import `pyploy`:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt # import pyplot, and call it 'plt'\n", "\n", "# this tells jupyter notebook to display plots 'in line' in the browser\n", "# you can also have interactivity by replacing 'inline' by 'notebook', but this can be much slower\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## First Plot" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np # import numpy for generating some data\n", "\n", "# generate some 'dummy' data\n", "x = np.linspace(0, 100, 50)\n", "y = x * 2.5 + np.random.normal(3, 23, 50)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[