fertes.blogg.se

Matplotlib subplot
Matplotlib subplot









This is how our input and output will look like in python. Plt.plot(a, b, label = '2nd Straight line', color = 'red')Ĩ. Plt.plot(a, b, label = '1st Straight line') Plt.plot(x, z, label = "cos function", c = 'red') (Here we have passed 2, 2 as first two numbers to get a 2 x 2 grid) plt.plot(x, y, label = "sine function") Next, we will learn to create 4 subplots, for which we will need 2 x 2 grid.Ĭode: import numpy as npĪ = ī = Ĭ = This is how our input and output will look like in python.Īs we can see in our output, we have got 2 plots stacked vertically (a 2 x 1 grid).ħ. (Here we have passed 2, 1 as first two numbers to get a 2 x 1 grid) plt.plot(a,b, label = "sin function") We are now ready to create our sub plots plt.subplot(2,1, 1) Let us take 2 functions, sine and cosine for this example a = np.arange(0, 10, 0.1) Next, let us learn how can we place these plots vertically.įor this, we will have to create a 2 x 1 grid. Also, the subplots have taken respective positions as per our argument.ĥ. Plt.plot(c,d, label = "cos function", c = 'red')Īs we can see, our output has 2 plots placed in 1 row and 2 columns as per our input argument. This is how our input and output will look like in python: Plt.plot(c,d, label = "cos function", c = 'red') # Now the 3rd number will define the position of the 2 plots.Ĭode: plt.plot(a,b, label = "sin function") 25) for i in range(rows*columns): exec (f"plt.subplot(grid”).3.

#Matplotlib subplot 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.

matplotlib subplot

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. You can reproduce Figure 6 using this code fig = plt.figure(figsize=(16, 6)) coord = # create coord array from 241, 242, 243. 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.

matplotlib subplot

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. It consists of three numbers representing the number of rows, columns, and the axes’ sequence. The variable coord in the code above is 111.

matplotlib subplot

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).









Matplotlib subplot