如何将不同数据帧中选定列的数据绘制到子图中?

2024-10-01 17:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个由两个线图组成的子图,两个图中的每一个都应该绘制来自不同数据帧的列。你知道吗

第一个子图应该在同一数据帧(curves1_monthly)中显示所有列作为行。另一个子图应该在另一个数据帧(curves2_yearly)中显示两列作为行。因此,两个数据集/帧的分辨率不同。你知道吗

曲线格式1\u每月:

    Date                       Col1   ...     Col9
0   2000-01-01 00:00:00+01:00  0.45   ...     0.34
1   2000-02-01 00:00:00+01:00  0.56   ...     0.72
2   2000-03-01 00:00:00+01:00  0.12   ...     0.04
3   2000-04-01 00:00:00+02:00  0.57   ...     0.98
4   2000-05-01 00:00:00+02:00  0.39   ...     0.63
.   ...                        ...    ...     ...
.   ...                        ...    ...     ...

曲线格式2\u每年:

Date        Column1       Column2        
2000        45.50         2.40
2001        46.70         7.00
2002        50.20         1.20
2003        32.40         3.40
2004        38.90         6.90
.           .             .
.           .             .
.           .             .

我的绘图功能:

def plot_curves(curves1_monthly, curves2_yearly):
    fig, axes = plt.subplots(nrows=1, ncols=2)
    # First subplot: all columns in df curves1_monthly except the 
    # 'Date' col:
    for curve_name in curves1_monthly[1:]:
        curves1_monthly.plot(kind='line', y=curve_name, ax=axes[0, 
                                                                0])
    # Second subplot:
    curves2_yearly.plot(kind='line', x='Date', y='Column1', 
                                                  ax=axes[0, 1])
    curves2_yearly.plot(kind='line', x='Date', y='Column2', 
                                                  ax=axes[0, 1])

当我做这个的时候

我收到此错误消息:

File "/Users/myself/.../my_program.py", line 46, in plot_curves curves1_monthly.plot(kind='line', y=curve_name, ax=axes[0, 0]) IndexError: too many indices for array

这里怎么了?你知道吗


Tags: 数据nameindateplotlineax曲线
1条回答
网友
1楼 · 发布于 2024-10-01 17:26:36

我认为一般来说,只是axes指数稍微偏离了。当您有一行绘图时,您只需要索引列号,类似于this example。此外,您还可以使用Pandas中的plot函数为您做很多工作:

def plot_curves(curves1_monthly, curves2_yearly):
    fig, axes = plt.subplots(nrows=1, ncols=2)
    # First subplot:
    curves1_monthly.plot(kind='line', ax=axes[0])
    # Second subplot:
    curves2_yearly.plot(kind='line', x='Date', ax=axes[1])

希望你所寻找的是类似于以下的东西:

相关问题 更多 >

    热门问题