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()
Related
I'm trying to create a 4x2 plot on a slightly non-rectangular dataset (x-axis range is smaller than y-axis range) using plt.subplots and assign colorbars using make_axes_locatable to have the colorbars nice and flush wih the subplots. I always end up with huge paddings/margins between the two subplot columns which I suspect originate from the colorbars... I've tried multiple things from many stackoverflow questions (e.g. using fig.subplots_adjust(), constrained_layout=True etc.) but to no avail. The margins between the two columns stay really large (see img below) and make the image unreadable...
Any input would be much appreciated!
Code used to reproduce the issue:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
data = np.random.random((10, 6))
fig, axs = plt.subplots(nrows=4, ncols=2, figsize=(16, 8), sharex=True, sharey=False, constrained_layout=True)
for idx in range(4):
# plot image
im1 = axs[idx, 0].imshow(data, cmap=cm.coolwarm)
im2 = axs[idx, 1].imshow(data, cmap=cm.viridis)
# make colorbars
ax1_divider = make_axes_locatable(axs[idx, 0])
cax1 = ax1_divider.append_axes("right", size="2%", pad=0.05)
cb1 = fig.colorbar(im1, cax=cax1, orientation="vertical")
ax2_divider = make_axes_locatable(axs[idx, 1])
cax2 = ax2_divider.append_axes("right", size="2%", pad=0.05)
cb2 = fig.colorbar(im2, cax=cax2, orientation="vertical")
You are plotting multiple images (which by default it tries to keep an equal aspect ratio), in which the height is greater than the width. Therefore, total height of the images > total width of the images.
It follows that one way to reduce the white spacing between columns is to reduce the width of the figure.
Try setting this:
fig, axs = plt.subplots(nrows=4, ncols=2, figsize=(4, 8), sharex=True, sharey=False, constrained_layout=True)
I've got code that produces a square image with smaller plots to the left and below the main image plot by using GridSpec with width_ratios and height_ratios:
import matplotlib.pyplot as plt
import numpy as np
# Some fake data.
imdata = np.random.random((100, 100))
extradata1 = np.max(imdata, axis=1)
extradata2 = np.max(imdata, axis=0)
fig = plt.figure(constrained_layout=True)
spec = fig.add_gridspec(ncols=2, nrows=2, width_ratios=(1, 8), height_ratios=(8, 1))
# Main image plot.
ax1 = fig.add_subplot(spec[:-1, 1:], aspect='equal')
ax1.imshow(imdata, cmap='viridis')
# Vertical (left) plot.
ax2 = fig.add_subplot(spec[:-1, 0], sharey=ax1)
ax2.plot(extradata1, range(imdata.shape[0]))
# Horizontal (bottom) plot.
ax3 = fig.add_subplot(spec[-1, 1:], sharex=ax1)
ax3.plot(range(imdata.shape[1]), extradata2)
plt.show()
I'd like the height of the left plot and the width of the bottom plot to be equal to the height and width of the main image, respectively. Currently as you can see the horizontal plot is wider than the image's horizontal size, and they also scale differently as the figure is scaled. Is it possible to constrain axis dimensions to those of other axes?
Calling imshow() with aspect='auto' should fix your problem:
ax1.imshow(imdata, cmap='viridis',aspect='auto')
For some more explanation on this, please look here:
Imshow: extent and aspect
import matplotlib.pyplot as plt
import numpy as np
# Some fake data.
imdata = np.random.random((100, 100))
extradata1 = np.max(imdata, axis=1)
extradata2 = np.max(imdata, axis=0)
fig = plt.figure(constrained_layout=True)
spec = fig.add_gridspec(ncols=2, nrows=2, width_ratios=(1, 8), height_ratios=(8, 1))
# Main image plot.
ax1 = fig.add_subplot(spec[:-1, 1:])
ax1.imshow(imdata, cmap='viridis',aspect='auto')
# Vertical (left) plot.
ax2 = fig.add_subplot(spec[:-1, 0], sharey=ax1)
ax2.plot(extradata1, range(imdata.shape[0]))
# Horizontal (bottom) plot.
ax3 = fig.add_subplot(spec[-1, 1:], sharex=ax1)
ax3.plot(range(imdata.shape[1]), extradata2)
Result:
Fourier's answer worked nicely, but I also found that I could get the desired behaviour by changing constrained_layout=True to constrained_layout=False in the plt.figure call.
Using aspect aspect="auto" works, it has the disadvantage of giving you non-square pixels.
For this kind of tasks, I found that the axes_grid toolkit is pretty useful
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Some fake data.
imdata = np.random.random((100, 100))
extradata1 = np.max(imdata, axis=1)
extradata2 = np.max(imdata, axis=0)
fig, main_ax = plt.subplots()
divider = make_axes_locatable(main_ax)
bottom_ax = divider.append_axes("bottom", 1.2, pad=0.1, sharex=main_ax)
left_ax = divider.append_axes("left", 1.2, pad=0.1, sharey=main_ax)
bottom_ax.xaxis.set_tick_params(labelbottom=False)
left_ax.yaxis.set_tick_params(labelleft=False)
main_ax.imshow(imdata, cmap='viridis')
left_ax.plot(extradata1, range(imdata.shape[0]))
bottom_ax.plot(range(imdata.shape[1]), extradata2)
plt.show()
I apologise for the titlegore, but I could not figure out how to phrase it in a different way. The problem is best illustrated by the picture below. As you can see, I made figure consisting of 5 subplots using matplotlibs gridspec, which are fit into 4 square panels. The three empty panels have their own sets of x coordinates, and require their own label. However, the data from the first two panels shares the X axis, and (given that the actual label will be lengthy) I'd rather have only a single label and a single set of ticks for both, as shown here.
But as you can see, this leaves a rather large gap of whitespace between the two panels where the label would have gone. And this is what I'd like to solve; I'd like to stretch the two panels in equal amounts to fill up this white space. At the same time the top of the top panel and the bottom of the bottom panel should still align with the subplot to the right, and the bottom of the two panels shouldn't interfere with the position of the row that comes below either. I looked into the documentation on adjusting the panels in the documentation but I couldn't figure it out.
As an aside I'd also like to have a single y-axis label for the two panels, but I think I can fudge that in with fig.text().
The code that generates the above plot:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import gridspec
xs = np.linspace(0,8*np.pi,101)
ys = np.cos(xs)
fig = plt.figure(figsize=(7.2,4.45*1.5))
gs1 = gridspec.GridSpec(4, 2, figure=fig)
#gs1.update(hspace=0.1)
ax1 = plt.subplot(gs1[0, 0])
ax1.plot(xs, ys)
#ax1.set_xlabel('X')
ax1.set_ylabel('Y1')
ax1.set_xticks([])
ax2 = plt.subplot(gs1[1, 0])
ax2.plot(xs, 0.5*ys)
ax2.set_xlabel('X')
ax2.set_ylabel('Y2')
ax2.set_ylim(-1,1)
gs2 = gridspec.GridSpec(4, 2)
ax3 = plt.subplot(gs2[0:2, 1])
ax3.set_xlabel('X3')
ax3.set_ylabel('Y3')
ax4 = plt.subplot(gs2[2:, 0])
ax4.set_xlabel('X4')
ax4.set_ylabel('Y4')
ax5 = plt.subplot(gs2[2:, 1])
ax5.set_xlabel('X5')
ax5.set_ylabel('Y5')
plt.tight_layout()
You can use a SubplotSpec in one of the quadrants of a 2x2 gridspec.
An example is found int gridspec-using-subplotspec.
Here it would look like
import numpy as np
from matplotlib import pyplot as plt
xs = np.linspace(0,8*np.pi,101)
ys = np.cos(xs)
fig = plt.figure(figsize=(7.2,4.45*1.5))
# 2x2 "outer" GridSpec
gs = fig.add_gridspec(2, 2)
# 2x1 "inner" GridSpec to be used
# in one cell of the outer grid
gs00 = gs[0,0].subgridspec(2, 1)
ax1 = fig.add_subplot(gs00[0])
ax1.plot(xs, ys)
ax1.set_ylabel('Y1')
ax1.set_xticks([])
ax2 = fig.add_subplot(gs00[1])
ax2.plot(xs, 0.5*ys)
ax2.set_xlabel('X')
ax2.set_ylabel('Y2')
ax2.set_ylim(-1,1)
ax3 = fig.add_subplot(gs[0,1])
ax3.set_xlabel('X3')
ax3.set_ylabel('Y3')
ax4 = fig.add_subplot(gs[1,0])
ax4.set_xlabel('X4')
ax4.set_ylabel('Y4')
ax5 = fig.add_subplot(gs[1,1])
ax5.set_xlabel('X5')
ax5.set_ylabel('Y5')
fig.tight_layout()
plt.show()
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()
I have some code to plot a grid, with the data in each cell being distinct and having a very specific position. The easiest way I found to do this was to create the grid with gridspec and use it to precisely position my subplots, however I'm having a problem where the overall grid is labelled from 0 to 1 along each axis. This happens every time, even when the dimensions of the grid are changed. Obviously these numbers have no relevance to my data, and as what I am aiming to display is qualitative rather than quantitative I would like to remove all labels from this plot entirely.
Here is a link to an image with an example of my problem
And here is the MWE that I used to create that image:
import numpy as np
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
# mock-up of data being used
x = 6
y = 7
table = np.zeros((x, y))
# plotting
fig = plt.figure(1)
gs = gridspec.GridSpec(x, y, wspace=0, hspace=0)
plt.title('Example Plot')
for (j, k), img in np.ndenumerate(table):
ax = fig.add_subplot(gs[x - j - 1, k])
ax.set_xticklabels('')
ax.set_yticklabels('')
plt.show()
I have not been able to find note of anything like this problem, so any help would be greatly appreciated.
If you just want to draw a grid over the plot, use this code:
import numpy as np
import matplotlib.pyplot as plt
# mock-up of data being used
x = 6
y = 7
table = np.zeros((x, y))
# plotting
fig = plt.figure(1)
plt.title('Example Plot')
plt.gca().xaxis.grid(True, color='darkgrey', linestyle='-')
plt.gca().yaxis.grid(True, color='darkgrey', linestyle='-')
plt.show()
Another variant is used gridspec:
...
# hide ticks of main axes
ax0 = plt.gca()
ax0.get_xaxis().set_ticks([])
ax0.get_yaxis().set_ticks([])
gs = gridspec.GridSpec(x, y, wspace=0, hspace=0)
plt.title('Example Plot')
for (j, k), img in np.ndenumerate(table):
ax = fig.add_subplot(gs[x - j - 1, k])
# hide ticks of gribspec axes
ax.get_xaxis().set_ticks([])
ax.get_yaxis().set_ticks([])