2017-10-09 2 views
2

Wie kann ich diese Balken trennen, ich möchte einen Abstand von 1 Breite zwischen den 4 (bezogen auf die 4 verschiedenen Typen) und bitte finden Sie das Bild im Zusammenhang mit der Ausgabe, die ich von diesem Code bekomme.Wie trennt man das Balkendiagramm?

import numpy as np 
import matplotlib.pyplot as plt 

N = 10 
Google = (10, 15, 32, 29, 13, 35, 2, 20, 27, 29) 

ind = np.arange(N) # the x locations for the groups 
width = 0.35  # the width of the bars 

fig, ax = plt.subplots() 
rects1 = ax.bar(ind, Google, width, color='b') 

Voicebase = (2, 16, 19, 30, 22, 30, 33, 4, 14, 18) 

rects2 = ax.bar(ind + width, Voicebase, width, color='g') 

Watson = (7, 17, 14, 19, 28, 4, 4, 34, 9, 17) 
rects3 = ax.bar(ind + width*2, Watson, width, color='y') 

Remeeting = (12, 21, 19, 35, 24, 6, 22, 31, 19, 14) 
rects4 = ax.bar(ind + width*3, Remeeting, width, color='r') 

# add some text for labels, title and axes ticks 
ax.set_ylabel('Char edit distance') 
ax.set_xticks(ind + width/2) 
ax.set_xticklabels(('A1', 'A2', 'A3', 'A4', 'A5','B1', 'B2', 'B3', 'B4', 'C1')) 

ax.legend((rects1[0], rects2[0], rects3[0], rects4[0]), ('Google', 'Voicebase','Watson', 'Remeeting')) 

def autolabel(rects): 
    """ 
    Attach a text label above each bar displaying its height 
    """ 
    for rect in rects: 
     height = rect.get_height() 
     ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
       '%d' % int(height), 
       ha='center', va='bottom') 

autolabel(rects1) 
autolabel(rects2) 
autolabel(rects3) 
autolabel(rects4) 
plt.show() 

enter image description here

Antwort

1

Eine Möglichkeit, dies zu tun ist, um den Abstand Ihrer ind so zu erhöhen, dass jede vierte Zahl erzeugt wird. Dies wird Platz für alle vier deiner Bars lassen.

import numpy as np 
import matplotlib.pyplot as plt 

Google = [10, 15, 32, 29, 13, 35, 2, 20, 27, 29] 
Voicebase = [2, 16, 19, 30, 22, 30, 33, 4, 14, 18] 
Watson = [7, 17, 14, 19, 28, 4, 4, 34, 9, 17] 
Remeeting = [12, 21, 19, 35, 24, 6, 22, 31, 19, 14] 

ind = np.arange(1,40,4) # the x locations for the groups 
width = 0.5  # the width of the bars 

fig, ax = plt.subplots() 

rects1 = ax.bar(ind, Google, width, color='b') 
rects2 = ax.bar(ind + width, Voicebase, width, color='g') 
rects3 = ax.bar(ind + width*2, Watson, width, color='y') 
rects4 = ax.bar(ind + width*3, Remeeting, width, color='r') 

# add some text for labels, title and axes ticks 
ax.set_ylabel('Char edit distance') 
ax.set_xticks(ind + width/2) 
ax.set_xticklabels(('A1', 'A2', 'A3', 'A4', 'A5','B1', 'B2', 'B3', 'B4', 'C1')) 

ax.legend((rects1[0], rects2[0], rects3[0], rects4[0]), ('Google', 'Voicebase','Watson', 'Remeeting')) 

def autolabel(rects): 
    """ 
    Attach a text label above each bar displaying its height 
    """ 
    for rect in rects: 
     height = rect.get_height() 
     ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
       '%d' % int(height), 
       ha='center', va='bottom') 

autolabel(rects1) 
autolabel(rects2) 
autolabel(rects3) 
autolabel(rects4) 
plt.show() 

enter image description here

-1

Ich möchte einen anderen Weg fügen, dass ein Problem zu beheben (gelöst beim Experimentieren) wird durch den Wert der Breite beispielsweise Absenken es den Wert 1 für die Abschnitte 4 übernimmt plus dem Leer ein. Wenn wir zum Beispiel den Wert width = 0.2 setzen, haben wir auch ein nettes Balkendiagramm.

Verwandte Themen