MatPlotLib Cheatsheet

Gamze Yılan
6 min readJan 21, 2023

Learn MatPlotLib in 6minutes!

Getting Started

MatPlotLib is a library that helps us visualize data. It’s an addition to your Python and NumPy projects and could be a lifesaver when it comes to data science.

If you’re a total newbie to data science and don’t know much about NumPy and/or Python, I can teach you both in a total of 10 minutes. Just click and read my relating articles.

In order to get started, we must first install the library. To do so, simply open your terminal and run:

python -m pip install -U matplotlib //works on Windows, Linux, MacOS

Note: If your development environment differs (ie. Ubuntu, Anaconda), make sure to check how to install via the documentation.

There are multiple graphic types you can create with MatPlotLib. You’ll learn more about them as you read further, but this should give you an overall visual idea on what is what:

Since MatPlotLib is a pyhton library, we’ll need a python file to work with it. And since we’ll need data to work with MatPlotLib, I’ll use NumPy to generate the required data. If you don’t have Python or NumPy on your system make sure to install them first. Then, create a file named example.py and import NumPy and MatPlotLib as:

import numpy as np

import matplotlib.pyplot as plt

Note: We use the as key to assign the library a name so that we can call the functions within the library simply by putting a dot next to the name of it.

To actually see the graphics that we create, we can simply the following line after creating the graphic:

plt.show()

Generating the Graphics

Let’s generate two numpy arrays to feed as data to our graphics first. In real life applications that’ll probably be an excel file and you can import excel file as a Numpy Array, Series or Matrix using the library Pandas.

However, for learning purposes we’re going to create them using Numpy itself for now. So let’s create a graphic that shows the relation between people’s ages and weights. We can declare the data as:

numpyAgeList = np.array([20, 25, 30, 40, 40, 60])

numpyWeightList = np.array([ 50, 55, 50, 70, 65, 60])

We can generate the graphic using the plot function as:

plt.plot(numpyAgelist, numpyWeightList, “g”)

plt.show() // to see the graphic

  • The data array written as the first parameter will get applied onto the x-axis and the array on the second parameter will get applied onto the y-axis.
  • The string “g” stands for “green” and will make the relating line on the graphic in the color green. You can use different colors by simply typing them as a string in the third parameter. (“r” for red, “b” for blue etc.)
  • The example above seems kind of confusing, you have no idea which one is the weight and which one id the age huh? Try adding labels to each axis with xlabel and ylabel as:

plt.xlabel(“Age”)

ply.ylabel(“Weight”)

  • This is a graph for what? Try adding a title to your graph with the title function as:

plt.title(“ Weight — Age Relation Graph”)

  • You can make the line dashed instead of solid as:

plt.plot(numpyAgelist, numpyWeightList, “g - -”)

  • You can even make the matching points marked with asterixes as:

plt.plot(numpyAgelist, numpyWeightList, “g * -”)

  • You can display multiple graphs side by side or below each other as if a grid view using subplots() function:

plt.subplots(1, 2, 1) // parameters standing for number of rows, number of columns and place index of the following graph

plt.plot(array1, array2, “r*-”)

plt.subplots(1, 2, 2) // puts the following graph on second slot on a grid of 1 row and 2 columns

plt.plot( array3, array4, “g- -“)

Customized Graphs

The plot function doesn’t always work according to our needs since it returns a cookie cutter shape and size. Hence, in order to create a more customized graph, we use the Figure instead. Let’s study an example:

myFigure = plt.figure()

figureAxes = myFigure.axes( [ 0.3, 0.3, 0.9, 0.9])

figureAxes.plot(numpyAgeList, numpyWeightList, “g”)

  • Within the .axes() function: the first parameter stands for the starting point ot the y-axis, the second parameter stands for the starting point of x-axis, and the third & fourth parameters stand for the width of both. This often comes in handy when you create two or more graphs that overlap.
  • You can give labels, a title etc. to the graph created via Figure as:

figureAxes.set_xlabel(“Age”)

figureAces.set_ylabel(“Weight”)

  • You can also generate a figure that is actually a gridview containing multiple figures, using .subplots() function similar to how we did with the regular graphs in the previous section:

myFigure = plt.figure() //declaring the figure

axes1 = [ 0.3, 0.3, 0.9, 0.9] //declaring the axes figure that the first cell will contain

axes2 = [ 0.4, 0.4, 0.8, 0.8] //declaring the axes figure that the second cell will contain

myFigure, (axes1, axes2) = plt.subplots(1,2) // drawing the two sub-figures with declared axes, creating the grid view of one row and two columns, and then assigning it to myFigure.

Styling the Graph

Our plain old graph might not be the best option when preparing a presentation for our co-workers. So let’s style and beautify them. First we better create the figure itself:

myFigure = plt.figure()

figureAxes = myFigure.axes( [ 0.3, 0.3, 0.9, 0.9])

figureAxes.plot(numpyAgeList, numpyWeightList, “g”)

  • Let’s say that the same graph also holds a line for people’s ages and number of children they have. Even if we make the lines in different colors, this might get confusing and we might forget which color stands for which data. So for that, we use label as:

figureAxes.plot(numpyAgeList, numpyNumberOfChildrenList, “r”, label = “number of children”)

figureAxes.legend(loc=1) // loc stands for location of the label: 1 for right top, 2 for left top, 3 for left bottom, 4 for right bottom. Counter-clockwise.

  • You can give the figure a width and a height while declaring it with figsize as:

myFigure = plt.figure(figsize=(2,2)) //if left empty, auto-generates by axis sizes

  • You can increase/decrease resolution of the graph with dpi as:

myFigure = plt.figure(dpi=100) // also resizes the graph if necessary

  • You can save your figure to your computer under the same directory with your code, using .savefig() function as:

myFigure.savefig(“myFigureName.png”, dpi=100) // you can define the format, the file name and the resolution here

  • If the pre-defined colors are not enough for you, you can call new colors from hex and even add transparency as:

figureAxes.plot(numpyAgeList, numpyWeightList, color=“#3A95A8”, alpha=0.5)

  • You can change the thickness of your lines as:

figureAxes.plot(numpyAgeList, numpyWeightList, color=“g”, linewidth=8.0)

  • You can make your line solid, dashed or dot-dashed as:

figureAxes.plot(numpyAgeList, numpyWeightList, color=“g”, linestyle=”-”) //solid line

figureAxes.plot(numpyAgeList, numpyWeightList, color=“g”, linestyle=”- -”) //dashed line

figureAxes.plot(numpyAgeList, numpyWeightList, color=“g”, linestyle=”- .”) //dot-dashed line

Graph Types

A scatter plot is a graph type that simply marks the matching points in both axes in a scattered manner. You can create a scatter plot as:

plt.scatter(numpyAgeList, numpyWeightList)

To see a grouped analysis of the data frequency within your array, you can create a histogram as:

plt.hist(numpyAgeList)

To see the proportion and the standard deviation of values within the array, you can also use boxplot as:

ply.boxplot(numpyAgeList)

There are many more graph types, but these three are good enough to get you working with matplotlib. Once you feel confident in what you’ve just learned and would like to know more, do make sure to check out the documentation to see many others!

--

--