简体   繁体   English

如何使用 Matplotlib/Seaborn 并排绘制两个堆叠直方图

[英]How to draw two stacked histograms side-by-side with Matplotlib/Seaborn

I am drawing a couple of stacked histograms using the code below.我正在使用下面的代码绘制几个堆叠的直方图。 I am using the same bin edges for both so they are aligned nicely.我对两者都使用了相同的 bin 边缘,因此它们可以很好地对齐。

How can I have these displayed on the same chart?我怎样才能将这些显示在同一张图表上? Ie a green/red and a blue/orange bar per each bin -- side-by-side.即每个箱子都有一个绿色/红色和一个蓝色/橙色条 - 并排。

I saw many questions and answers similar to this suggesting using a bar chart and calculating the width of the bars, but this seems like something that should be supported out-of-the-box, at least in matplotlib.我看到很多与此类似的问题和答案,建议使用条形图并计算条形的宽度,但这似乎应该是开箱即用的,至少在 matplotlib 中是这样。

Also, can I draw stacked histograms directly with seaborn?另外,我可以直接用 seaborn 绘制堆叠直方图吗? I wasn't able to find a way.我找不到办法。

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)

在此处输入图像描述

Well, I think plt.bar is your best bet here.好吧,我认为plt.bar是您最好的选择。 To create stacked histograms, you can use its bottom argument.要创建堆叠直方图,您可以使用其bottom参数。 To display two bar charts side-by-side you can shift the x values by some width , just like in this original matplotlib example:要并排显示两个条形图,您可以将x值移动一些width ,就像在这个原始的 matplotlib 示例中一样:

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()

This returns:这将返回:

1个

Here is a simple example (histograms are not stacked) for 2 histograms displayed together with each bin having a dedicated place for each of them side by side:这是一个简单的示例(直方图未堆叠),其中显示了 2 个直方图,每个 bin 都有一个并排的专用位置:

# 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM