1

I have a nested list, whose data I need to plot on different histograms.

    ...
    ...
    numbers = [[float(line[1]) for line in chr ] for chr in result]
    plt.hist(numbers)
    plt.show()

Doing so, the output is one only figure, with the bars of the different histograms in each bin (for instance the first bin contains the first bar of every histogram) Instead what I want is to have separate histograms. I tried to do a for cycle:

    for w in numbers:
       plt.hist(w)
       plt.show()

but doing so I obtaine only a histogram per time, and I am allowed to see the next one, only when i close the previous one. What shall I do?

1 Answer 1

3

Use subplots. Choose n_rows and n_columns such that you can make len(numbers) subplots. i starts at 1, not 0.

i = 1
for w in numbers:
    plt.subplot(n_rows, n_columns, i)
    i += 1
    plt.hist(w)
plt.show()

By taking plt.show() out of the for loop the figure is drawn on the screen only once and there is no need to close earlier incomplete versions.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you accept the answer? It is the best way to let other people know that the question is answered. (and I get the credits :) )

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.