데이터 목록에 대한 라벨과 값을 같이 표시해준다.
import numpy as np
import matplotlib.pyplot as plt
def draw_bar_chart(values, labels, subject, draw_mean=True, fig_size=(10, 6)):
x = np.arange(len(values))
y = values
figure = plt.figure(figsize=fig_size)
barlist = plt.bar(x, y, 0.7, align='center')
mean = np.mean(y)
if draw_mean == True:
plt.axhline(mean, color='coral', linestyle='dashed', linewidth=1)
handles = []
for index in np.arange(len(barlist)):
if barlist[index].get_height() > mean:
color = '#ec8482' # red like
else:
color = '#a4d8ee' # blue like
barlist[index].set_color(color)
handles.append(plt.Rectangle((0,0), 1, 1, color=color))
plt.title(subject)
plt.legend(handles, labels)
plt.xticks(x, labels)
for bar in barlist:
height = bar.get_height()
val = int(height)
plt.text(bar.get_x() + bar.get_width() / 2.,
height + height*0.01,
val, ha='center', va='bottom')
plt.show()
draw_bar_chart([1,2,4,6,7,7], ['v1','v2','v3','v4','v5','v6'], 'sample')
댓글 없음:
댓글 쓰기