2016-01-20 157 views
0

我想通過循環一系列包含值的數組來創建一系列直方圖。對於每個數組,我的腳本正在生成一個單獨的直方圖。使用默認設置,這將導致直方圖中頻率最高的條接觸圖表頂部(this is what it looks like now)。我希望有一些空間:this is what I want it to look like.python matplotlib直方圖:根據bin中的最大頻率編輯x軸

我的問題是:如何使最大值?y軸依賴於我的垃圾箱中出現的最大頻率的價值我想y軸比我最長的酒吧稍長

我不能設置像這樣的價值這樣做:

plt.axis([100, 350, 0, 5]) #[xmin, xmax, ymin, ymax] 

matplotlib.pyplot.ylim(0,5) 

因爲我繪製了一系列直方圖,並且最大頻率強烈地變化。

我的代碼現在看起來是這樣的:

import matplotlib.pyplot as plt 

for LIST in LISTS: 
    plt.figure() 
    plt.hist(LIST) 
    plt.title('Title') 
    plt.xlabel("x-axis [unit]") 
    plt.ylabel("Frequency") 
    plt.savefig('figures/'LIST.png') 

如何定義Y軸運行從0到1.1 *(1個箱的最大頻率)?

回答

0

如果我理解正確,這是你希望實現的目標嗎?

import matplotlib.pyplot as plt 
import numpy.random as nprnd 
import numpy as np 

LISTS = [] 

#Generate data 
for _ in range(3): 
    LISTS.append(nprnd.randint(100, size=100)) 

#Find the maximum y value of every data set 
maxYs = [i[0].max() for i in map(plt.hist,LISTS)] 
print "maxYs:", maxYs 

#Find the largest y 
maxY = np.max(maxYs) 
print "maxY:",maxY 

for LIST in LISTS: 
    plt.figure() 
    #Set that as the ylim 
    plt.ylim(0,maxY) 
    plt.hist(LIST) 
    plt.title('Title') 
    plt.xlabel("x-axis [unit]") 
    plt.ylabel("Frequency") 
    #Got rid of the safe function 
plt.show() 

產生最大y限制與maxY相同的圖形。也有一些調試輸出:

maxYs: [16.0, 13.0, 13.0] 
maxY: 16.0 

plt.hist()返回與x, y數據集的元組的功能。所以你可以撥打y.max()來獲得每組的最大值。 Source.

+0

不完全,但它確實解決了我的問題。我正在尋找術語maxY。我現在所要做的就是將maxY乘以1.1,以使Y軸比我的最大Y值長10%。謝謝! –