Matplotlib Subplot Inside Subplot
In your example, you're defining a 2x1 subplot and only looping through two axes objects that get created. In each of the two loops, when you call dfcol_pat_columns.plotx'Week',axax, since col_pat_columns is a list and you're passing it to df, you're just plotting multiple columns from your dataframe.That's why it's multiple series on a single plot.
From matplotlib 3.0 on, you can use matplotlib.axes.Axes.inset_axes. import numpy as np import matplotlib.pyplot as plt fig, axes plt.subplots2,2 for ax in axes
Create multiple subplots using plt.subplots . pyplot.subplots creates a figure and a grid of subplots with a single call, while providing reasonable control over how the individual plots are created. For more advanced use cases you can use GridSpec for a more general subplot layout or Figure.add_subplot for adding subplots at arbitrary locations within the figure.
The subplot will take the index position on a grid with nrows rows and ncols columns. index starts at 1 in the upper left corner and increases to the right. index can also be a two-tuple specifying the first, last indices 1-based, and including last of the subplot, e.g., fig.add_subplot3, 1, 1, 2 makes a subplot that spans the upper 23
Here we used the plt.subplot syntax inside the loop and specified which subplot index we should plot the data. We used Python's enumerate function so we could record the index position n of the list as we iterate it. We need to add 1 to this number as enumerate starts counting from zero, but plt.subplot starts counting at 1.
Suppose you know total subplots and total columns you want to use. import matplotlib.pyplot as plt Subplots are organized in a Rows x Cols Grid Tot and Cols are known Tot number_of_subplots Cols number_of_columns Compute Rows required Rows Tot Cols EDIT for correct number of rows If one additional row is necessary -gt add one if Tot Cols ! 0 Rows 1 Create a
matplotlib.pyplot.subplots matplotlib.pyplot. subplots nrows 1, ncols 1, , sharex False, sharey False, squeeze True, width_ratios None, height_ratios None, subplot_kw None, gridspec_kw None, fig_kw source Create a figure and a set of subplots. This utility wrapper makes it convenient to create common layouts of subplots, including the enclosing figure object, in a
Parameters args int, int, int, index, or SubplotSpec, default 1, 1, 1. The position of the subplot described by one of. Three integers nrows, ncols, index.The subplot will take the index position on a grid with nrows rows and ncols columns.index starts at 1 in the upper left corner and increases to the right.index can also be a two-tuple specifying the first, last indices 1-based
Subplots in Matplotlib In Matplotlib, subplots are multiple plots displayed within the same figure. This allows us to compare different data sets or visualize . In this example, we create a figure with a 22 layout of subplots and nest two subplots inside one of the subplots. Subplot Grids.
For more options, see Create multiple subplots using plt.subplots. import matplotlib.pyplot as plt import numpy as np Create some fake data. x1 np. linspace 0.0, 5.0 y1 np. cos Michael Droettboom and the Matplotlib development team 2012-2025 The Matplotlib development team. Created using Sphinx 8.2.3. Built from v3.10.3-2