【发布时间】:2020-05-19 14:35:20
【问题描述】:
我在 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)
【问题讨论】:
-
我不确定我是否知道您所说的“同一个数字上的多个箱线图”是什么意思。你有多个子图吗?
-
不,
dataToPlot包含多个数据样本,plt.boxplot将其视为:它绘制的箱线图与输入中的样本数量一样多。 -
那么,你不能再做一个
axins=zoomed_inset_axes(ax,6,loc=2)并为下一个绘图设置不同的坐标范围吗? -
我没有设置每个箱线图的位置,所以我不知道它们会出现在哪里。还是我错过了什么?
-
也许我不完全知道你的问题是什么......你想自动设置缩放图的范围,而不是明确输入 yrange?
标签: python plot matplotlib zooming boxplot