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.
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
Step 7: How to add labels, color, plot multiple lines, and change line width in matplotlib
Step 8: How to add legend, remove legend, and show legend in matplotlib
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.
Step 10: How to add/set title and change title fontsize, location, and fontweight in matplotlib graph
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 12: How to change the background color in matplotlib
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.
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 20: The goal is for you to learn how to graph with matplotlib using python and be able to create any graph you want.
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…
…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.