Python implementation drawing multiple subgraphs on a canvas

Time:2020-6-28

Matplotlib can combine many small graphs and display them in a large graph. The method used is called subplot

Draw evenly

Import using import matplotlib.pyplot Module, and abbreviated as PLT plt.figure Create an image window


import matplotlib.pyplot as plt
 
plt.figure()

use plt.subplot To create a small diagram plt.subplot (2,2,1) means that the whole image window is divided into two rows and two columns, and the current position is 1. Use plt.plot ([0,1], [0,1]) creates a small graph at position 1


plt.subplot(2,2,1)
plt.plot([0,1],[0,1])

plt.subplot (2,2,2) means that the whole image window is divided into two rows and two columns, and the current position is 2. Use plt.plot ([0,1], [0,2]) creates a small graph at position 2


plt.subplot(2,2,2)
plt.plot([0,1],[0,2])

plt.subplot (2,2,3) means that the whole image window is divided into two rows and two columns, and the current position is 3 plt.subplot (2,2,3) can be abbreviated as plt.subplot (223), Matplotlib can also identify plt.plot ([0,1], [0,3]) creates a small graph at position 3


plt.subplot(223)
plt.plot([0,1],[0,3])

plt.subplot (224) means that the whole image window is divided into 2 rows and 2 columns, and the current position is 4. Use plt.plot ([0,1], [0,4]) creates a small graph at position 4

plt.subplot(224)
plt.plot([0,1],[0,4])
 
plt.show () Exhibition

Uneven drawing

If you want to show different sizes of small graphs, what should you do? Take the above four small graphs as an example, if you put the first small graph on the first row, and the remaining three small graphs on the second row

use plt.subplot (2,1,1) divide the whole image window into 2 lines and 1 column, and the current position is 1. Use plt.plot ([0,1], [0,1]) creates a small graph at position 1


plt.subplot(2,1,1)
plt.plot([0,1],[0,1])

use plt.subplot (2,3,4) divide the whole image window into 2 rows and 3 columns, and the current position is 4. Use plt.plot ([0,1], [0,2]) creates a small graph at position 4


plt.subplot(2,3,4)
plt.plot([0,1],[0,2])

Here we need to explain why the fourth position puts the second small figure. We used it in the previous step plt.subplot (2,1,1) divide the whole image window into two rows and one column. The first small figure occupies the first position, that is, the whole first row. In this step, use plt.subplot (2,3,4) the whole image window is divided into two rows and three columns, so the first row of the whole image window becomes three columns, that is to say, three positions, So the first position of the second line is the fourth position of the whole image window

use plt.subplot (235) divide the whole image window into 2 rows and 3 columns, and the current position is 5. Use plt.plot ([0,1], [0,3]) create a small graph at the 5th position. Do the same and create again plt.subplot (236).

plt.subplot(235)
plt.plot([0,1],[0,3])
 
plt.subplot(236)
plt.plot([0,1],[0,4])
 
plt.show () Exhibition

Full code

plt.figure()
plt.subplot(2,1,1)
plt.plot([0,1],[0,1])
 
plt.subplot(2,3,4)
plt.plot([0,1],[0,2])
 
plt.subplot(235)
plt.plot([0,1],[0,3])
 
plt.subplot(236)
plt.plot([0,1],[0,4])
 
plt.show () Exhibition

give the result as follows

The above Python implementation drawing multiple subgraphs on a canvas is all the content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developepaer more.