Home > Backend Development > Python Tutorial > How Can I Plot Multiple Pandas DataFrames in Matplotlib Subplots?

How Can I Plot Multiple Pandas DataFrames in Matplotlib Subplots?

Barbara Streisand
Release: 2024-12-08 18:22:12
Original
641 people have browsed it

How Can I Plot Multiple Pandas DataFrames in Matplotlib Subplots?

Plotting Multiple Dataframes in Subplots

When working with multiple Pandas DataFrames with a common value scale, you may desire to visualize them within the same plot using subplots.

To achieve this, you can leverage the functionality of matplotlib. Firstly, create the subplots manually using plt.subplots(), specifying the desired number of rows and columns.

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, ncols=2)
Copy after login

Here, axes is an array containing the individual subplot axes, each of which you can access through indexing.

Now, you can plot each DataFrame on a specific subplot by passing the ax keyword argument within the plot() method. For instance, to plot df1 on the first subplot, you would use:

df1.plot(ax=axes[0,0])
Copy after login

To ensure a shared x-axis across all subplots, you can specify sharex=True when creating the subplots:

fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True)
Copy after login

By following these steps, you can effortlessly visualize multiple DataFrames within subplots, allowing for convenient comparisons and analysis.

The above is the detailed content of How Can I Plot Multiple Pandas DataFrames in Matplotlib Subplots?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template