To create multiple plots in Python using matplotlib, we can use the subplots
function. This function returns a Figure object and an array of Axes objects, which are used to create individual plots.
Here's the general syntax:
main.py98 chars4 lines
nrows
and ncols
specify the number of rows and columns of subplots we want to create.
sharex
and sharey
are optional arguments that determine whether the subplots share the same x-axis and y-axis, respectively.
We can then use the individual Axes objects to plot our data. Here's an example:
main.py477 chars24 lines
This code generates two subplots, sharing the same x-axis, and plots the sine and cosine functions on them. We also set some plot-level properties such as a title and axis labels.
The resulting plot should display two stacked plots, with the sine function in the top plot and the cosine function in the bottom plot.
gistlibby LogSnag