假设我正在使用 matplotlib 绘制 3D 散点图。我正在使用此处给出的代码:https ://matplotlib.org/2.1.1/gallery/mplot3d/scatter3d.html
出于我的目的,我需要删除轴、刻度线等。所以我这样做:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
ax.set_axis_off()
Run Code Online (Sandbox Code Playgroud)
我还删除了所有轴标签。要删除白色填充,我将像这样保存图形:
plt.savefig("test.png", bbox_inches = 'tight', pad_inches = 0)
plt.show()
Run Code Online (Sandbox Code Playgroud)
但我想要的是一个仅限制所有数据点所在的部分的图形,如下所示:
使用subplots_adjust。这将消除轴周围(以及轴之间,如果有多个)的任何空间,因此不存在“图形死空间”。
fig, ax = plt.subplots()
ax.scatter(np.random.random(100), np.random.random(100))
ax.set_axis_off()
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
fig.savefig('test.png', edgecolor='r', lw=2) # red line to highlight extent of figure
Run Code Online (Sandbox Code Playgroud)
与没有subplots_adjust