5

I am trying to plot a scatterplot over an image without having any white space around it.

If I plot just the image as follows, then there is no white space:

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)

but as I add a scatter plot over the image as follows:

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)

At this point, by using the following savefig command, the white space is added around the image:

plt.savefig(im_filename,format="png",bbox_inches='tight',pad_inches=0)

Any idea on how to remove the white space definitely?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
fstab
  • 4,801
  • 8
  • 34
  • 66

2 Answers2

8

By switching to the mpl object-oriented style, you can plot both the image and the scatter plot on the same axes, and hence only have to set the whitespace once, by using ax.imshow and ax.scatter.

In the example below, I've used subplots_adjust to remove the whitespace around the axes, and ax.axis('tight') to set the axis limits to the data range.

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()

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Turning off the axes and setting axes limits can be also set at the very end before `show()` ? – fstab Feb 16 '16 at 12:07
  • 2
    did you try `savefig`? it seems that `plt.show` produces the desired result, but `savefig` (the invocation with parameters as I wrote them in the question) still adds the white margin – fstab Feb 16 '16 at 15:54
  • actually, it turns out the order of the scatter and the `ax.axis('tight'); ax.axis('off')` does matter. Move those before the scatter, and it should work. You also need to remove the `bbox_inches='tight',pad_inches=0` options – tmdavison Feb 16 '16 at 16:01
  • Unfortunately it is still not working. Everything seems so aleatory. Is there a reason that it has to be set before the plot? Why is it necessary to remove `bbox_inches='tight',pad_inches=0` from `savefig` ? – fstab Feb 16 '16 at 16:28
  • maybe I understood what the problem was, i was setting `dpi=450` as option to `savefig`. Without that, the white border is not generated. Still it's a problem because I need a high-res scatterplot over the image. – fstab Feb 16 '16 at 16:35
  • 2
    For some reason this trick does not seem to work in python3 matplotlib 2.0. I simply used ```ax.set_yticklabels([]); ax.set_xticklabels([]);ax.axis('off') ``` instead. –  Nov 08 '17 at 12:29
  • @rraadd88 Thank you! I was about to give up, but your solution works for me as well. – C. E. Nov 20 '17 at 15:24
0

This worked for expanding images to full screen in both show and savefig with no frames, spines or ticks, Note everything is done in the plt instance with no need to create the subplot, axis instance or bbox:

from matplotlib import pyplot as plt

# create the full plot image with no axes 
plt.subplots_adjust(left=0, right=1, bottom=0, top=1)
plt.imshow(im, alpha=.8)
plt.axis('off')

# add scatter points
plt.scatter(sx, sy, c="red", s=10, linewidths=.2, alpha=.8)

# display the plot full screen (backend dependent)
mng = plt.get_current_fig_manager()
mng.window.state('zoomed')

# save and show the plot
plt.savefig('im_filename_300.png', format="png", dpi=300)
plt.show()
plt.close()  # if you are going on to do other things

This worked for at least 600 dpi which was well beyond the original image resolution at normal display widths. This is very convenient for displaying OpenCV images without distortion using

import numpy as np
im = img[:, :, ::-1]

to convert the colour formats before the plt.imshow.

Pmason
  • 91
  • 4