【问题标题】:Labels are cut off标签被切断
【发布时间】:2020-06-27 08:44:35
【问题描述】:

您好,我正在使用以下代码来绘制 3D 图表:

%matplotlib inline


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(rf_pd['max_depth'], rf_pd['n_estimators'], [1] - rf_pd['mean_train_score'], c='red', s=60)
ax.view_init(50, 320)
ax.set(xlabel='Max depth', ylabel='Nº estimators', zlabel='Error: 1 - F1 Score')
plt.title('Random Forest - F1 error al entrenar por parámetros')
fig.tight_layout()
plt.show()
fig.savefig('a.png')

我得到以下图像:

问题是如何解决标签 (z) 和 X 和 Y 中的刻度被截断的事实?

谢谢

【问题讨论】:

  • 也许试试: fig.tight_layout() ?有时会有所帮助。
  • 不行
  • 如果你添加一些填充,例如fig.tight_layout(pad=5.0)?
  • 预览最差,因为刻度太近了

标签: python matplotlib jupyter-notebook


【解决方案1】:

如果你有 Matplotlib >= 3.1 你可以这样做

fig = plt.figure(constrained_layout=True)

并删除fig.tight_layout()

为了匹配内联显示的图像的行为

fig.savefig('a.png', bbox_inches='tight')

这将“收缩包装”图形以确保所有艺术家都可见。不利的一面是您无法控制最终保存的图形的最终大小。

如果您想控制最终输出的大小,并使用旧版本的 Matplotlib 删除 fig.tight_layout() 并手动调整 Axes 的位置,直到通过 ax.set_position (https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.axes.Axes.set_position.html) 看起来不错

p>

此输出在 mpl 3.3 中会略有不同(请参阅https://matplotlib.org/3.3.0/users/whats_new.html#axes3d-no-longer-distorts-the-3d-plot-to-match-the-2d-aspect-ratio

【讨论】:

  • 这是否意味着如果我使用 mpl>=3.1,我将不得不在具有所需 figsize 的截断 3d 绘图或所有艺术家可见但具有随机 figsize 的 3d 绘图之间妥协?
猜你喜欢
  • 2018-11-07
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多