<jupyter_start><jupyter_text>|                                  -                                  |                                  -                                  ||---------------------------------------------------------------------|---------------------------------------------------------------------|| [Exercise 9 (multiple graphs)]() |     [Exercise 10 (subfigures)]()     |  MatplotlibTo 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. Simple figureWe will start with an example. The standard way to import matplotlib is as the abbreviation `plt`.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Let's first have some data to visualize:<jupyter_code>a=np.array([2, 5, 7, 4, 7, 0, 3, 1, 9, 2])<jupyter_output><empty_output><jupyter_text>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!<jupyter_code>plt.plot(a)                   # plot the points in the array a
plt.title("My first figure")  # Add a title to the figure
plt.xlabel("My x-axis")       # Give a label to the x-axis
plt.ylabel("My y-axis")       # Give a label to the y-axis
plt.show()                    # Tell matplotlib to output the figure.
                              # Not strictly required in notebooks (but a bit neater).<jupyter_output><empty_output><jupyter_text>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. ![components of a figure](example_figure2.png)  Exercise 9 (multiple graphs)In the above plot the x coordinates were implicitly set to the indices of the array `a`, that is, `arange(10)`. Find out from the documentation of `plt.plot` how to specify the x coordinates explicitly. Find out also how to draw multiple graphs in one axes.Make your `main` function plot the following two graphs in one axes. The first graphs has x coordinates 2,4,6,7 and y coordinates 4,3,5,1. The second graph has x coordinates 1,2,3,4 and y coordinates 4,2,3,1.Add also a title and some labels for x axis and y axis. Note that in the non-interactive mode you have to call `plt.show()` for the figure to show.The plot should look like the one below.![multiplot](multiplot.png)  Subfigures One can create a figure with several subfigures using the command `plt.subplots`. It creates a grid of subfigures, where the number of rows and columns in the grid are given as parameters. It returns a pair of a figure object and an array containing the subfigures. In matplotlib the subfigures are called `axes`. Note the one letter difference: axis is singular and axes is plural of axis. So, you can think of `axes` as the pair of x-axis and y-axis that defines a two dimensional (sub)figure. An example of this:<jupyter_code>fig, ax = plt.subplots(2,2)
print(ax.shape)
ax[0,0].plot(np.arange(6))          # top left
ax[0,1].plot(np.arange(6,0,-1))     # top right
ax[1,0].plot((-1)**np.arange(6))    # bottom left
ax[1,1].plot((-1)**np.arange(1,7))  # bottom right
plt.show()<jupyter_output>(2, 2)<jupyter_text>If you don't want to mess around with axes you can direct all your calls to pyplot (in a more matlabby way) like so:<jupyter_code>plt.subplot(2, 2, 1)    # Note the 1-indexing of subplots. 
plt.plot(np.arange(6))
plt.subplot(2, 2, 2)
plt.plot(np.arange(6, 0, -1))
plt.subplot(2, 2, 3)
plt.plot((-1)**np.arange(6))
plt.subplot(2, 2, 4)
plt.plot((-1)**np.arange(1, 7))
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12405001225790635, 'fraction_numerical': 0.018632017651385144, 'mean_word_length': 3.088176352705411, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '542616', 'license': 'MIT', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1161, 'n_words': 514, 'path': 'course-page/W03-02 matplotlib.ipynb', 'repo_name': 'ored95/data-analysis-course', 'size': '4079'}