我正在尝试使用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年被限制在一个地方。我需要拉伸该区域并正确显示它。我怎么能这样做?
答案 0 :(得分:2)