簡體   English   中英

用matplotlib指定X軸范圍?

[英]Specify the X-axis range with 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, "")

輸出為:

在此處輸入圖片說明

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

使用.axis()

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

它采用以下格式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM