【发布时间】:2020-09-04 11:01:45
【问题描述】:
我正在绘制一个收敛图并显示与我使用连接箱线图的平均值的偏差:
由于某种原因,Matplotlib 强制为每个箱线图打勾,我似乎无法将它们删除。我的当前情节代码如下所示:
label = ["" for i in range(160)]
no_labels = int(np.floor(len(label)/20))
for i in range(no_labels):
label[i*20] = str(i*no_samples/no_labels)
# Weird behaviour for the last label so adding it manually
label[-1] = no_samples
fig = plt.figure(figsize=(10,5))
ax = fig.add_axes([0,0,1,1])
ax.set_xlabel("Samples", labelpad=10)
ax.set_ylabel("Error (MSE)", labelpad=10)
ax.set_ylim(0, 0.11)
ax.boxplot(data, flierprops=flyprops, showcaps=False,
boxprops=colorprops, whiskerprops={'color' : 'tab:blue'},
labels=label, patch_artist=True)
我尝试了多种操作 MPL 中可用的轴刻度的方法。
1) 试图让 MPL 完成工作:
ax.xaxis.set_major_locator(MultipleLocator(20))
2) 尝试手动设置刻度:ax.set_xticks([list_of_ticks])
3) 尝试了解决方法
ax.xaxis.set_minor_locator(MultipleLocator(20))
# Removing major ticks, setting minor ticks
ax.xaxis.set_tick_params(which='major', size=0, width=2, direction='in')
ax.yaxis.set_tick_params(which='major', size=5, width=2, direction='in')
这些似乎都不起作用,我不确定为什么。我认为这可能与我的 label 变量有关,但如果我不以这种方式包含它,MPL 会为每个条目都包含一个轴标签,这是一团糟。
如何在连接的箱线图中每 1000 个条目设置一次轴刻度?`
编辑:输入数据是一个形状为 (15, 160) s.t. 的 numpy 数组。有 160 个箱线图,每个箱线图有 15 个样本。 3 个样本的 5 个箱线图的示例数据如下所示:
np.random.rand(3,5)
>>> array([[0.05942481, 0.03408175, 0.84021109, 0.27531937, 0.62428798],
[0.24658313, 0.77910387, 0.2161348 , 0.39101172, 0.14038211],
[0.40694432, 0.22979738, 0.87056873, 0.788295 , 0.29337562]])
【问题讨论】:
-
这将有助于理解数据的样子。您能否包含一些示例数据(可能使用随机值生成)?
-
感谢您的回复,已编辑添加示例数据
标签: python matplotlib boxplot axis-labels xticks