#Matplotlib subplot example code#
For example, if you want to create Figure 6 (two rows and 4 columns) with gridspec, you can use this code import matplotlib.pyplot as plt fig = plt.figure(figsize=(16, 6)) rows = 2 columns = 4 grid = plt.GridSpec(rows, columns, wspace =. Create simple axes in a figure with gridspecĪs I mentioned before, besides using subplot to create some axes in a figure, you can also use gridspec. After that, create the subplot using the same procedure with the previous code, but place it in looping syntax. , 248 for i in range(len(coord)): plt.subplot(coord) plt.annotate('subplot ' + str(coord), xy = (0.5, 0.5), va = 'center', ha = 'center')īecause you want to create 8 axes (2 rows and 4 columns), so you need to make an array from 241 to 248. , 248 for i in range(1, 9): # in python, 9 is not included row = 2 column = 4 coord.append(str(row)+str(column)+str(i)) # create subplot 241, 242, 243. subplots creates a figure and a grid of subplots with a single call, while providing reasonable control over how the individual plots are created. Syntax: subplots (self, nrows1, ncols1, sharexFalse, shareyFalse, squeezeTrue, subplotkwNone, gridspeckwNone) Parameters: This method accept the following parameters that are described below: nrows, ncols : These parameter are the number of. You can reproduce Figure 6 using this code fig = plt.figure(figsize=(16, 6)) coord = # create coord array from 241, 242, 243. Without knowing the dimensions of the images, you could do something like this: for idx, arch in enumerate (bmps): i idx 3 Get subplot row j idx // 3 Get subplot column image Image.open (arch) iarshp np.array (image).shape Get h,w dimensions image nvert ('L') convert to grayscale Load grayscale matrix, reshape to. The subplots () method figure module of matplotlib library is used to display the figure window.
Create a simple subplot using looping in Matplotlib (Image by Author). You can use this code to generate it fig = plt.figure(figsize=(12, 4)) coord1 = 121 coord2 = 122 plt.subplot(coord1) plt.annotate('subplot ' + str(coord1), xy = (0.5, 0.5), va = 'center', ha = 'center') plt.subplot(coord2) plt.annotate('subplot ' + str(coord2), xy = (0.5, 0.5), va = 'center', ha = 'center')įigure 6. Next step is creating two horizontal axes in a figure, as shown in Figure 4. You also can generate Figure 3 without subplot syntax because you only generate one axes in a figure. Because you only have one row and one column (it means you only have one cell), your axes are the main figure. coord 111 means, you generate a figure that consists of one row, one column, and you insert the subplot in the first sequence axes. Try to use the func below to add colorbar: def addcolorbar(mappable): from mpltoolkits.axesgrid1 import makeaxeslocatable import matplotlib.pyplot as plt lastaxes plt.gca() ax mappable.axes fig ax.figure divider makeaxeslocatable(ax) cax divider.appendaxes('right', size'5', pad0.05) cbar fig.colorbar(mappable, caxcax) plt. It consists of three numbers representing the number of rows, columns, and the axes’ sequence. The variable coord in the code above is 111. One of the important things to understand the subplot in Matplotlib is defining the coordinate of the axes. You can generate Figure 3 using the following code import matplotlib.pyplot as plt fig = plt.figure() coord = 111 plt.subplot(coord) plt.annotate('subplot ' + str(coord), xy = (0.5, 0.5), va = 'center', ha = 'center') A simple subplot in Matplotlib (Image by Author).