I am having some trouble trying to figure out how to title matplotlib subplots as part of a for loop, and am not sure how to use a referenced value as part of the title. I am using python.
I have the following dataframe:
Parameter P1 P2 P3 P1 P2 P3 P1 P2 P3
Color Blue Red Green Blue Red Green Blue Red Green
Size
--------------------------------------------------------------------------------
Small XX XX XX XX XX XX XX XX XX
Medium XX XX XX XX XX XX XX XX XX
Big XX XX XX XX XX XX XX XX XX
Then running the following code:
fig, axs = plt.subplots(nrows=1,
ncols=5,
figsize=(10, 3))
groups = df.groupby(level=1, axis=1)
for ax, group in zip(axs.flatten(), groups):
group[1].plot(kind='bar',
ax=ax,
color=['b', 'g', 'r'],
sharey=True,
legend=False,
rot=True)
I get three bar graphs, as I want. The only problem is that they are not titled. These 3 bar graphs are supposed to correspond to P1, P2, and P3. How can I title these subplots as part of the for loop so that they are titled "Parameter 1", "Parameter 2", and "Parameter 3"? I am very confused about how to enter a subplot title function in the for loop that references I prior order string.
The imports I used are:
from io import StringIO
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt