2

Hello I have this code :

import matplotlib.pyplot as plt
import math
import numpy as np

a = np.array([2.79e-06,2.47e-06,0.000122,3.9e-05,0.000527,9.41e-06,3.11e-05,4.63e-05,0.000749,0.000499,0.000174,0.000984,0.000912,5.86e-05,0.000344,0.000754,0.000267,2.53e-06,8.73e-05,0.000791,1.69e-06,7.56e-05,0.001,3.1e-06,0.000305,2.4e-06,6.37e-06,3.25e-07,6.67e-05,0.000167,0.000954,2.36e-07,3.8e-06,0.000337,8.1e-06,1.68e-05,0.000332,3.64e-06,2e-05,5.97e-06,0.000808,2.34e-06,0.000121,0.000972,2.59e-05,0.000761,8.76e-05,0.000253,0.000819,7.88e-06,5.04e-05,1.75e-05,5.83e-05,0.000271,3.18e-06,3.29e-05,0.000979,0.000925,2.55e-05,0.000347,0.000269,9.51e-06,5.54e-06,7.18e-05,1.44e-05,8.42e-09,1.86e-05,0.000377,1.68e-05,0.000991,4.69e-06,9.87e-05,4.45e-05,4.05e-06,6.76e-05,5.66e-06,6.51e-06,3.76e-06,6.44e-05,2.91e-09,0.000565,9.18e-06,0.0003,0.0002,9.43e-05,8.57e-06,6.32e-05,4e-06,5.18e-06,0.000181,0.000999,1.67e-05,0.000941,6.49e-05,0.000141,4.07e-06,2.68e-06,0.000407,1.47e-05,5.2e-06,6.53e-06,0.000462,1.38e-05,0.000794,3.32e-07,2.19e-06,0.000432,0.000156])


plt.clf()
plt.hist(a)
plt.xscale('log')
plt.title('Histogram of a')
plt.savefig('a.png')

But I don't understand when I see the histogram I get this picture :

enter image description here

But it is not good because the length of the different bins are not equals !

Could you help me please ?

Thank you so much !

Edit :

This is the new code for Florian :

import matplotlib.pyplot as plt
import math
import numpy as np

a = np.array([2.79e-06,2.47e-06,0.000122,3.9e-05,0.000527,9.41e-06,3.11e-05,4.63e-05,0.000749,0.000499,0.000174,0.000984,0.000912,5.86e-05,0.000344,0.000754,0.000267,2.53e-06,8.73e-05,0.000791,1.69e-06,7.56e-05,0.001,3.1e-06,0.000305,2.4e-06,6.37e-06,3.25e-07,6.67e-05,0.000167,0.000954,2.36e-07,3.8e-06,0.000337,8.1e-06,1.68e-05,0.000332,3.64e-06,2e-05,5.97e-06,0.000808,2.34e-06,0.000121,0.000972,2.59e-05,0.000761,8.76e-05,0.000253,0.000819,7.88e-06,5.04e-05,1.75e-05,5.83e-05,0.000271,3.18e-06,3.29e-05,0.000979,0.000925,2.55e-05,0.000347,0.000269,9.51e-06,5.54e-06,7.18e-05,1.44e-05,8.42e-09,1.86e-05,0.000377,1.68e-05,0.000991,4.69e-06,9.87e-05,4.45e-05,4.05e-06,6.76e-05,5.66e-06,6.51e-06,3.76e-06,6.44e-05,2.91e-09,0.000565,9.18e-06,0.0003,0.0002,9.43e-05,8.57e-06,6.32e-05,4e-06,5.18e-06,0.000181,0.000999,1.67e-05,0.000941,6.49e-05,0.000141,4.07e-06,2.68e-06,0.000407,1.47e-05,5.2e-06,6.53e-06,0.000462,1.38e-05,0.000794,3.32e-07,2.19e-06,0.000432,0.000156])


plt.clf()
plt.hist(a, bins=np.logspace(a.min(), a.max(), 50))
plt.xscale('log')
plt.title('Histogram of a')
plt.savefig('a.png')

And this is what I get :

Histogram of a

6
  • So, you just want to change the x scale labels to a scientific notation? Commented Aug 29, 2017 at 15:47
  • I want to have the same size for the bins ! Commented Aug 29, 2017 at 15:51
  • You can accomplish this by removing the log scale. The only difference will be the x scale format since the y scale remains linear Commented Aug 29, 2017 at 16:01
  • I know that but in this case I lose the log scale and if you look at my data I need log scale because I have a lot of little quantities close to 0... Commented Aug 29, 2017 at 16:04
  • I think I figured it out. Try np.logspace(np.log10(a.min()), np.log10(a.max()) , 50) Commented Jul 4, 2021 at 19:28

1 Answer 1

1

You are using a logarithmic scale, that's why some bins seem larger. But they are actually all the same size if you remove plt.xscale('log') from your code.

If you need to keep the logarithm scale, then, create your own customised scale for the bins. Look at the documentation here https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.hist.html

Florian

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

8 Comments

Sorry but I don't understand why it is like this ! Is there a way to have bins with the same size ?
Your bins are the same size. What you see is just due to your logarithm scale. Remove plt.xscale('log') and you will see that it is the same size.
Yeah obviously but I want to have the results in the log scale with the same size for the bins...
If you want to keep your logarithm scale, then use another scale for the bins, like : plt.hist(a, bins=np.logspace(0.1, 1.0, 50))
It is always the same I get a line... :/
|

Your Answer

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