简体   繁体   中英

Labels are cut off

Hi I am using the following code in order to plot a 3D chart:

%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')

And I am getting the following image: 在此处输入图像描述

The questions is how can fix the fact of the labels (z) and ticks in X and Y are cut off?

Thanks

If you have Matplotlib >= 3.1 you can do

fig = plt.figure(constrained_layout=True)

and remove fig.tight_layout()

To match the behavior of the image shown inline do

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

which will "shrink wrap" the figure to make sure all of the artists are visible. The down side is you lose control of the final size of the final saved figure.

If you want to keep control of the size of the final output and have an older version of Matplotlib removing fig.tight_layout() and manually adjust the location of the Axes until it looks good via ax.set_position ( https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.axes.Axes.set_position.html )


This output will be slightly different in mpl 3.3 (see https://matplotlib.org/3.3.0/users/whats_new.html#axes3d-no-longer-distorts-the-3d-plot-to-match-the-2d-aspect-ratio )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM