简体   繁体   English

在相同的 plot 中添加另一个时间序列

[英]Adding another time-series in the same plot

I have the code that is shown below and I want to add another time-series in the data with different color and I am not able to it.我有下面显示的代码,我想在数据中添加另一个颜色不同的时间序列,但我做不到。 Can anyone help?谁能帮忙? My time-series data are presented below.我的时间序列数据如下所示。 The main thing I am trying to do is compare those two timeseries in the same plot in order to acquire further data about the profile of the production.我想做的主要事情是比较同一 plot 中的这两个时间序列,以便获取有关生产概况的更多数据。

import numpy as np
from datetime import datetime, date, time

import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.cm import ScalarMappable, get_cmap
from matplotlib.dates import DateFormatter
from pandas.core.groupby import SeriesGroupBy


def load_data(
    filename: str = 'WTPV_Spring_2016.csv',
) -> pd.DataFrame:
    """
    Load the indicated CSV, parsing dates.
    """

    return pd.read_csv(
        filepath_or_buffer=filename,
        names=('Date', 'Energy'), parse_dates=[0],
        encoding='utf8', delimiter=';',
    )


def make_quantiles(
    df: pd.DataFrame,
    quantiles: np.ndarray,
) -> pd.Series:
    """
    Apply the given quantiles over time groups. The input quantiles are assumed
    to include 0 and 1 but these are not included in the calculation.
    The output is a series with a multi-level index: time, quantile. For plot
    compatibility the time is represented as a time of day on Jan 1 1970.
    """
    fake_date = date(1970, 1, 1)
    def dt_to_time(t: time) -> datetime:
        return datetime.combine(fake_date, t)

    times = df.Date.dt.time.apply(dt_to_time)
    df.set_index(times, inplace=True)
    by_time: SeriesGroupBy = df.Energy.groupby(level=0)
    bands = by_time.quantile(quantiles[1:-1])
    bands.index.names = ('Time', 'Quantile')
    return bands


def plot(quantiles: np.ndarray, bands: pd.Series) -> plt.Figure:
    """
    Plot the given quantile bands as filled regions with an associated colour
    bar. The colour bar shows only the first half of the quantile range; the
    second half is symmetric and implied.
    """
    fig, ax = plt.subplots()
    ax.set_title('WT & PV Spring 2016')
    ax.set_xlabel('Time')
    ax.set_ylabel('Energy (MWh)')

    fig.autofmt_xdate()
    ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))

    map = ScalarMappable(
        cmap=get_cmap('viridis'),
        norm=plt.Normalize(vmin=0, vmax=0.5),
    )
    ax.set_facecolor(map.to_rgba(0))
    fig.colorbar(map, label='quantile')

    counterposed_quantiles = np.vstack((
        quantiles[1: len(quantiles)//2],
        quantiles[-2: len(quantiles)//2: -1],
    )).T
    for q0, q1 in counterposed_quantiles:
        y0 = bands.loc[:, q0]
        x = y0.index
        y1 = bands.loc[x, q1]
        ax.fill_between(x, y0, y1, color=map.to_rgba(q0))

    q = 0.5
    y = bands.loc[:, q]
    x = y.index
    ax.plot(x, y, color=map.to_rgba(q))

    return fig


def main() -> None:
    quantiles = np.linspace(0, 1, 41)
    data = load_data()
    bands = make_quantiles(data, quantiles)
    plot(quantiles, bands)
    plt.show()


if __name__ == '__main__':
    main()

The timeseries时间序列

2016/09/01 00:00:00;460;0
2016/09/01 01:00:00;376;0
2016/09/01 02:00:00;320;0
2016/09/01 03:00:00;250;0
2016/09/01 04:00:00;207;0
2016/09/01 05:00:00;204;0
2016/09/01 06:00:00;176;0
2016/09/01 07:00:00;134;0
2016/09/01 08:00:00;89;103
2016/09/01 09:00:00;50;420
2016/09/01 10:00:00;43;837
2016/09/01 11:00:00;48;1213
2016/09/01 12:00:00;49;1470
2016/09/01 13:00:00;62;1594
2016/09/01 14:00:00;82;1609
2016/09/01 15:00:00;79;1506
2016/09/01 16:00:00;78;1288
2016/09/01 17:00:00;93;994
2016/09/01 18:00:00;147;645
2016/09/01 19:00:00;191;309
2016/09/01 20:00:00;202;72
2016/09/01 21:00:00;171;0
2016/09/01 22:00:00;163;0
2016/09/01 23:00:00;168;0
2016/09/02 00:00:00;209;0
2016/09/02 01:00:00;244;0
2016/09/02 02:00:00;237;0
2016/09/02 03:00:00;186;0
2016/09/02 04:00:00;142;0
2016/09/02 05:00:00;105;0
2016/09/02 06:00:00;62;0
2016/09/02 07:00:00;55;0
2016/09/02 08:00:00;66;94
2016/09/02 09:00:00;62;376
2016/09/02 10:00:00;49;745
2016/09/02 11:00:00;31;1085
2016/09/02 12:00:00;35;1341
2016/09/02 13:00:00;38;1496
2016/09/02 14:00:00;53;1527
2016/09/02 15:00:00;59;1457
2016/09/02 16:00:00;67;1286
2016/09/02 17:00:00;59;1005
2016/09/02 18:00:00;65;659
2016/09/02 19:00:00;84;326
2016/09/02 20:00:00;135;76
2016/09/02 21:00:00;170;0
2016/09/02 22:00:00;233;0
2016/09/02 23:00:00;281;0
2016/09/03 00:00:00;325;0
2016/09/03 01:00:00;380;0
2016/09/03 02:00:00;395;0
2016/09/03 03:00:00;395;0
2016/09/03 04:00:00;398;0
2016/09/03 05:00:00;433;0
2016/09/03 06:00:00;488;0
2016/09/03 07:00:00;532;0
2016/09/03 08:00:00;603;100
2016/09/03 09:00:00;725;418
2016/09/03 10:00:00;806;840
2016/09/03 11:00:00;886;1220
2016/09/03 12:00:00;938;1483
2016/09/03 13:00:00;966;1622
2016/09/03 14:00:00;941;1648
2016/09/03 15:00:00;956;1568
2016/09/03 16:00:00;919;1374
2016/09/03 17:00:00;881;1086
2016/09/03 18:00:00;858;726
2016/09/03 19:00:00;859;355
2016/09/03 20:00:00;822;78
2016/09/03 21:00:00;809;0
2016/09/03 22:00:00;822;0
2016/09/03 23:00:00;801;0
2016/09/04 00:00:00;733;0
2016/09/04 01:00:00;656;0
2016/09/04 02:00:00;593;0
2016/09/04 03:00:00;570;0
2016/09/04 04:00:00;560;0
2016/09/04 05:00:00;499;0
2016/09/04 06:00:00;417;0
2016/09/04 07:00:00;345;0
2016/09/04 08:00:00;321;100
2016/09/04 09:00:00;335;430
2016/09/04 10:00:00;365;861
2016/09/04 11:00:00;400;1253
2016/09/04 12:00:00;411;1526
2016/09/04 13:00:00;524;1664
2016/09/04 14:00:00;576;1688
2016/09/04 15:00:00;603;1610
2016/09/04 16:00:00;650;1425
2016/09/04 17:00:00;731;1138
2016/09/04 18:00:00;784;762
2016/09/04 19:00:00;885;371
2016/09/04 20:00:00;937;79
2016/09/04 21:00:00;984;0
2016/09/04 22:00:00;984;0
2016/09/04 23:00:00;1001;0
2016/09/05 00:00:00;984;0
2016/09/05 01:00:00;949;0
2016/09/05 02:00:00;928;0
2016/09/05 03:00:00;831;0
2016/09/05 04:00:00;869;0
2016/09/05 05:00:00;827;0
2016/09/05 06:00:00;806;0
2016/09/05 07:00:00;821;0
2016/09/05 08:00:00;793;98
2016/09/05 09:00:00;752;430
2016/09/05 10:00:00;764;865
2016/09/05 11:00:00;754;1263
2016/09/05 12:00:00;772;1539
2016/09/05 13:00:00;759;1673
2016/09/05 14:00:00;669;1697
2016/09/05 15:00:00;609;1603
2016/09/05 16:00:00;568;1384
2016/09/05 17:00:00;549;1078
2016/09/05 18:00:00;559;696
2016/09/05 19:00:00;628;320
2016/09/05 20:00:00;665;63
2016/09/05 21:00:00;677;0
2016/09/05 22:00:00;612;0
2016/09/05 23:00:00;616;0
2016/09/06 00:00:00;652;0
2016/09/06 01:00:00;738;0
2016/09/06 02:00:00;772;0
2016/09/06 03:00:00;794;0
2016/09/06 04:00:00;798;0
2016/09/06 05:00:00;738;0
2016/09/06 06:00:00;757;0
2016/09/06 07:00:00;745;0
2016/09/06 08:00:00;672;64
2016/09/06 09:00:00;668;252
2016/09/06 10:00:00;741;491
2016/09/06 11:00:00;880;718
2016/09/06 12:00:00;992;855
2016/09/06 13:00:00;1022;920
2016/09/06 14:00:00;1114;937
2016/09/06 15:00:00;1177;831
2016/09/06 16:00:00;1193;650
2016/09/06 17:00:00;1237;478
2016/09/06 18:00:00;1218;288
2016/09/06 19:00:00;1171;128
2016/09/06 20:00:00;1216;26
2016/09/06 21:00:00;1243;0
2016/09/06 22:00:00;1201;0
2016/09/06 23:00:00;1106;0
2016/09/07 00:00:00;1119;0
2016/09/07 01:00:00;1129;0
2016/09/07 02:00:00;1075;0
2016/09/07 03:00:00;1030;0
2016/09/07 04:00:00;967;0
2016/09/07 05:00:00;834;0
2016/09/07 06:00:00;794;0
2016/09/07 07:00:00;665;0
2016/09/07 08:00:00;714;43
2016/09/07 09:00:00;811;172
2016/09/07 10:00:00;886;353
2016/09/07 11:00:00;910;537
2016/09/07 12:00:00;874;708
2016/09/07 13:00:00;891;913
2016/09/07 14:00:00;855;1011
2016/09/07 15:00:00;831;1002
2016/09/07 16:00:00;851;969
2016/09/07 17:00:00;907;797
2016/09/07 18:00:00;898;508
2016/09/07 19:00:00;857;238
2016/09/07 20:00:00;839;47
2016/09/07 21:00:00;808;0
2016/09/07 22:00:00;755;0
2016/09/07 23:00:00;756;0
2016/09/08 00:00:00;716;0
2016/09/08 01:00:00;632;0
2016/09/08 02:00:00;562;0
2016/09/08 03:00:00;517;0
2016/09/08 04:00:00;519;0
2016/09/08 05:00:00;484;0
2016/09/08 06:00:00;461;0
2016/09/08 07:00:00;387;0
2016/09/08 08:00:00;278;60
2016/09/08 09:00:00;242;247
2016/09/08 10:00:00;248;477
2016/09/08 11:00:00;258;692
2016/09/08 12:00:00;189;870
2016/09/08 13:00:00;176;1040
2016/09/08 14:00:00;172;1105
2016/09/08 15:00:00;155;1033
2016/09/08 16:00:00;179;905
2016/09/08 17:00:00;188;701
2016/09/08 18:00:00;197;444
2016/09/08 19:00:00;201;215
2016/09/08 20:00:00;223;42
2016/09/08 21:00:00;228;0
2016/09/08 22:00:00;261;0
2016/09/08 23:00:00;249;0
2016/09/09 00:00:00;262;0
2016/09/09 01:00:00;252;0
2016/09/09 02:00:00;241;0
2016/09/09 03:00:00;229;0
2016/09/09 04:00:00;186;0
2016/09/09 05:00:00;204;0
2016/09/09 06:00:00;216;0
2016/09/09 07:00:00;240;0
2016/09/09 08:00:00;260;60
2016/09/09 09:00:00;347;276
2016/09/09 10:00:00;378;555
2016/09/09 11:00:00;431;809
2016/09/09 12:00:00;476;995
2016/09/09 13:00:00;501;1124
2016/09/09 14:00:00;491;1160
2016/09/09 15:00:00;517;1103
2016/09/09 16:00:00;514;979
2016/09/09 17:00:00;527;769
2016/09/09 18:00:00;527;487
2016/09/09 19:00:00;577;220
2016/09/09 20:00:00;666;39
2016/09/09 21:00:00;704;0
2016/09/09 22:00:00;694;0
2016/09/09 23:00:00;656;0
2016/09/10 00:00:00;596;0
2016/09/10 01:00:00;587;0
2016/09/10 02:00:00;574;0
2016/09/10 03:00:00;524;0
2016/09/10 04:00:00;481;0
2016/09/10 05:00:00;469;0
2016/09/10 06:00:00;541;0
2016/09/10 07:00:00;3;0
2016/09/10 08:00:00;14;65
2016/09/10 09:00:00;27;310
2016/09/10 10:00:00;27;629
2016/09/10 11:00:00;77;925
2016/09/10 12:00:00;66;1152
2016/09/10 13:00:00;4;1310
2016/09/10 14:00:00;94;1349
2016/09/10 15:00:00;514;1265
2016/09/10 16:00:00;527;1096
2016/09/10 17:00:00;7;845
2016/09/10 18:00:00;7;531
2016/09/10 19:00:00;66;240
2016/09/10 20:00:00;4;41
2016/09/10 21:00:00;694;0
2016/09/10 22:00:00;242;0
2016/09/10 23:00:00;248;0
2016/09/11 00:00:00;258;0
2016/09/11 01:00:00;189;0
2016/09/11 02:00:00;176;0
2016/09/11 03:00:00;172;0
2016/09/11 04:00:00;155;0
2016/09/11 05:00:00;179;0
2016/09/11 06:00:00;188;0
2016/09/11 07:00:00;197;0
2016/09/11 08:00:00;201;73
2016/09/11 09:00:00;223;359
2016/09/11 10:00:00;28;726
2016/09/11 11:00:00;261;1066
2016/09/11 12:00:00;242;1305
2016/09/11 13:00:00;48;1443
2016/09/11 14:00:00;258;1472
2016/09/11 15:00:00;189;1386
2016/09/11 16:00:00;176;1197
2016/09/11 17:00:00;172;914
2016/09/11 18:00:00;155;569
2016/09/11 19:00:00;179;254
2016/09/11 20:00:00;188;41
2016/09/11 21:00:00;197;0
2016/09/11 22:00:00;201;0
2016/09/11 23:00:00;223;0
2016/09/12 00:00:00;228;0
2016/09/12 01:00:00;261;0

The result I am aiming into is those two charts be together in one with each one having different color map.我的目标是将这两张图表合二为一,每一张都有不同的颜色 map。 在此处输入图像描述

在此处输入图像描述

Thank you very much for advance for the help.非常感谢您提前提供帮助。

How about this?这个怎么样? I'm assuming here, that you got the data as part of your code.我在这里假设,您将数据作为代码的一部分。

import pandas as pd

# load csv
df2 = pd.read_csv("two.csv", delimiter=';')

# plot all columns
ax = df2.plot.line()
ax.figure.savefig('test.png')

Output: Output:

生成的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM