2016-11-13 150 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