【问题标题】:Plot separate pandas dataframe as subplots [duplicate]将单独的熊猫数据框绘制为子图[重复]
【发布时间】:2021-05-18 21:01:35
【问题描述】:

我想将 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

拜托,你能帮帮我吗?

【问题讨论】:

  • ax=axes[0]ax=axes[1]
  • 解决了。感谢您的快速回答

标签: python pandas matplotlib


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2013-08-16
    • 2018-03-19
    • 2021-09-26
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多