如何在不将所有条形堆叠在一起的情况下绘制两个带有堆叠条形图的直方图?
how to plot two histograms with stacked bars, without stacking all the bars together?
我有 4 个直方图,可以说是 A、B、C 和 D。我想将直方图 A 和 B 一起绘制,带有堆叠条,以及直方图 C 和 D,也带有堆叠条,但不堆叠四个直方图在一起。所以我想在一个带有并排条的直方图中有两个堆叠的直方图。
到目前为止,我可以用堆积条绘制 A-B-C-D;或 A-B 和 C-D 在不同的堆叠直方图中,但两个直方图的条不是并排的。是我的代码:
plot=[A,B,C,D] #values from 0-10
ww=[wA,wB,wC,wD] #weights
堆叠的所有条形:
plt.hist(plot,bins=10,weights=ww,label=['A','B','C','D'],histtype="barstacked")
A-B 直方图 + C-D 直方图,但一个直方图隐藏了另一个:
plt.hist(plot[0:2],bins=10,weights=ww[0:2],label=['loses','wins'],stacked=True)
plt.hist(plot[2:4],bins=10,weights=ww[2:4],label=['l','w'],stacked=True)
在此先感谢您的帮助!
你可以使用 hist()
returns:
的补丁列表
import numpy as np
import matplotlib.pyplot as plt
A = np.arange(100)
B = np.arange(100)
C = np.arange(100)
D = np.arange(100)
wA = np.abs(np.random.normal(size=100))
wB = np.abs(np.random.normal(size=100))
wC = np.abs(np.random.normal(size=100))
wD = np.abs(np.random.normal(size=100))
plot=[A,B,C,D] #values from 0-10
ww=[wA,wB,wC,wD] #weights
n, bins, patches = plt.hist(plot[0:2],bins=10,weights=ww[0:2],label=['loses','wins'],stacked=True)
n, bins, patches2 = plt.hist(plot[2:4],bins=10,weights=ww[2:4],label=['l','w'],stacked=True)
for patch in patches:
plt.setp(patch, 'width', 10)
for patch in patches2:
plt.setp(patch, 'width', 5)
plt.show()
更新
我发现有一种更好更简洁的方法可以做到这一点:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
# the data
A = np.arange(10)
B = np.arange(10)
C = np.arange(10)
D = np.arange(10)
wA = np.abs(np.random.normal(size=10))
wB = np.abs(np.random.normal(size=10))
wC = np.abs(np.random.normal(size=10))
wD = np.abs(np.random.normal(size=10))
## necessary variables
width = 0.5 # the width of the bars
## the bars
rects1 = ax.bar(A - width/2, wA, width,
color='blue')
rects2 = ax.bar(B- width/2, wB, width, bottom=wA,
color='green')
rects3 = ax.bar(C + width/2, wC, width,
color='red')
rects4 = ax.bar(D + width/2, wD, width, bottom=wC,
color='yellow')
# axes and labels
ax.set_xlim(-width,len(A)+width)
plt.show()
结果呢:
有关详细信息,请参阅 this link。
我有 4 个直方图,可以说是 A、B、C 和 D。我想将直方图 A 和 B 一起绘制,带有堆叠条,以及直方图 C 和 D,也带有堆叠条,但不堆叠四个直方图在一起。所以我想在一个带有并排条的直方图中有两个堆叠的直方图。
到目前为止,我可以用堆积条绘制 A-B-C-D;或 A-B 和 C-D 在不同的堆叠直方图中,但两个直方图的条不是并排的。是我的代码:
plot=[A,B,C,D] #values from 0-10
ww=[wA,wB,wC,wD] #weights
堆叠的所有条形:
plt.hist(plot,bins=10,weights=ww,label=['A','B','C','D'],histtype="barstacked")
A-B 直方图 + C-D 直方图,但一个直方图隐藏了另一个:
plt.hist(plot[0:2],bins=10,weights=ww[0:2],label=['loses','wins'],stacked=True)
plt.hist(plot[2:4],bins=10,weights=ww[2:4],label=['l','w'],stacked=True)
在此先感谢您的帮助!
你可以使用 hist()
returns:
import numpy as np
import matplotlib.pyplot as plt
A = np.arange(100)
B = np.arange(100)
C = np.arange(100)
D = np.arange(100)
wA = np.abs(np.random.normal(size=100))
wB = np.abs(np.random.normal(size=100))
wC = np.abs(np.random.normal(size=100))
wD = np.abs(np.random.normal(size=100))
plot=[A,B,C,D] #values from 0-10
ww=[wA,wB,wC,wD] #weights
n, bins, patches = plt.hist(plot[0:2],bins=10,weights=ww[0:2],label=['loses','wins'],stacked=True)
n, bins, patches2 = plt.hist(plot[2:4],bins=10,weights=ww[2:4],label=['l','w'],stacked=True)
for patch in patches:
plt.setp(patch, 'width', 10)
for patch in patches2:
plt.setp(patch, 'width', 5)
plt.show()
更新
我发现有一种更好更简洁的方法可以做到这一点:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
# the data
A = np.arange(10)
B = np.arange(10)
C = np.arange(10)
D = np.arange(10)
wA = np.abs(np.random.normal(size=10))
wB = np.abs(np.random.normal(size=10))
wC = np.abs(np.random.normal(size=10))
wD = np.abs(np.random.normal(size=10))
## necessary variables
width = 0.5 # the width of the bars
## the bars
rects1 = ax.bar(A - width/2, wA, width,
color='blue')
rects2 = ax.bar(B- width/2, wB, width, bottom=wA,
color='green')
rects3 = ax.bar(C + width/2, wC, width,
color='red')
rects4 = ax.bar(D + width/2, wD, width, bottom=wC,
color='yellow')
# axes and labels
ax.set_xlim(-width,len(A)+width)
plt.show()
结果呢:
有关详细信息,请参阅 this link。