Inspired by this question, I've been trying to get images plotted without gaps.
In my toy example I have four images that I want to place in two rows. They have different shapes: different number of rows, same number of columns. Despite the differences, they should fit in a single figure without gaps, as in the following illustration:
When I try to get them together, though, setting plt.subplots_adjust(wspace=0, hspace=0)
doesn't do the trick, because the images have different shapes.
Here's the code:
from numpy.random import rand
import matplotlib.pyplot as plt
test_data = [[rand(10,10), rand(10,10)],[rand(5,10), rand(5,10)]]
f, axarr = plt.subplots(2,2)
for i in range(2):
for j in range(2):
axarr[i, j].imshow(test_data[i][j])
plt.tight_layout()
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
I've tried playing around with set_aspect
and equal
, but without luck.
Does anyone know how to get those gaps away?
To remove gaps between bars, we can change the align value to center in the argument of bar () method. Create a dictionary called data with two keys, milk and water. Get the list of keys and values in the dictionay. Using subplots () method, create a figure and add a set of two subplots. On axis 2, use bar method to plot bars without gaps.
Using subplots () method, create a figure and add a set of two subplots. On axis 2, use bar method to plot bars without gaps. Set the width attribute as 1.0. Set the title using set_title () method. Use tight_layout () to adjust the padding between and around the subplots.
Hiding the Whitespaces and Borders in the Matplotlib figure When we use plt.axis (‘off’) command it hides the axis, but we get whitespaces around the image’s border while saving it. To remove/hide whitespace around the border, we can set bbox_inches=’tight’ in the savefig () method.
Unfortunately even the tight_layout () function tends to cause the subplot titles to overlap: The way to resolve this issue is by increasing the height padding between subplots using the h_pad argument: You can find more Matplotlib tutorials here.
You can set the height_ratios
of the subplots using the gridspec_kw
argument in the call to plt.subplots
, and use the heights of the different images to set that ratio.
From the docs to plt.subplots()
:
gridspec_kw : dict, optional
Dict with keywords passed to the GridSpec constructor used to create the grid the subplots are placed on.
Note that your example had the exact aspect ratio of the default matplotlib image size, so you wouldn't notice any gaps appearing until you add more rows, or change the shapes of the images.
So, to expand this to a general solution, you will need to set the figure size according to the shapes of the images. For example, lets expand your example to 3 rows, 2 columns. We'll also explicitly set the figure width to 8 inches, and adjust the height based on the image sizes.
from numpy.random import rand
import matplotlib.pyplot as plt
test_data = [[rand(10,10), rand(10,10)],[rand(5,10), rand(5,10)],[rand(2,10), rand(2,10)]]
cmaps = [['viridis', 'binary'], ['plasma', 'coolwarm'], ['Greens', 'copper']]
heights = [a[0].shape[0] for a in test_data]
widths = [a.shape[1] for a in test_data[0]]
fig_width = 8. # inches
fig_height = fig_width * sum(heights) / sum(widths)
f, axarr = plt.subplots(3,2, figsize=(fig_width, fig_height),
gridspec_kw={'height_ratios':heights})
for i in range(3):
for j in range(2):
axarr[i, j].imshow(test_data[i][j], cmap=cmaps[i][j])
axarr[i, j].axis('off')
plt.subplots_adjust(wspace=0, hspace=0, left=0, right=1, bottom=0, top=1)
plt.show()
You can combine all images into a single array, which you then plot with imshow
.
To make sure that there is no extra spacing around the image, we need to calculate the aspect ratio and set the figure size accordingly. (To show that I changed the lower images' pixel number to 4).
from numpy.random import rand
import matplotlib.pyplot as plt
import numpy as np
test_data = [[rand(10,10), rand(10,10)],[rand(4,10), rand(4,10)]]
a = np.c_[np.array(test_data[0][0]),np.array(test_data[0][1]) ]
b = np.c_[np.array(test_data[1][0]),np.array(test_data[1][1]) ]
c = np.r_[a,b]
dpi=100
width = 5 #inch
height = width*c.shape[0]/float(c.shape[1])
fig, ax = plt.subplots(figsize=(width,height ), dpi=dpi)
ax.imshow(c)
ax.axis("off")
plt.subplots_adjust(0,0,1,1)
plt.savefig(__file__+".png")
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With