1

Given a simple list of negative values: l = [0, -1, -1, -1, -10, -100]

What's the fastest way to visualize its histogram with the standard method plt.hist(l) ?

I want to be able to see all entries in the list and their relative frequencies.

Doing:

from matplotlib import pyplot as plt 
l = [0, -1, -1, -1, -10, -100]
plt.hist(l)
plt.show()

Results in:

enter image description here

What would be the correct way to set the number of the bins in the current case?

Any help highly apprecited

1
  • Using the bins argument? Commented Nov 30, 2018 at 19:24

1 Answer 1

1

Suppose all the entries in your list l are integers. You can use bins to control the number or size of bins in your histogram.

from matplotlib import pyplot as plt
l = [0, -1, -1, -1, -10, -100]
plt.hist(l, bins=max(l)-min(l)+1)
ax = plt.subplot()
ax.set_xticks(l)
plt.show()

enter image description here

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

2 Comments

Thank you. Could you specify how to have all the entries values plotted on the x-axis? Instead of -100, -80, -60, -40, -20, 0 only
@Employee You can use set_xticks() to specify the values on x-axis tick. I have changed the plot.

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.