plottools.py 1.81 KB
Newer Older
1
import matplotlib.pyplot as plt
2
import numpy as np
3
from .timeseries import *
4
5


6
7
8
9
10
11
def plot_timeseries(figure_id, timeseries, plt_linestyle='-', plt_linewidth=2, plt_color=None):
    """
    This function plots either a single timeseries or several timeseries in the figure defined by figure_id.
    Several timeseries (handed over in a list) are plotted in several subplots.
    In order to plot several timeseries in one plot, the function is to be called several times (hold is activated).
    """
12
    plt.figure(figure_id)
13
    if not isinstance(timeseries, list):
14
15
16
17
        if plt_color:
            plt.plot(timeseries.time, timeseries.values, linestyle=plt_linestyle, label=timeseries.label, linewidth=plt_linewidth, color=plt_color)
        else:
            plt.plot(timeseries.time, timeseries.values, linestyle=plt_linestyle, label=timeseries.label, linewidth=plt_linewidth)
18
19
        plt.gca().autoscale(axis='x', tight=True)
        plt.legend()
20
21
22
    else:
        for ts in timeseries:
            plt.subplot(len(timeseries), 1, timeseries.index(ts) + 1)
23
24
25
26
            if plt_color:
                plt.plot(ts.time, ts.values, linestyle=plt_linestyle, label=ts.label, linewidth=plt_linewidth, color=plt_color)
            else:
                plt.plot(ts.time, ts.values, linestyle=plt_linestyle, label=ts.label, linewidth=plt_linewidth)
27
28
            plt.gca().autoscale(axis='x', tight=True)
            plt.legend()
29

30
31
32
33
34
35
36
37
38
39
40

def set_timeseries_labels(timeseries, timeseries_labels):
    """
    Sets label attribute of timeseries, later used in plotting functions.
    Suitable for single timeseries as well as for several timeseries (handed over in a list).
    """
    if not isinstance(timeseries, list):
        timeseries.label = timeseries_labels
    else:
        for ts in timeseries:
            ts.label = timeseries_labels[timeseries.index(ts)]