Bar 차트 그리기
📑 목차 (Contents)
plt.bar
-
plt.hist의 경우는 값 덩어리를 넘겨주면 알아서 bins으로 구분하여 그려주는 반면 -
plt.bar의 경우는 x, y가 각각 존재하는 값을 넘겨준다고 생각하면 됩니다. -
역시나 말로 설명하면 이상하군요.
-
아무튼 아래처럼 그려주고 변경할 수 있습니다.
## counter to histogram
xs = [i for i in range(0, 10)]
ys = [np.random.normal(0, 1) for i in range(0, 10)]
plt.figure(figsize=(12, 6))
bar_containers = plt.bar(x=xs,
height=ys,
width=0.7, ## 비율지표
orientation='vertical',
align='center', ## xticks에서 중간에 있을 것이냐 아니면 끝(edge)에 있을 것인가를 결정함.
color='black',
edgecolor='red',
linewidth=7,
alpha=0.8
)
plt.xticks(xs, ["label_{}".format(i) for i in range(0, 10)],
fontproperties=BMHANNA, fontsize=15
)
## 가끔 그림에서 minus sign('-')을 인식하지 못하는 경우가 있는데, 그때 아래를 사용해서 세팅을 바꿔줘야 함
plt.rcParams['axes.unicode_minus'] = False
## 그냥 font만 넘겨줘도 되는구나...
plt.yticks(fontproperties=BMDOHYEON)
## bar별로 변경하고 싶을때
## 다음은 6번째 있는 bar의 색깔을 변경한 것
bar_containers[5].set_facecolor('blue')## 내부
bar_containers[5].set_edgecolor('black')## 밖에 선
plt.savefig('../../assets/images/markdown_img/180802_plt_bar.svg')
plt.show()
GitHub에서 이 글의 마크다운 원본 보기 & 수정 제안src/content/posts/2018-08-02-plt_bar