在Matplotlib中如何并排显示两个图像如show(fig1,fig2)

0 投票
1 回答
809 浏览
提问于 2025-04-17 23:27

一般来说,我在使用matplotlib的时候,把两个图放在一个图里,比如用plot(a);plot(b),是没有问题的。不过现在我在用一个特定的库,它会生成一个图形,我想在上面叠加一个箱线图。这两个图都是用matplotlib生成的,所以我觉得应该可以,但我只看到一个图。下面是我的代码。我在使用beeswarm库,这里是它的ipython笔记本。我只能画出beeswarm图或者箱线图,而不能在一个图里同时画出这两个。我的主要目标是把列散点图和箱线图一起保存为一个pdf文件。谢谢,

from beeswarm import beeswarm
fig=plt.figure()
figure(figsize=(5,7))
ax1=plt.subplot(111)
fig.ylim=(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2)

enter image description here

1 个回答

1

问题出在箱线图的位置上。默认情况下,位置列表是从1开始的,这就把箱线图移动到了1的位置,而你的蜜蜂群图则在0的位置。

所以这两个图在画布上的位置不一样。

我稍微修改了一下你的代码,这样似乎解决了你的问题。

from beeswarm import beeswarm
fig = plt.figure(figsize=(5,7))
ax1 = fig.add_subplot(111)
# Here you may want to use ax1.set_ylim(0,11) instead fig.ylim=(0,11)
ax1.set_ylim(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2,positions=[0])

祝好

撰写回答