我正在尝试在图像上绘制散点图,周围没有任何空白。
如果我只绘制如下图像,则没有空白区域:
fig = plt.imshow(im,alpha=alpha,extent=(0,1,1,0))
plt.axis('off')
fig.axes.axis('tight')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
但是当我在图像上添加一个散点图时,如下所示:
fig = plt.scatter(sx, sy,c="gray",s=4,linewidths=.2,alpha=.5)
fig.axes.axis('tight')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
此时,通过使用以下savefig 命令,在图像周围添加了空白区域:
plt.savefig(im_filename,format="png",bbox_inches='tight',pad_inches=0)
关于如何明确删除空白的任何想法?
最佳答案
通过切换到 mpl 面向对象的样式,您可以在同一轴上绘制图像和散点图,因此只需使用 ax.imshow
设置一次空白和 ax.scatter
。
在下面的示例中,我使用了 subplots_adjust
删除轴周围的空白,ax.axis('tight')
将轴限制设置为数据范围。
import matplotlib.pyplot as plt
import numpy as np
# Load an image
im = plt.imread('stinkbug.png')
# Set the alpha
alpha = 0.5
# Some random scatterpoint data
sx = np.random.rand(100)
sy = np.random.rand(100)
# Creare your figure and axes
fig,ax = plt.subplots(1)
# Set whitespace to 0
fig.subplots_adjust(left=0,right=1,bottom=0,top=1)
# Display the image
ax.imshow(im,alpha=alpha,extent=(0,1,1,0))
# Turn off axes and set axes limits
ax.axis('tight')
ax.axis('off')
# Plot the scatter points
ax.scatter(sx, sy,c="gray",s=4,linewidths=.2,alpha=.5)
plt.show()
关于python - 如何完全去除散点图周围的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35429984/