matplotlib:多个箱形图的插入轴

问题描述 投票:5回答:1

我在matplotlib中有一些箱线图,我想使用inset axes放大特定的y范围([0,0.1])。从文档中的example不清楚,我应该如何对同一图上的多个箱形图执行此操作。我试图修改此示例提供的代码,但是有太多不必要的复杂性。我的代码很简单:

# dataToPlot is a list of lists, containing some data. 
plt.figure()
plt.boxplot(dataToPlot)
plt.savefig( 'image.jpeg', bbox_inches=0)

如何添加插入轴并放大两者的第一个箱形图?我该怎么做?

编辑:我尝试了下面的代码,但这是我得到的:“在此处输入图像描述”“ >>

出了什么问题?

# what's the meaning of these two parameters?
fig = plt.figure(1, [5,4])
# what does 111 mean?
ax = fig.add_subplot(111)
ax.boxplot(data)
# ax.set_xlim(0,21)  # done automatically based on the no. of samples, right?
# ax.set_ylim(0,300) # done automatically based on max value in my samples, right?
# Create the zoomed axes
axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6, location = 1 (upper right)
axins.boxplot(data)
# sub region of the original image
#here I am selecting the first boxplot by choosing appropriate values for x1 and x2 
# on the y-axis, I'm selecting the range which I want to zoom in, right?
x1, x2, y1, y2 = 0.9, 1.1, 0.0, 0.01
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
# even though it's false, I still see all numbers on both axes, how do I remove them?
plt.xticks(visible=False)
plt.yticks(visible=False)
# draw a bbox of the region of the inset axes in the parent axes and
# connecting lines between the bbox and the inset axes area
# what are fc and ec here? where do loc1 and loc2 come from?
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
plt.savefig( 'img.jpeg', bbox_inches=0)

我在matplotlib中有一些箱线图,我想使用插入轴放大特定的y范围([0,0.1])。从文档中的示例对我来说还不清楚,我应如何针对...

python plot matplotlib zoom boxplot
1个回答
15
投票

loc确定缩放轴的位置,upper right为1,upper left为2,依此类推。我稍微修改了示例代码以生成多个缩放轴。

热门问题
推荐问题
最新问题