从图形中移除坐标轴

4 投票
1 回答
4108 浏览
提问于 2025-04-16 20:52

有没有办法从 pyplot.figure() 中去掉坐标轴呢?

使用 pyplot.imsave() 来创建没有坐标轴的图像是可以的,效果不错。

plt.imsave(file, zi)

使用 pyplot.imsave() 生成的图像

不过这个方法有点限制,因为它只能处理网格数据。

当我使用 pyplot.figure() 并用 pyplot.savefig() 保存时,像这样:

...
# create figure
fig = plt.figure(figsize=(1.0,1.0))
# apply contour plot
plt.contour(zi,15,linewidths=0.1,colors='k')
plt.contourf(zi,15,cmap=plt.cm.jet)
# flip the y-axis
ax = plt.gca()
ax.set_ylim(ax.get_ylim()[::-1])
# save to file, 256x256 pixels
plt.savefig(file1, dpi=256)

使用 pyplot.savefig() 生成的图像

保存下来的图像上还是会有坐标轴,正如上面的图所示。

1 个回答

6

我用下面的代码成功去掉了坐标轴的显示,以及为坐标轴留出的任何空隙:

fig = plt.figure(figsize=(1.0,1.0))
ax = fig.add_axes([0.0, -0.2, 1.2, 1.2])
plt.contour(zi,15,linewidths=0.1,colors='k')
plt.contourf(zi,15,cmap=plt.cm.jet)
ax.set_ylim(ax.get_ylim()[::-1])
ax.set_axis_off()
plt.savefig(file1, dpi=256)

撰写回答