Matplotlib子图自定义布局问询:6张子图按指定行列排列实现
How to Create Your Custom 3-Row Subplot Layout in Matplotlib
Hey there! The problem with your current code is that plt.subplots(6) creates 6 subplots arranged in a single vertical column—totally not what you want for that custom 3-row layout. To build the setup you described:
- Column 1 is occupied entirely by subplot
a1(spanning all 3 rows) - Column 2 has
a2(row 1) anda3(row 2) - Column 3 has
c1(row 1),c2(row 2), andc3(row 3)
you'll need to use Matplotlib's tools for non-uniform subplot grids. Below are two straightforward methods to implement this:
Method 1: Using gridspec (Flexible and Recommended)
gridspec lets you define a grid of rows and columns, then assign subplots to span specific sections of that grid:
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec # Set up the figure and grid (3 rows, 3 columns) fig = plt.figure(figsize=(12, 8)) # Adjust size to fit your needs gs = gridspec.GridSpec(3, 3) # 3 rows, 3 columns # Assign each subplot to its position with proper spans a1 = fig.add_subplot(gs[:, 0]) # Column 0, all 3 rows a2 = fig.add_subplot(gs[0, 1]) # Row 0, Column 1 a3 = fig.add_subplot(gs[1, 1]) # Row 1, Column 1 c1 = fig.add_subplot(gs[0, 2]) # Row 0, Column 2 c2 = fig.add_subplot(gs[1, 2]) # Row 1, Column 2 c3 = fig.add_subplot(gs[2, 2]) # Row 2, Column 2 # Your existing plotting code goes here # Compounding Amount being plotted a1.plot(x_indexes, Amount_list) c1.plot(x_indexes, Non_compounding_list) L = 1 S = 1 x_long = [] x_short = [] for i in L_Amount_list: x_long.append(L) L += 1 for i in S_Amount_list: x_short.append(S) S += 1 a2.plot(x_short, S_Amount_list) a3.plot(x_long, L_Amount_list) c2.plot(x_short, S_Non_compounding_list) c3.plot(x_long, L_Non_compounding_list) # Optional: Add titles and clean up spacing a1.set_title('a1: Compounding Amount') a2.set_title('a2: Short Compounding') a3.set_title('a3: Long Compounding') c1.set_title('c1: Non-Compounding Amount') c2.set_title('c2: Short Non-Compounding') c3.set_title('c3: Long Non-Compounding') plt.tight_layout() # Automatically adjusts subplot spacing plt.show()
Method 2: Using subplot2grid (More Intuitive for Beginners)
subplot2grid uses a grid shape and location coordinates to place subplots, with options to set how many rows/columns each subplot spans:
import matplotlib.pyplot as plt fig = plt.figure(figsize=(12, 8)) # Syntax: subplot2grid((total_rows, total_cols), (start_row, start_col), rowspan, colspan) a1 = plt.subplot2grid((3, 3), (0, 0), rowspan=3) # Spans 3 rows, 1 column a2 = plt.subplot2grid((3, 3), (0, 1)) # Starts at row 0, column 1 a3 = plt.subplot2grid((3, 3), (1, 1)) # Starts at row 1, column 1 c1 = plt.subplot2grid((3, 3), (0, 2)) # Starts at row 0, column 2 c2 = plt.subplot2grid((3, 3), (1, 2)) # Starts at row 1, column 2 c3 = plt.subplot2grid((3, 3), (2, 2)) # Starts at row 2, column 2 # Your plotting code (same as above) a1.plot(x_indexes, Amount_list) c1.plot(x_indexes, Non_compounding_list) L = 1 S = 1 x_long = [] x_short = [] for i in L_Amount_list: x_long.append(L) L += 1 for i in S_Amount_list: x_short.append(S) S += 1 a2.plot(x_short, S_Amount_list) a3.plot(x_long, L_Amount_list) c2.plot(x_short, S_Non_compounding_list) c3.plot(x_long, L_Non_compounding_list) # Add titles and adjust layout a1.set_title('a1: Compounding Amount') a2.set_title('a2: Short Compounding') a3.set_title('a3: Long Compounding') c1.set_title('c1: Non-Compounding Amount') c2.set_title('c2: Short Non-Compounding') c3.set_title('c3: Long Non-Compounding') plt.tight_layout() plt.show()
Key Notes
- Both methods will create exactly the layout you described.
gridspecis more powerful for complex grids, whilesubplot2gridis easier to read for simple non-uniform layouts. - The
figsizeparameter lets you adjust the overall size of your figure to make sure all subplots are readable. plt.tight_layout()is crucial to prevent overlapping labels and titles.
内容的提问来源于stack exchange,提问作者selcuk egese
相关产品推荐
相关产品推荐

