使用matplotlib指定X轴范围?

时间:2015-11-13 16:46:41

标签: python matplotlib

我正在尝试使用matplotlib绘制直方图。这是我的代码:

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年被限制在一个地方。我需要拉伸该区域并正确显示它。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

使用.axis()

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

采用以下格式:

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