

Lastly, add any other customizations to the subplot by calling the axis variables. By calling the index value in the brackets, the axis variable becomes dynamic. Then, assign and plot the filtered DataFrame to an axis variable. Next, filter the DataFrame for the first item in the list.

To start, grab the index value of the list item with ind = df.index(i) Create Individual Axes Variables for each DataFrame Category
PYPLOT SUBPLOT LOOP ANNOTATION CODE
This is great if you change the data set or if you use the code for a different project. Multiplying 3.5 by the number of items in your list, using len(lst) allows the figure size to be dynamic. I find that a fig size with a height of 3.5 subplot works pretty well for each subplot. Please note that the order of the list is the order of how the subplots will appear. One way to accomplish this would be to run this on the category column: df.unique.tolist()

In the loop, I will create a new DataFrame based on each item in the list. In my example, I am going to make a list of unique country names. The next step is to make a list of the categories to filter. New data points would add more rows to the dataset. In the example below, there are two category columns and one numerical column.
PYPLOT SUBPLOT LOOP ANNOTATION HOW TO
Check out my post on Analyzing COVID-19 Data in Python to see how to access them. One of my favorite customizations is to add a data label to the most recent point in the chart using ax.annotate().įor this post, I am using the publically available COVID-19 data sets. It’s a quick way to accomplish the goal of creating subplots, however, the ability to customize an individual subplot is lost.īy looping through the DataFrame and creating individual subplot axis variables along the way, we can implement custom formatting for each or all the subplots. One of which is to use the Pandas df.plot() function and passing in subplots = True argument. There are multiple ways to create a series of subplots from a Pandas DataFrame. In this post, I’ll show you how to use a For Loop to create individual subplots and axes variables from a single Pandas DataFrame. I have found that using a For Loop to create a series of subplots allows for greater flexibility to customize the individual plots compared to using the Pandas plot function.
