<jupyter_start><jupyter_text>Guide to plotting with `matplotlib`This notebook will take you through the basics of making and manipulating plots with Matplotlib, a versatile package to quickly visualize data from Python. Here we will be learning:- Generating simple line and scatter plots- Adding plot aesthetics: titles, axis labels, axis ticks, etc- Subplots: making multiple figures and axesWe will be building on the skills learnt in previous notebooks, particularly [03. Math](https://github.com/panoptes/panoptes-tutorials/blob/master/python/03.%20Math.ipynb) and [13. Numpy](https://github.com/panoptes/panoptes-tutorials/blob/master/python/13.%20Numpy.ipynb).We will be using the `pyplot` module within matplotlib to plot figures, which contains functions that makes changes to a figure, e.g. creating the figure, making a plotting area, plotting lines/points, adding labels within the figure, etc. It works similar to MATLAB. Let's see how it works.First we will need to import the modules needed. For this notebook we will also be needing numpy.<jupyter_code># import needed modules
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>From now on, as you learned in the [10. Modules](https://github.com/panoptes/panoptes-tutorials/blob/master/python/10.%20Modules.ipynb) and [13. Numpy](https://github.com/panoptes/panoptes-tutorials/blob/master/python/13.%20Numpy.ipynb) notebooks, we will use the shorthand syntax `np.` and `plt.` whenever we want to call a function from *numpy* and *matplotlib.pyplot* respectively. This will make writting commands faster and easier.   Simple PlotLet's start by plotting some numbers on a figure using the `plot` function.<jupyter_code>plt.plot([1, 2, 3, 4])
#plt.show()<jupyter_output><empty_output><jupyter_text>Here we have plotted the numbers 1, 2, 3 and 4 along the y axis. Python does this by default when you provide the plot() command with a single list/array, as we have done. It also automatically generates x values of the same length as our array. As our array has 4 numbers, but python starts with 0, python generated an x array starting from 0 with 4 numbers, so [0,1,2,3]. -------------------------------Let's now plot one array on the x, and another on the y by specifying both of them in the plot() command.<jupyter_code>plt.plot([0,1,2,3], [1,2,3,4])<jupyter_output><empty_output><jupyter_text>This generates the exact same plot as before. We could also plot the same two arrays by assigning them to variables first before passing them onto the plot() command. Let's put them in numpy arrays for ease. This enables us to edit and manipulate the arrays as we like before doing any plotting.<jupyter_code># specify the array to plot on the x axis
x = np.array([0,1,2,3])
# specify the array to plot on the y axis
y = np.array([1,2,3,4])

# plot y vs. x
plt.plot(x,y)<jupyter_output><empty_output><jupyter_text>The default plot() command convention is specifying the desired x and y array, as we have done here, one after the other in the following order: `plot(, )`. These are the first two arguments.------------------------------------------------------------ Challenge 1: Simple plotMake a numpy array of 10 integers from 0 to 10, and another array of 10 numbers from 0.0 to 20.0 (in steps of 2), and assign each of them to individual variables. Make a plot of the second vs. first array.<jupyter_code># Make x array

# Make y array

# Plot y vs. x<jupyter_output><empty_output><jupyter_text>Scatter plot: formatting styleWe can change this line graph to a scatter plot by specifying an additional third argument. This can also format the colour of the plot. This is done the format string with a letter and symbol (from MATLAB). The default setting is `'b-'`, which stands for solid blue ('b') lines ('-'). To change this line graph style to a scatter plot, we use 'o' in the string after the letter. You can change the colours as well, from blue circle: 'bo' to red: 'ro' or even green: 'go'. You can even use triangles with '^'.Let's see try this. At the same time, let's generate some arrays automatically and use some math.We generate a numpy array of 10 equally spaced numbers to use as the x range using numpy's `arange` function. The y values will be an array of numbers twice that of the x range.<jupyter_code># arrays
x = np.arange(0, 10, 1) # note this will go from 0 to 9 in steps of 1
y = x * 2

# plotting
plt.plot(x, y, 'bo')<jupyter_output><empty_output><jupyter_text>-----------------------Let's now try plotting two different scatter plots in the same figure. This is done by repeating the same three agruments we just one after the other in the same `plot()` command. We will make them different colours to distinguish them.<jupyter_code># plot 1 arrays
x1 = np.arange(0, 10, 1)
y1 = x1 * 2

# plot 2 arrays
x2 = np.arange(0, 10, 2)
y2 = x2 + 5

# let's plot them
plt.plot(x1, y1, 'bo', x2, y2, 'ro')<jupyter_output><empty_output><jupyter_text>Challenge 2: Scatter plot of function $y = x^3$Generate two arrays: 1) numbers from 0 to 10 in step of 0.5, for the x range; and 2) values of array 1 raised to the power of 3 and assign it to variable y1. Make another y array of values that take the exponential value of array 1.Make a scatter plot of both y arrays vs. x on the same plot. Make sure they are different colours and shapes.<jupyter_code># Generate x array

# Generate two y array

# Make a scatter plot of y1 and y2 vs. x on the same plot.<jupyter_output><empty_output><jupyter_text>Adding axis labels and figure titlesIt is useful and informative to add labels to our axis as well as a title to our plot. This will make it easier to understand what is being plotted and what trends are visible.<jupyter_code># initiate figure
plt.figure()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09664082687338502, 'fraction_numerical': 0.020155038759689922, 'mean_word_length': 4.193202146690519, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '626654', 'license': 'MIT', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1640, 'n_words': 841, 'path': 'python/14. Matplotlib.ipynb', 'repo_name': 'jlibermann/panoptes-tutorials', 'size': '5805'}