Put images produced by another function into subplot - python

So I was provided a function show_image that takes an array, and basically calls
plt.imshow(img, cmap='gray')
plt.axis('off')
in its body. It correctly shows the image, that's no problem.
I was asked to create four images with this function, and show them in one figure, each as a subplot. I think I have to use the function and am not supposed to change things inside that function (so I can't add a new return or anything)
I'm not sure how to get the images to show/arrange as subplots of a larger image. I tried
fig, ([ax1, ax2], [ax3, ax4]) = plt.subplots(2, 2,figsize=(12,16))
ax1 = show_image(image)
ax2 = show_image(log)
...
but that didn't work. Specifically, it seems that only the last image was created, and that image always shows on the bottom right subplot. What else could I do?

You can have the plotting functionality in your show_image function
import matplotlib.pyplot as plt
import numpy as np
def show_image(*images):
# get the 4 subplots
fig, ax = plt.subplots(2, 2,figsize=(12,16))
# now assign each images to each subplot
ax[0][0].imshow(images[0], cmap='gray')
ax[0][1].imshow(images[1], cmap='gray')
ax[1][0].imshow(images[2], cmap='gray')
ax[1][1].imshow(images[3], cmap='gray')
plt.axis('off')
arr1 = np.random.randint(255, size=(28,28))
arr2 = np.random.randint(255, size=(28,28))
arr3 = np.random.randint(255, size=(28,28))
arr4 = np.random.randint(255, size=(28,28))
show_image(arr1, arr2, arr3, arr4)

Related

plotting a grid of png with matplotlib

I have found multiple similar questions with this subject but so far I couldn't adapt any solution to my needs, so I'm sorry for reposting.
I'm trying to plot a grid of png images using matplotlib, the closest I've got to what I want is using the code below, which can be found here https://matplotlib.org/stable/gallery/axes_grid1/simple_axesgrid.html .
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
im1 = np.arange(100).reshape((10, 10))
im2 = im1.T
im3 = np.flipud(im1)
im4 = np.fliplr(im2)
fig = plt.figure(figsize=(4., 4.))
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols=(2, 2), # creates 2x2 grid of axes
axes_pad=0.1, # pad between axes in inch.
)
for ax, im in zip(grid, [im1, im2, im3, im4]):
# Iterating over the grid returns the Axes.
ax.imshow(im)
plt.show()
My question is, how do I get rid of the x and y ticks/labels and also give each image a title?
Again, I'm sorry for repeating the question.
This code
import matplotlib.pyplot as plt
image = plt.imread("sample.png")
fig, axes = plt.subplots(2, 3)
for row in [0, 1]:
for column in [0, 1, 2]:
ax = axes[row, column]
ax.set_title(f"Image ({row}, {column})")
ax.axis('off')
ax.imshow(image)
plt.show()
is going to produce

How to save a series of matplotlib plots as an image file?

I'm trying to save a series of matplotlib figures as one single image file with many slices. To put things in perspective, the following is the code I'm using:
for n in range(len(image.shape[0])): #this image here is a timelapse image
plt.imshow(image[n, :, :], cmap='gray')
ax = plt.gca()
for acontour in contour_list:
ax.add_patch(patches.Polygon(acontour[:, [1, 0]],linewidth=1,edgecolor='r',facecolor='none'))
plt.show()
I'm trying to overlay the corresponding contour on the original image for every slice and save all the images.
Thanks in advance.
Will this help:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2,len(image.shape[0])//2, figsize=(15, 6))
fig.subplots_adjust(hspace = .5, wspace=.001)
axs = axs.ravel()
for n in range(len(image.shape[0])):
# Your plot code
plt.savefig('image.png')

How to make the size of subplot equally?

I am using matplotlib and GridSpec to plot 9 images in 3x3 subplots.
fig = plt.figure(figsize=(30,40))
fig.patch.set_facecolor('white')
gs1 = gridspec.GridSpec(3,3)
gs1.update(wspace=0.05, hspace=0.05)
ax1 = plt.subplot(gs1[0])
ax2 = plt.subplot(gs1[1])
ax3 = plt.subplot(gs1[2])
ax4 = plt.subplot(gs1[3])
ax5 = plt.subplot(gs1[4])
ax6 = plt.subplot(gs1[5])
ax7 = plt.subplot(gs1[6])
ax8 = plt.subplot(gs1[7])
ax9 = plt.subplot(gs1[8])
ax1.imshow(img1,cmap='gray')
ax2.imshow(img2,cmap='gray')
...
ax9.imshow(img9,cmap='gray')
However, the images have a different size from each row. For example, the first-row images size is 256x256, the images in the second row have a size of 200x200 and the third row has a size of 128x128
I want to plot the images in the subplot with same size. How should I use it in python? Thanks
This is an example of 4x3 subplot
Don't use matplotlib.gridspec, but use figure.add_subplot as demonstrated with the runnable code below. However, when doing some plotting, you need to set_autoscale_on(False) to suppress its behavior of size adjusting.
import numpy as np
import matplotlib.pyplot as plt
# a function that creates image array for `imshow()`
def make_img(h):
return np.random.randint(16, size=(h,h))
fig = plt.figure(figsize=(8, 12))
columns = 3
rows = 4
axs = []
for i in range(columns*rows):
axs.append( fig.add_subplot(rows, columns, i+1) )
# axs[-1] is the new axes, write its title as `axs[number]`
axs[-1].set_title("axs[%d]" % (i))
# plot raster image on this axes
plt.imshow(make_img(i+1), cmap='viridis', alpha=(i+1.)/(rows*columns))
# maniputate axs[-1] here, plot something on it
axs[-1].set_autoscale_on(False) # suppress auto sizing
axs[-1].plot(np.random.randint(2*(i+1), size=(i+1)), color="red", linewidth=2.5)
fig.subplots_adjust(wspace=0.3, hspace=0.4)
plt.show()
The resulting plot:
I suppose you want to show the images in different sizes, such that all pixels of the different images are equally sized.
This is in general hard, but for the case where all images in a row (or column) of the subplot grid are of the same size, it becomes easy. The idea can be to use the gridspec's height_ratios (or width_ratios in case of columns) argument and set it to the image's pixel height (width).
import matplotlib.pyplot as plt
import numpy as np
images = [np.random.rand(r,r) for r in [25,20,12] for _ in range(3)]
r = [im.shape[0] for im in images[::3]]
fig, axes = plt.subplots(3,3, gridspec_kw=dict(height_ratios=r, hspace=0.3))
for ax, im in zip(axes.flat, images):
ax.imshow(im)
plt.show()

Losing edge values in last colormap subplot matplotlib

I'm using python 3.5.2 and matplotlib 1.5.3.
I'm doing some colormaps with the "y" axis shared.
The problem is that when placing the colorbar in the last subplot I lose the first and last tick on the x axis. But if you enlarge the figure (figsize = (12,3) for example) some white space appears at the edges of the other subplot.
import numpy as np
import matplotlib.pyplot as plt
matrix = np.random.random((10, 10, 3))
fig = plt.figure(figsize=(10, 3)) # Try a figsize=(12, 3)
for i in range(3):
if i == 0:
ay1 = plt.subplot(1, 3, i+1)
else:
plt.subplot(1, 3, i+1, sharey=ay1)
plt.imshow(matrix[:, :, i], interpolation='nearest')
if i == 2:
plt.colorbar()
plt.show()
What is the correct way to do it?
Using sharey only makes sense, if you use different sized images. But when the images are differently sized, in some parts of the figure there is nothing - which will be painted white.
On the other hand, if you do have same size pictures as here, there is no need to use sharey. In that case, you can simply plot your data and add a colorbar.
import numpy as np
import matplotlib.pyplot as plt
matrix = np.random.random((10, 10, 3))
fig, ax = plt.subplots(1,3, figsize=(12, 3))
plt.subplots_adjust(left=0.05, right=0.85)
for i in range(3):
im = ax[i].imshow(matrix[:, :, i], interpolation='nearest')
ax[i].set_aspect("equal")
plt.draw()
p = ax[-1].get_position().get_points().flatten()
ax_cbar = fig.add_axes([0.9,p[1], 0.02, p[3]-p[1]] )
plt.colorbar(im, cax=ax_cbar)
plt.show()

Scaling images generated by imshow

The following code snippet
import matplotlib.pyplot as plt
import numpy as np
arr1 = np.arange(100).reshape((10,10))
arr2 = np.arange(25).reshape((5,5))
fig, (ax1, ax2, ) = plt.subplots(nrows=2, figsize=(3,5))
ax1.imshow(arr1, interpolation="none")
ax2.imshow(arr2, interpolation="none")
plt.tight_layout()
plt.show()
produces two images with the same size, but a much lower "pixel density" in the second one.
I would like to have the second image plotted at the same scale (i.e. pixel density) of the first, without filling the subfigure, possibly correctly aligned (i.e. the origin of the image in the same subplot position as the first one.)
Edit
The shapes of arr1 and arr2 are only an example to show the problem. What I'm looking for is a way to ensure that two different images generated by imshow in different portions of the figure are drawn at exactly the same scale.
The simplest thing I could think of didn't work, but gridspec does. The origins here aren't aligned explicitly, it just takes advantage of how gridspec fills rows (and there's an unused subplot as a spacer).
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec
sizes = (10, 5)
arr1 = np.arange(sizes[0]*sizes[0]).reshape((sizes[0],sizes[0]))
arr2 = np.arange(sizes[1]*sizes[1]).reshape((sizes[1],sizes[1]))
# Maybe sharex, sharey? No, we pad one and lose data in the other
#fig, (ax1, ax2, ) = plt.subplots(nrows=2, figsize=(3,5), sharex=True, sharey=True)
fig = plt.figure(figsize=(3,5))
# wspace so the unused lower-right subplot doesn't squeeze lower-left
gs = gridspec.GridSpec(2, 2, height_ratios = [sizes[0], sizes[1]], wspace = 0.0)
ax1 = plt.subplot(gs[0,:])
ax2 = plt.subplot(gs[1,0])
ax1.imshow(arr1, interpolation="none")
ax2.imshow(arr2, interpolation="none")
plt.tight_layout()
plt.show()

Categories