0

I would like to plot pandas dataframes as subplots.

I read this post: How can I plot separate Pandas DataFrames as subplots?

Here is my minimum example where, like the accepted answer in the post, I used the ax keyword:

import pandas as pd

from matplotlib.pyplot import plot, show, subplots

import numpy as np  

# Definition of the dataframe

df = pd.DataFrame({'Pressure': {0: 1, 1: 2, 2: 4}, 'Volume': {0: 2, 1: 4, 2: 8}, 'Temperature': {0: 3, 1: 6, 2: 12}})


# Plot
 
fig,axes = subplots(2,1)

df.plot(x='Temperature', y=['Volume'], marker = 'o',ax=axes[0,0])

df.plot(x='Temperature', y=['Pressure'], marker = 'o',ax=axes[1,0])


show() 

Unfortunately, there is a problem with the indices:

df.plot(x='Temperature', y=['Volume'], marker = 'o',ax=axes[0,0])

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

Please, could you help me ?

2
  • 2
    ax=axes[0] and ax=axes[1] Commented May 18, 2021 at 13:00
  • It is solved. Thank you for your quick answer Commented May 18, 2021 at 13:07

1 Answer 1

0

If you have only one dimension (like 2 x 1 subplots), you can just used axes[0] and axes[1]. When you have two dimensional subplots (2 x 3 subplots for example), you indeed need slicing with two numbers.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.