我正在使用matplotlib绘制条形图。 我需要144个竖条。
如果我使用多达140个小节,一切都按预期工作,但是一旦达到141小时,我会在最后一个小节的右边看到一个奇怪的空白:
IDs = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '140', '141', '142', '143', '144']
values = [875, 1439, 1613, 1112, 1590, 1413, 1146, 1325, 1888, 1500, 1409, 827, 1431, 1024, 1271, 1203, 1503, 1423, 1571, 1216, 1279, 1158, 1095, 1355, 1331, 1292, 1002, 840, 806, 1141, 1032, 1046, 1000, 941, 818, 728, 1146, 1093, 1284, 1074, 1127, 1034, 1207, 1051, 1379, 692, 1205, 1103, 1528, 1105, 1116, 961, 1150, 840, 513, 8, 1270, 1427, 1051, 820, 1439, 852, 0, 1216, 987, 503, 565, 655, 1198, 503, 860, 848, 670, 998, 697, 865, 961, 891, 1095, 1120, 1001, 590, 630, 1133, 862, 787, 959, 1026, 459, 564, 437, 807, 290, 569, 482, 0, 4695, 4726, 4858, 4514, 5039, 5865, 5336, 3873, 5666, 3644, 3789, 4269, 4629, 5308, 3681, 4212, 5003, 5141, 5835, 5331, 5284, 5526, 5185, 4973, 4899, 4866, 5167, 5054, 4413, 5668, 4533, 5122, 127, 4210, 4582, 4734, 4681, 5319, 3519, 5414, 3902, 4139, 5108, 5040, 4450, 4085, 4489, 5632]
width = 0.75
N = len(IDs)
ind = numpy.arange(N)
plt.bar(ind, values,width)
plt.xticks(ind+width/2., IDs)
fig = plt.gcf()
fig.set_size_inches(len(IDs)/4,10)
ax = fig.add_subplot(1,1,1)
ax.tick_params(axis='x', labelsize=6)
plt.tight_layout()
plt.savefig(filename, dpi=200, facecolor='w', edgecolor='w',bbox_inches='tight', pad_inches=0.1)
plt.close()
只有140个ID /值对的结果: 结果包含141个(或更多)ID /值对:
任何人都可以解释这种行为,或者更好的是,告诉我如何摆脱右侧的这个空白?
答案 0 :(得分:6)
您可以手动轻松设置限制:
fig = plt.figure()
ax = fig.add_subplot(111)
#.... do the plotting here
ax.set_xlim([0,150]) # or any other number
plt.show() # or savefig