Matplotlib Cheat Sheet πŸ“Š

Part 1

Mulbah Kallen
Analytics Vidhya
4 min readAug 16, 2019

--

Overtime you will be able to create plots like this with ease.

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 πŸ“‰

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.

Note our first code block initializes our necessary libraries while Our second code block creates our plot.

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.

plt.figure( figsize = ( 15 , 5 ) )

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.

plt.plot( [ 12 , 20 , 35 ] , [ 12 , 20 , 24 ] , ’g’ ) β€” β€” β€” β€” β€” β€” β€” try both β€” β€” β€” β€” β€” β€” β€” -plt.plot( [ 12 , 20 , 35 ] , [ 12 , 20 , 24 ] , ’r )
plt.plot( [ 12 , 20 , 35 ] , [ 12 , 20 , 24 ] , ’yo’ ) β€” β€” β€” β€” β€” β€” β€”try both β€” β€” β€” β€” β€” β€” β€” -plt.plot( [ 12 , 20 , 35 ] , [ 12 , 20 , 24 ] , ’go’ )

It is also possible to plot multiple sets of data within the same plot by passing multiple arguments within the plot( ) method

x = np.arange( 1 , 5 )
y = x**3

plt.plot( [ 12 , 20 , 35 ] , [ 12 , 20 , 24 ] , ’go’ , y, x , ’r^’ )

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.

plt.figure( figsize = ( 15 , 5 ) )
plt.subplot( 1 , 2 , 1 ) β€” β€” β€” the 1 , 2 , 1 indicates 1 row 2 column position 1
plt.plot( [ 1 , 2 , 3 , 4 ] , [ 1 , 4 , 9 , 16 ] ,’go’ )
plt.title( β€˜1st Subplot’ )
plt.subplot( 1 , 2 , 2 ) β€” β€” β€” the 1 , 2 , 1 indicates 1 row 2 column position 1
plt.plot( x , y , ’r^’ )
plt.title( β€˜2nd Subplot’ )
plt.suptitle( β€˜My Sub-plots’ )

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).

x = np.arange( 1 , 5 )
y = x**3
fig, ax = plt.subplots( nrows = 2 , ncols = 2 , figsize = ( 6 , 6 ) )
ax[ 0 , 1 ].plot( [ 1 , 2 , 3 , 4 ] , [ 1 , 4 , 9 , 16 ] ,’go’ )
ax[ 1 , 0 ].plot( x , y , ’r^’ )
ax[ 0 , 1 ].set_title( β€˜Squqres’ )
ax[ 1 , 0 ].set_title( β€˜Cubes’ )
plt.show( )

In Part 2 we will dive into plotting different types of plots using pyplot.
Bar graphs, Pie Charts, Histograms and so on.

--

--