将单独的熊猫数据框绘制为子图

时间:2021-05-18 12:58:24

标签: python pandas matplotlib

我想将 Pandas 数据框绘制为子图。

我读过这篇文章:How can I plot separate Pandas DataFrames as subplots?

这是我的最小示例,就像帖子中接受的答案一样,我使用了 ax 关键字:

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() 

不幸的是,索引有问题:

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

拜托,你能帮我吗?

1 个答案:

答案 0 :(得分:0)

如果你只有一个维度(比如 2 x 1 子图),你可以只使用轴 [0] 和轴 [1]。当您有二维子图(例如 2 x 3 子图)时,您确实需要用两个数字进行切片。