<jupyter_start><jupyter_text>Introduction to matplotlib with `numpy`  Overview- **Teaching:** 5 min- **Exercises:** 10 min**Questions*** What is matplotlib?* Why should I use it?**Objectives*** See how to quickly plot numerical data. Plotting data in NumPy and pandas is handled by an external Python module called *matplotlib*. Like NumPy and pandas it is a large library and has been around for a while (first released in 2003). Hence we won't cover all its functionality in this lesson.To see the wide range of possibilities you have with matplotlib see [matplotlib example gallery](http://matplotlib.org/gallery.html), [<NAME>'s tutorial](http://www.labri.fr/perso/nrougier/teaching/matplotlib/other-types-of-plots) and [<NAME> tutorial](http://nbviewer.jupyter.org/github/matplotlib/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb) after the course.Here we will cover the basic uses of it and how it integrates with NumPy and in a later episode with pandas. While working through these examples you may want to refer to the [matplotlib documentation](http://matplotlib.org/api/pyplot_api.html).  Plotting using NumPy and matplotlibThe most common interface to matplotlib is its `pyplot` module which provides a way to affect the current state of matplotlib directly. As with both NumPy and pandas, there is a conventional way to import matplot lib, which is as follows:<jupyter_code>%config InlineBackend.figure_format = 'svg'

import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>It's very useful to be able to quickly plot any data we have in front of us. As we will see on the next page, matplotlib's power comes from its configurability.  Info: Plotting in notebooksYou may have noticed the line```python%config InlineBackend.figure_format = 'svg'```and wondered what it did. This line just tweaks Jupyter notebook's internal configuration, setting the figure format to be SVG, to make the plots that we generate look a little bit nicer. We recommend you also include this line, but feel free to experiment without it, or with other values. You may recall that in the "Introduction to Python" course, we met the sinc function. NumPy has its own implementation of the sinc function, which for those interested is defined as:$$\text{sinc}(x):=\begin{cases}\frac{\sin(\pi x)}{\pi x} &\text{ if } x\neq{0}\\1 & \text{ if } x = 0\end{cases}$$This function often crops up in signal processing applications.Beyond the abstract mathematical definition, we can use what we have learnt in Python to find out more about sinc.First we pick 1001 points on the x-axis between -5 and 5 using the `linspace` function. Then we can evaluate the `sinc` function at all 1001 points (recall that we can perform bulk operations on NumPy arrays).<jupyter_code>x = np.linspace(-5, 5, 1000)
# sinc(x) is defined to be sin(pi*x)/(pi*x) in numpy
y_sinc = np.sinc(x)<jupyter_output><empty_output><jupyter_text>Now `x` contains an array of x-coordinates and `y_sinc` contains an array of y-coordinates. We can plot y against x by calling matplotlib's `plot` function:<jupyter_code>plt.plot(x, y_sinc)<jupyter_output><empty_output><jupyter_text>You can see that it has plotted the sinc function between -5 and +5.  Info: matplotlib in notebooksDue to the nature of notebooks, you can see `[]` before the plot figure. Really here we should have called `plot.show()` to produce the plot, which is what we do in later examples. The interplay between notebooks and their content can be complex, but if you follow the guidlines here, you should at least be able to reproduce what you see. If you have ever drawn a graph before, you can probably remember someone shouting:"But you haven't labelled your axes! And there's no title!"We will go over this functionality now. Note that any functions called on the `plt` object will affect the state of the matplotlib plot from that point on in the script, until the `show` method is called.Firstly, we can add some additional information to the line that we plot, by passing the keyword argument `label` with a string that describes the line that is being drawn. Next, we call the `title` function to set a title for the plot. The `legend` function then causes the label that we set first to be displayed, by default, matplotlib will try and find the best position for the legend. Finally, calling the `show` function displays the plot so we can see it.<jupyter_code>plt.plot(x, y_sinc, label='sinc(x)')

# Set title and legend, then show plot
plt.title('The sinc function')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Looking at our new plot, it seems better, but we have still not labelled our axes. We can do this by calling `xlabel` and `ylabel` to set the x and y labels respectively. We can also change the location of the legend if we didn't like the position that matplotlib chose for us, using the `loc` keyword argument.<jupyter_code>plt.plot(x, y_sinc, label='sinc(x)')

# Set title and legend, then show plot
plt.title('The sinc function')
plt.legend(loc='upper left')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0777992277992278, 'fraction_numerical': 0.005598455598455598, 'mean_word_length': 5.131360946745562, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '384015', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1431, 'n_words': 720, 'path': 'nbplain/03_plotting.ipynb', 'repo_name': 'Research-Software-Skills-Bath/data-and-plotting', 'size': '5196'}