0
$\begingroup$

I am trying to plot the data in a loop, I am using the below code to build the plot. but Its running for all the loop, I can see the column name is getting printed for all th but its building the plot for only the last one. is there anything I have add extra to generate the plot for individual iteration?

for (index, colname) in enumerate(df):
    print(colname)
    counts = df[colname].value_counts(dropna=False)
    counts.plot.bar(title=colname, grid=True)
$\endgroup$

1 Answer 1

0
$\begingroup$

I think the thing is that by calling the drawing function, you are constantly redrawing the same figure. Try to create a figure every time:

import matplotlib.pyplot as plt
for (index, colname) in enumerate(df):
    print(colname)
    plt.figure()
    counts = df[colname].value_counts(dropna=False)
    counts.plot.bar(title=colname, grid=True)

You can also divide one drawing into many parts and draw several graphs at a time using matplotlib subplots. matplotlib subplots doc

$\endgroup$
1
  • $\begingroup$ Fantastic, Thanks for the subplot tip. $\endgroup$ Dec 3, 2022 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.