2012-11-09 28 views
10

Tôi đang cố gắng vẽ 4 giá trị trung bình với các thanh lỗi dương và giá trị lớn nhất trong cốt truyện.Làm thế nào để vẽ âm thanh chỉ là thanh lỗi tích cực với pyplot.bar?

means = [26.82,26.4,61.17,61.55]   # Mean Data 
stds = [4.59,4.39,4.37,4.38]   # Standard deviation Data 
peakval = ['26.82','26.4','61.17','61.55'] # String array of means 

ind = np.arange(len(means)) 
width = 0.35 
colours = ['red','blue','green','yellow'] 

pyplot.figure() 
pyplot.title('Average Age') 
for i in range(len(means)): 
    pyplot.bar(ind[i],means[i],width,color=colours[i],align='center',yerr=stds[i],ecolor='k') 
pyplot.ylabel('Age (years)') 
pyplot.xticks(ind,('Young Male','Young Female','Elderly Male','Elderly Female')) 

def autolabel(bars,peakval): 
    for ii,bar in enumerate(bars): 
     height = bars[ii] 
     pyplot.text(ind[ii], height-5, '%s'% (peakval[ii]), ha='center', va='bottom') 
autolabel(means,peakval)  

Tuy nhiên, tôi không thể tìm hiểu cách chỉ vẽ các thanh lỗi dương. Vì vậy, tôi kết thúc với một đồ thị như thế này:

Average Age

Bất kỳ lời đề nghị sẽ được đánh giá rất nhiều.

Trả lời

11

Nếu tôi hiểu đúng, bạn có thể làm điều này:

import numpy as np 
from matplotlib import pyplot 

means = [26.82,26.4,61.17,61.55]   # Mean Data 
stds = [(0,0,0,0), [4.59,4.39,4.37,4.38]] # Standard deviation Data 
peakval = ['26.82','26.4','61.17','61.55'] # String array of means 

ind = np.arange(len(means)) 
width = 0.35 
colours = ['red','blue','green','yellow'] 

pyplot.figure() 
pyplot.title('Average Age') 
pyplot.bar(ind, means, width, color=colours, align='center', yerr=stds, ecolor='k') 
pyplot.ylabel('Age (years)') 
pyplot.xticks(ind,('Young Male','Young Female','Elderly Male','Elderly Female')) 

def autolabel(bars,peakval): 
    for ii,bar in enumerate(bars): 
     height = bars[ii] 
     pyplot.text(ind[ii], height-5, '%s'% (peakval[ii]), ha='center', va='bottom') 
autolabel(means,peakval) 
pyplot.show() 

Kết quả:

enter image description here

Nó hoạt động vì bạn có thể vượt qua như yerr một danh sách 2xN, đại diện cho "offsets tích cực và tiêu cực ", xem documentation.

+0

Tuyệt vời, cảm ơn bạn. –

Các vấn đề liên quan