{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "\n", "\n", "| - | - |\n", "|---------------------------------------------------------------------|---------------------------------------------------------------------|\n", "| [Exercise 9 (multiple graphs)](<#Exercise-9-(multiple-graphs)>) | [Exercise 10 (subfigures)](<#Exercise-10-(subfigures)>) |\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Matplotlib\n", "\n", "To understand the data better it helps to be able to visualize it in various ways. [Matplotlib](https://matplotlib.org/) is the most common low-level visualization library for Python. It can create line graphs, scatter plots, density plots, histograms, heatmaps, and so on. During this course we will not go deep into details of matplotlib, instead we have just some examples spread throughout the rest of this material of its use.\n", "\n", "## Simple figure\n", "We will start with an example. The standard way to import matplotlib is as the abbreviation `plt`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This is needed so that the plotted figures appear embedded in this notebook:\n", "%matplotlib inline \n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's first have some data to visualize:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a=np.array([2, 5, 7, 4, 7, 0, 3, 1, 9, 2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below the `plot` function does the actual drawing of the graph, the rest of the function calls adjust some details of the figure. Make sure you understand how the values in the array `a` correspond to the features in the figure! " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot(a) # plot the points in the array a\n", "plt.title(\"My first figure\") # Add a title to the figure\n", "plt.xlabel(\"My x-axis\") # Give a label to the x-axis\n", "plt.ylabel(\"My y-axis\"); # Give a label to the y-axis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The key components of any matplotlib figure and the terminology is shown in the below image. The toplevel object is `figure` and it can contain one or more `subfigures`, which are strangely called `axes` in matplotlib." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![components of a figure](example_figure2.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####