gpt4 book ai didi

python - 有没有办法在 matplotlib 中制作多个水平箱线图?

转载 作者:太空狗 更新时间:2023-10-30 02:34:43 24 4
gpt4 key购买 nike

我正在尝试制作一个 matplotlib 图,它将多个水平箱线图相互堆叠。该文档显示了如何制作单个水平箱线图以及如何制作多个垂直方向的图 in this section.

我尝试在以下代码中使用子图:

import numpy as np
import pylab as plt

totfigs = 5

plt.figure()
plt.hold = True

for i in np.arange(totfigs):
x = np.random.random(50)
plt.subplot('{0}{1}{2}'.format(totfigs,1,i+1))
plt.boxplot(x,vert=0)
plt.show()

虽然我的输出结果只是一个水平箱线图。

有什么建议吗?

编辑:感谢@joaquin,我修复了plt.subplot调用行。现在子图版本有效,但仍然希望箱线图全部在一个图中......

最佳答案

如果我没理解错的话,您只需要向 boxplot 传递一个列表(或二维数组),其中包含您要绘制的每个数组。

import numpy as np
import pylab as plt

totfigs = 5

plt.figure()
plt.hold = True
boxes=[]
for i in np.arange(totfigs):
x = np.random.random(50)
boxes.append(x)

plt.boxplot(boxes,vert=0)
plt.show()

enter image description here

关于python - 有没有办法在 matplotlib 中制作多个水平箱线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6665771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com