

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. 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. To place the legend for each curve or subplot adding label. Plot the curve on all the subplots (3), with different labels, colors. Create a figure and a set of subplots, using the subplots () method, considering 3 subplots. Using numpy, create points for x, y1, y2 and 圓.

You can reproduce Figure 6 using this code fig = plt.figure(figsize=(16, 6)) coord = # create coord array from 241, 242, 243. To add legends in a subplot, we can take the following Steps. 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. Create a user-defined function, Init, to draw a clear frame. Set the figure size and adjust the padding between and around the subplots. coord 111 means, you generate a figure that consists of one row, one column, and you insert the subplot in the first sequence axes. To animate using Matplotlib with subplots and ArtistAnimation, we can take the following steps. 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).
