kolja
kolja

Reputation: 547

plot all columns of a pandas dataframe with matplotlib

I have a dataframe with a datetime-index and 65 columns.
And I want to plot all these colums among themselves, not in a grid or in one figure.

df= test[['one', 'two', 'three','four']]
fig, ax = plt.subplots(figsize=(24, 5))
df.plot(ax=ax)
plt.show()

The example is all in one plot and not all among themselves.

Upvotes: 1

Views: 8243

Answers (1)

benrussell80
benrussell80

Reputation: 347

You can loop over the columns of the DataFrame and create a new figure for each column. This will plot them all at once. If you want the next one to show up once the previous one is closed then put the call to plt.show() inside the for loop.

import pandas as pd 
import matplotlib.pyplot as plt 


df = pd.DataFrame({
        'one': [1, 3, 2, 5],
        'two': [9, 6, 4, 3],
        'three': [0, 1, 1, 0],
        'four': [-3, 2, -1, 0]})

for i, col in enumerate(df.columns):
    df[col].plot(fig=plt.figure(i))
    plt.title(col)

plt.show()

Upvotes: 2

Related Questions