A tutorial on how to use MatPlotLib in Python

Python is a programming language, and MatPlotLib is a tool that can be downloaded and used within Python. MatPlotLib can be used to create graphs, interactive data visualizations(including 3D visualizations), and more all within Python. It is extremely useful because the user can fully customize practically every aspect of the data visualization with code. MatPlotLib could be used in Digital Humanities to provide the tools to make data visualizations specific for a certain topic, and it allows much more flexibility than many other data visualization sites. The combination of Python and MatPlotLib can allow for an efficient way to parse data from a spreadsheet and return a customized data visualization, which could be useful in contexts of having a spreadsheet of data that is related to a humanities field such as history or environmental studies, and being able to plot that data.

To start, you must download MatPlotLib, and begin your file with the line import matplotlib.pyplot as plt in order to import the library to be able to access all of the tools it has available.

Then, I created a function to go through my spreadsheet of data and add each value to a list. I started by initializing each list by creating three blank lists for the dates, snow, and precipitation. I also initialized the date as 0 because my spreadsheet starts at day 1 of the year.

I then used this for loop:

This will open the file to read it, and the loop will go through each line in the file, but it will skip over the line with the labels because we only want the numerical data in our visualization. I then striped the \n out of each line with the .strip function. And then I created a variable that splits on every comma, meaning we now have each of our values stored in a certain index of the parts variable.

I then added 1 to the date for each line because my file has one line for every day of the 2023. I also created new variables for the snow amount and precipitation amount that will find the values in the parts list. This will take the value of just the snow or precipitation and store it, and the append function adds these values the our blank lists. We are now building up lists by going through our spreadsheet.

This is the function I created that uses MatPlotLib. I set x as the dates and 2 y values as the snow and precipitation because I want the date on the x axis. I then use the plt.plot function for both of our y values, and then I customize the graph with a title, and x and y labels. I also created a legend so it would be clear which line was representing the snow and which line was representing the precipitation. Finally, I finished with plt.show() so it actually shows the visualization.

Lastly, I created my main function that gives the filename we are going to access, and just gets these two functions to work together.

The end result of our graph is going to look like this:

For more information visit MatPlotLib or this website for helpful documentation about how to use MatPlotLib.

3 thoughts on “A tutorial on how to use MatPlotLib in Python

  1. This is really cool. I have used R to do similar things and I always knew there was a way to do similar things in python, but this tutorial makes it really easy to understand exactly how to do it. It’s definitely something I want to try out at some point and I will hopefully make use of this tutorial!

  2. Hi Molly, thank you so much for your insight into this tool in Python. More options for our data visualizations are great, and the examples shown on the MatPlotLib link are very insightful on our possible uses. Your screenshots were beyond helpful going step by step as well, you made a good tutorial!

  3. Hi Molly! This is a really informative tutorial! As someone that does not have much knowledge about various coding languages, your tutorial made it very easy to understand. The screenshots also are very helpful since oftentimes in class when we are following instructions for a project, I tend to get lost since I do not know exactly what to type/press. Overall, good job!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

css.php