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 ?

Julien
  • 763
  • 8
  • 32

1 Answers1

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.

Rutger
  • 593
  • 5
  • 11