fbpx

How to Graph With MatplotLib Using Python With Pictures and Video

How to Graph With MatplotLib Using Python With Pictures and Video

In this tutorial, I will show you step by step how to create a graph with matplotlib using python.

This is the graph we will try to re-create today.

This graph is from 538 blog

First of all, you might be wondering, why are we recreating an existing graph? Well, one of the best ways to learn something new is to imitate something that has already been created and learn how it was done. It is a good way to practice your skills. If you can re-create the thing that has already been created, then you will have learned a new skill and can now create your own original work using the skills you have learned. Enough philosophy about learning by re-creation. Let’s dive in.

For our recreation process, we will not use data to the left of “prohibition” period. Our re-created graph will be the data to the right side of the “prohibition” period. Our graph will look a little bit different, but overall, you will learn how to recreate the graph above.

Step 1: How to import the right python libraries for matplotlib graphing

Code:
How to import the right python libraries for matplotlib

Step 2: Get image of the graph we are trying to recreate

Code:
Result:

Step 3: How to load data for matplotlib graph in python, google and jupyter notebook

Code:
How to load data for matplotlib
Result: Make sure our data look how we want it to look.

Step 4: How to set and change figure size and background information for the graph in matplotlib.

Code:
Matplotlib how to add subplots and figure size
Result:

Step 5: How to set the axis range in matplolib

Code:
How to set and change axis range
Result:

Step 6: Define the location of the data points to be plotted.

Code:

Step 7: How to add labels, color, plot multiple lines, and change line width in matplotlib

Code:
How to plot a line in matplotlib
Result:
Multiple lines and colors in matplotlib

Step 8: How to add legend, remove legend, and show legend in matplotlib

Code:
how to add legend in matplolib

Make the labels show up. As you can  see, we defined the labels for the graph above, but in order for them to show up, we have to add one more line of code.

Result:

But, we actually don’t want the legend where it is right now. So you can remove it and as we go on, we will add the legend back and place it next to the lines individually just like it is presented in 538 graph.

Step 9: How to set, remove, and change the color for xticks & yticks on matplotlib graph.

Code:
How to set and change horizontal and vertical tick marks

Before:

After:

Step 10: How to add/set title and change title fontsize, location, and fontweight in matplotlib graph

Code:
Set custom graph title, fontsize, and font weight.
Result:

Make it bold, use x and y location to move it to the left side of the graph and align it with the y axis. Make sure we leave enough space for subtitle.

Step 11: How to add/set sub-title text and change fontsize, fontweight, and location in matplotlib graph

Code:
How to increase and change fontsize in matplotlib
Result:

Move the subtitle directly under the title. Also, change the font-size to match.

Step 12: How to change the background color in matplotlib

Code:

Change the background color / axis color (face color) of the graph. You need to set the facecolor to white, but I set it to red just to show you what facecolor is.

change graph background color
Result:

When the facecolor is set to red, this is what it looks like. But for recreating this graph, set it to white. Not red. The red below is just for demontration purposes so that you will see what facecolor is. When you set it to white, you really can’t see the difference which is why I set it to red for clarification purposes.

Step 13: How to add the sub-text that shows what * means in matplotlib graph.

Code:
How to change text size in matplotlb
Result:

Step 14: How to set/add grid in matplotlib graph

Code:
How to add gridlines in matplotlib
Result:

Before adding gridlines….

…After adding gridlines.

Step 15: How to remove graph spines and borders in matplotlib.

Code:
How to set borders in matplotlib
Result:

Before removing spines….

…After removing spines.

Step 16: How to add axis tick labels in matplotlib graph

Code:
How to set labels in matplotlib graph
Result:

Step 17: How to Add the text that says "Prohibition"

Code:
How to add italic or bold to text in matplolib graph.
Result:

Step 18: How to annotate the legends/labels and move text next to the graph lines in matplotlib.

Code:
How to annotate graph in matplotlib
Result:

Step 19: How to show the plot in matplotlib

Code:
How to display plot in matplotlib
Result:
To deepen your knowledge of matplotlib graphing with python, I have provided you with additional resources below.

This basic graph shows you the name of everything on a graph. You can download the code for this particular graph here and play around with it and see for yourself how to create this graph. You can comment out different lines of code to learn which lines of codes creates different things on the graph.

If you want to create this graph…

Anatomy and basics of a graph from matplotlib

…You need these lines of code to create the graph above.

You can download the code above and play around with it to see for yourself how to create the graph you just saw. You can comment out different lines of code to learn which lines of codes creates different things on the graph.

2 Comments

Leave a Comment

Scroll to Top