Matplotlib Cheat Sheet π
Part 1
Here is the Zero to Hero cheat sheet for creating plots using the Pandas plotting library Matplotlib. This is not an all inclusive cheat sheet but you should feel fairly confident with your beginner plotting abilities once youβve gone through it two or three times.
Our first plot π
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
Letβs first create the most simple plot possible. Then see what modifications we can make. The plot below just takes three inputs for the x-axis and y-axis as any array and plots them. Note the code beneath each image specifies how each plot is created.
If you are coding along be sure to add plt.show( ) at the end of your code in order to view your plot. Go ahead and duplicate what you see below. The first bracket contains x coordinates while the second bracket contains y coordinates.
Next we will give our plot a title as well as a label for the x-axis and the y-axis.
We can also change the size of our graph. Look beneath the images for the additional code we will be adding to our existing code block or any variations to our code in order to change our visualization.
Previously utilizing plt.plot we passed in two arguments. One array for the x coordinates and another array for the y coordinates. Note that the default color and appearance for our plotted points is a blue line. We can pass a third argument that allows us to change the color of our line and/or turn our line into points. βgβ for green βrβ for red. βgoβ for green dots and βroβ for red dots.
It is also possible to plot multiple sets of data within the same plot by passing multiple arguments within the plot( ) method
np.arange returns evenly spaced values within a given interval. For our plot np.arange gave us four x values then sets y equal to the cubed value of each x value.
Now letβs create multiple plots in one figure. This can be done by using the subplot( ) method. The subplot( ) method takes three arguments nrows(), ncols() and index. These arguments indicate the number of rows the number of columns and the index number of the subplot. Lets create a figure with two subplots side-by-side. We want our figure to look like the image below.
If we prefer to have our plots in a vertical row all we have to do is change the plt.subplot () argument to (2 , 1 ,1) and (2 , 1 , 2). 2 rows, 1 column, position 1 or position 2.
Creating two subplots is fairly easy but when we try and create more subplots the method above can become tedious. In order to overcome this we can use the plt.subplots( ) method. This method creates two objects ( figure and axis ) and allows us to assign them arbitrary variable names to ( fig & ax ). We then assign the variables to the subplot method that indicates the number of rows, the number of columns and the figure size. ( this designates how many plots we want to create).
In Part 2 we will dive into plotting different types of plots using pyplot.
Bar graphs, Pie Charts, Histograms and so on.