2014-06-10 28 views
1

這裏是我的代碼片段繪製bincounts:如何強制PyPlot顯示空箱在柱狀圖

pos = np.arange(len(ranger)) 
    print pos 
    width = 1.0  # gives histogram aspect to the bar diagram 

    ax = plt.axes() 
    ax.set_xticks(pos + (width/2)) 
    ax.set_xticklabels(ranger) 

    plt.bar(pos, freq, width, color='b',) 

    plt.savefig("binplotdisease/"+key+"_binplot.png") 
    plt.close() 

遊俠是0.17。然而,當我繪製一些數據時,我發現如果例如只有一個數爲9而沒有其他數(其餘爲0),那麼只有9個出現在圖中。爲了我的工作目的,我希望能夠看到從0到17的整個空間,以便我能更好地查看趨勢。如何更改繪圖設置,以便看到空箱?

+0

你能正確地重新表述您的問題。你的空箱子是什麼意思?首先當空箱子時,你怎麼能顯示一個箱子? – ThePredator

回答

3

使用plt.xlim明確,以防止matplotlib從自動調整x值的顯示範圍:

import numpy as np 
import matplotlib.pyplot as plt 

pos = np.arange(4) 
freq = [0, 1, 3, 0] 
width = 1.0  

plt.bar(pos, freq, width, color='b',) 
plt.xlim(pos.min(), pos.max()+width) 
plt.show() 

enter image description here

+0

非常感謝!這似乎工作! – indiaash524

+0

這是一個已知的bug已經修復(找不到快速問題....問題是0高度補丁沒有考慮到什麼計算數據限制)。該修復程序在master分支上,並將在1.4 – tacaswell