2017-08-29 82 views
0
數刻度

你好,我有這樣的代碼:直方圖與Python

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') 

但是,當我看到直方圖我得到這幅畫我不明白:

enter image description here

但它是不好,因爲不同箱子的長度不相等!

請問您能幫我嗎?

非常感謝!

編輯:

這是弗洛裏安新代碼:

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') 

而這就是我得到:

Histogram of a

+0

所以,你只是想在x刻度標籤更改爲科學記數法的文件? – Eduardo

+0

我想要箱子的大小相同! –

+0

您可以通過刪除日誌比例來完成此操作。唯一的區別將是x刻度格式,因爲y刻度保持線性 – Eduardo

回答

1

您使用的是對數刻度,這就是爲什麼一些箱子看起來更大。但是,如果您從代碼中刪除plt.xscale('log'),它們實際上都是相同的大小。

如果您需要保持對數刻度,那麼請創建您自己的自定義刻度。 看這裏 https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.hist.html

弗洛裏安

+0

對不起,但我不明白爲什麼它是這樣的!有沒有辦法讓箱子的大小相同? –

+1

您的垃圾箱尺寸相同。你看到的僅僅是由於你的對數尺度。刪除'plt.xscale('log')',你會看到它是相同的大小。 –

+0

很明顯,但我想在日誌規模的結果與相同的大小的箱子... –