2015-11-13 153 views
0

我想畫一個使用matplotlib的直方圖。這是我的代碼:用matplotlib指定X軸範圍?

import matplotlib.pyplot as plt 
from pylab import * 


class Histogram(object): 
    @staticmethod 
    def plot_histogram(dictionary, labelx, labely, show, save, filename): # x and y are list of values 
     x = [int(year) for year,freq in dictionary.iteritems()] 
     y = [int(freq) for year,freq in dictionary.iteritems()] 
     print x,y 
     plt.bar(x,y,align='center') # A bar chart 
     plt.xlabel(labelx) 
     plt.ylabel(labely) 
     for i in range(len(y)): 
      plt.hlines(y[i],0,x[i]) # Here you are drawing the horizontal lines 
     if show: 
      plt.show() 
     if save: 
      pylab.savefig(filename) 


if __name__=="__main__": 
    Histogram.plot_histogram({2015:1, 2014:1,2008:1, 2011:1, 2010:2, 2012:1},"x","y",True, False, "") 

輸出是:

enter image description here

6年裏,我很感興趣,在一個地方受到限制。我需要拉伸該區域並正確顯示。我怎樣才能做到這一點?

回答

2

使用.axis()

plt.axis([2010, 2016, 2, 0]) 

它採用以下格式:

axis([min_x, max_x, min_y, max_y])