Matplotlib / Seaborn을 사용하여 두 개의 누적 히스토그램을 나란히 그리는 방법
Aug 19 2020
아래 코드를 사용하여 몇 개의 누적 히스토그램을 그리고 있습니다. 두 가지 모두에 동일한 빈 가장자리를 사용하여 잘 정렬됩니다.
같은 차트에 어떻게 표시 할 수 있습니까? 즉, 각 빈당 녹색 / 빨간색 및 파란색 / 주황색 막대가 나란히 표시됩니다.
나는 유사한 많은 질문과 답변 보았다 이 막대 차트를 사용하여 막대의 폭을 계산 제안을하지만,이 아웃 - 오브 - 박스 지원해야한다, 적어도하기 matplotlib에서 뭔가처럼 보인다.
또한 seaborn으로 직접 누적 히스토그램을 그릴 수 있습니까? 나는 방법을 찾을 수 없었다.
plt.hist( [correct_a, incorrect_a], bins=edges, stacked=True, color=['green', 'red'], rwidth=0.95, alpha=0.5)

plt.hist( [correct_b, incorrect_b], bins=edges, stacked=True, color=['green', 'red'], rwidth=0.95, alpha=0.5)

답변
1 PéterLeéh Aug 19 2020 at 16:25
음, plt.bar여기에서 최선의 방법 이라고 생각 합니다. 누적 히스토그램을 생성하려면 해당 bottom
인수를 사용할 수 있습니다 . 두 개의 막대 차트를 나란히 표시하려면 이 원래 matplotlib 예제 에서와 같이 x
값을 some 만큼 이동할 수 있습니다 .width
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(16, 8))
correct_a = np.random.randint(0, 20, 20)
incorrect_a = np.random.randint(0, 20, 20)
correct_b = np.random.randint(0, 20, 20)
incorrect_b = np.random.randint(0, 20, 20)
edges = len(correct_a)
width=0.35
rects1 = ax.bar(np.arange(edges), incorrect_a, width, color="red", label="incorrect_a")
rects2 = ax.bar(np.arange(edges), correct_a, width, bottom=incorrect_a, color='seagreen', label="correct_a")
rects3 = ax.bar(np.arange(edges) + width, incorrect_b, width, color="blue", label="incorrect_b")
rects4 = ax.bar(np.arange(edges) + width, correct_b, width, bottom=incorrect_b, color='orange', label="correct_b")
# placing the ticks to the middle
ticks_aligned = np.arange(edges) + width // 2
ax.set_xticks(np.arange(edges) + width / 2)
ax.set_xticklabels((str(tick) for tick in ticks_aligned))
ax.legend()
다음을 반환합니다.
BossaNova Aug 19 2020 at 16:05
다음은 2 개의 히스토그램에 대한 간단한 예입니다 (히스토그램은 쌓이지 않음).
# generating some data for this example:
a = [1,2,3,4,3,4,2,3,4,5,4,3,4,5,4,1,2,3,2,1,3,4,5,6,7,6,5,4,3,4,6,5,4,3,4]
b = [1,2,3,4,5,6,7,6,5,6,7,6,5,4,3,4,5,6,7,6,7,6,7,5,4,3,2,1,3,4,5,6,5,6,5,6,7,6,7]
# plotting 2 histograms with bars centered differently within each bin:
plt.hist(a, bins=5, align='left', rwidth=0.5)
plt.hist(b, bins=5, align='mid', rwidth=0.5, color='r')