I have a function I've created to generate one or multiple plots depending on user inputs. The user can pass in a data frame and a list of the column names (categorySubset
) to graph. The user can input a list with one column name or a list with several column names.
def generateMultidistribution(df, primaryDistribution, categoricalColumnName, categorySubset):
# Initialize subplots
fig,ax = plt.subplots(nrows=len(categorySubset), ncols=1)
fig.suptitle('Line Item Distributions')
# Specify subplot configuration
fig.set_figheight(len(categorySubset) * 4.95)
fig.set_figwidth(9.2)
fig.tight_layout(pad = 5)
graphIndex = 0
for category in range(0, len(categorySubset)):
... # graphing code removed for brevity
If I only pass in one argument for categorySubset
, the function fails since subplots needs the number or rows (or columns) to be more than 1. Is there a good fix to allow this to work regardless of the number of columns (a.k.a. len(categorySubset)
)? Why does plt.subplots()
require more than 1 row or column?