Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove padding/border in a matplotlib subplot

Tags:

matplotlib

The second subplot is just the first image with an overlay ploted. In the second plot there appears to have white padding/boarder. How do I remove this padding/whitespace?

enter image description here

For completness, here is the fragment of code that performs the plotting:

fig, ax = plt.subplots(1, 2)
fig.set_size_inches(16, 6, forward=True)
plt.subplots_adjust(0.05, 0.05, 0.95, 0.95, 0.05, 0.05)
ax[0].set_title("Region Labels")
ax[0].imshow(image_labels)

ax[1].set_title("Region Connectivity Graph")
ax[1].imshow(image_labels)
for edge in edges:
    ax[1].plot([centers[edge[0]][0],centers[edge[1]][0]],
             [centers[edge[0]][1],centers[edge[1]][1]]) 
for a in ax:
    a.set_xticks(())
    a.set_yticks(())
plt.show()
like image 547
Michael Avatar asked Apr 22 '13 21:04

Michael


People also ask

How do I remove the margins in Matplotlib?

The python plotting library matplotlib will by default add margins to any plot that it generates. They can be reduced to a certain degree through some options of savefig() , namely bbox_inches='tight' and pad_inches=0 .

How do I remove spaces between subplots?

We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. The parameters wspace and hspace specify the space reserved between Matplotlib subplots.

How do I reduce the white space between subplots in Matplotlib?

subplots_adjust() Method to Change Space Between Subplots in Matplotlib. We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. wspace and hspace specify the space reserved between Matplotlib subplots.

What is padding in Matplotlib?

pad: This parameter is used for padding between the figure edge and the edges of subplots, as a fraction of the font size. h_pad, w_pad: These parameter are used for padding (height/width) between edges of adjacent subplots, as a fraction of the font size.


1 Answers

By default, Matplotlib adds some margin to plotted data. I cant test it because it dont have your image_labels and centers, but this should normally work:

ax[1].autoscale_view('tight')

An alternative would be to manually set the xlim and ylim of the axes:

ax[1].set_xlim(0,image_labels.shape[1])
ax[1].set_ylim(0,image_labels.shape[0])
like image 80
Rutger Kassies Avatar answered Sep 22 '22 14:09

Rutger Kassies