2016-11-13 155 views
3

如何使用matplotlib barcharts增加每个条形图之间的空间,因为它们会将它们自行填充到中心。 enter image description here(这是它目前看起来)matplotlib条形图:空间条形图

import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 
def ww(self):#wrongwords text file 

    with open("wrongWords.txt") as file: 
     array1 = [] 
     array2 = [] 
     for element in file: 
      array1.append(element) 

     x=array1[0] 
    s = x.replace(')(', '),(') #removes the quote marks from csv file 
    print(s) 
    my_list = ast.literal_eval(s) 
    print(my_list) 
    my_dict = {} 

    for item in my_list: 
     my_dict[item[2]] = my_dict.get(item[2], 0) + 1 

    plt.bar(range(len(my_dict)), my_dict.values(), align='center') 
    plt.xticks(range(len(my_dict)), my_dict.keys()) 

    plt.show() 

回答

6

尝试更换

plt.bar(range(len(my_dict)), my_dict.values(), align='center') 

plt.figure(figsize=(20, 3)) # width:20, height:3 
plt.bar(range(len(my_dict)), my_dict.values(), align='edge', width=0.3) 
+0

我不断收到此错误 'ValueError异常:无效对齐:左' – Smith

+0

很抱歉的错误。对齐选项有两个选择:{'edge','center'}。 – swatchai

+0

已经固定了酒吧的间距,欢呼声。然而,x轴上的单词仍然挤在一起。我将如何使它垂直? – Smith