plot multiple pandas series in multiple plots in python

To plot multiple pandas series in multiple plots in Python using matplotlib, first you need to have a pandas DataFrame containing the series you want to plot. You can then use matplotlib to create subplots and plot each series on a different subplot.

Here's some example code:

main.py
import pandas as pd
import matplotlib.pyplot as plt

# create some sample data
series1 = pd.Series([1, 2, 3])
series2 = pd.Series([4, 3, 5])
series3 = pd.Series([5, 6, 4])
df = pd.concat([series1, series2, series3], axis=1)
df.columns = ['series1', 'series2', 'series3']

# create subplots
fig, axs = plt.subplots(nrows=len(df.columns), ncols=1, figsize=(8, 8))

# plot each series on a different subplot
for i, col in enumerate(df.columns):
    axs[i].plot(df.index, df[col])
    axs[i].set_title(col)

# adjust subplots
plt.tight_layout()

# show plot
plt.show()
565 chars
24 lines

This will create a plot with three subplots, one for each series in the DataFrame. Each subplot will have a line plot of the corresponding series, and the title of the subplot will indicate which series it is. Adjust the figsize argument to suit your needs.

related categories

gistlibby LogSnag