Beispiel #1
0
def make_timeseries_plot_2sided(time,
                                data1,
                                data2,
                                title,
                                units1,
                                units2,
                                monthly=True,
                                fig_name=None,
                                dpi=None):

    fig, ax1 = plt.subplots(figsize=(9, 6))
    ax1.plot_date(time, data1, '-', linewidth=1.5, color='blue')
    ax1.grid(True)
    if not monthly:
        monthly_ticks(ax1)
    ax1.set_ylabel(units1, color='blue', fontsize=16)
    ax1.tick_params(axis='y', labelcolor='blue')
    ax2 = ax1.twinx()
    ax2.plot_date(time, data2, '-', linewidth=1.5, color='red')
    ax2.get_yaxis().get_major_formatter().set_useOffset(False)
    ax2.set_ylabel(units2, color='red', fontsize=16)
    ax2.tick_params(axis='y', labelcolor='red')
    if np.amin(data1) < 0 and np.amax(data1) > 0 and np.amin(
            data2) < 0 and np.amax(data2) > 0:
        # Both timeseries cross 0. Line them up there.
        val1 = max(-np.amin(data1), np.amax(data1))
        val2 = max(-np.amin(data2), np.amax(data2))
        ax1.set_ylim([-val1, val1])
        ax2.set_ylim([-val2, val2])
        ax1.axhline(color='black')
    plt.title(title, fontsize=18)
    finished_plot(fig, fig_name=fig_name, dpi=dpi)
Beispiel #2
0
def timeseries_multi_plot(times,
                          datas,
                          labels,
                          colours,
                          title='',
                          units='',
                          monthly=True,
                          fig_name=None):

    # Figure out if time is a list or a single array that applies to all timeseries
    multi_time = isinstance(times, list)
    # Boolean which will tell us whether we need a line at 0
    crosses_zero = False

    fig, ax = plt.subplots(figsize=(11, 6))
    # Plot each line
    for i in range(len(datas)):
        if multi_time:
            time = times[i]
        else:
            time = times
        ax.plot_date(time,
                     datas[i],
                     '-',
                     color=colours[i],
                     label=labels[i],
                     linewidth=1.5)
        if (not crosses_zero) and (np.amin(datas[i]) < 0) and (np.amax(
                datas[i]) > 0):
            crosses_zero = True

    ax.grid(True)
    if crosses_zero:
        # Add a line at 0
        ax.axhline(color='black')
    if not monthly:
        monthly_ticks(ax)
    plt.title(title, fontsize=18)
    plt.ylabel(units, fontsize=16)
    # Move plot over to make room for legend
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
    # Make legend
    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

    finished_plot(fig, fig_name=fig_name)
Beispiel #3
0
def make_timeseries_plot(time,
                         data,
                         title='',
                         units='',
                         monthly=True,
                         fig_name=None):

    fig, ax = plt.subplots()
    ax.plot_date(time, data, '-', linewidth=1.5)
    if np.amin(data) < 0 and np.amax(data) > 0:
        # Add a line at 0
        ax.axhline(color='black')
    ax.grid(True)
    if not monthly:
        monthly_ticks(ax)
    plt.title(title, fontsize=18)
    plt.ylabel(units, fontsize=16)
    finished_plot(fig, fig_name=fig_name)
Beispiel #4
0
def make_timeseries_plot_2sided(time,
                                data1,
                                data2,
                                title,
                                units1,
                                units2,
                                monthly=True,
                                fig_name=None,
                                dpi=None):

    fig, ax1 = plt.subplots(figsize=(9, 6))
    ax1.plot_date(time, data1, '-', linewidth=1.5, color='blue')
    ax1.grid(True)
    if not monthly:
        monthly_ticks(ax1)
    ax1.set_ylabel(units1, color='blue', fontsize=16)
    ax1.tick_params(axis='y', labelcolor='blue')
    ax2 = ax1.twinx()
    ax2.plot_date(time, data2, '-', linewidth=1.5, color='red')
    ax2.get_yaxis().get_major_formatter().set_useOffset(False)
    ax2.set_ylabel(units2, color='red', fontsize=16)
    ax2.tick_params(axis='y', labelcolor='red')
    plt.title(title, fontsize=18)
    finished_plot(fig, fig_name=fig_name, dpi=dpi)
Beispiel #5
0
def timeseries_multi_plot(times,
                          datas,
                          labels,
                          colours,
                          title='',
                          units='',
                          monthly=True,
                          fig_name=None,
                          dpi=None,
                          legend_in_centre=False,
                          dates=True):

    # Figure out if time is a list or a single array that applies to all timeseries
    multi_time = isinstance(times, list)
    # Boolean which will tell us whether we need a line at 0
    negative = False
    positive = False
    for data in datas:
        if np.amin(data) < 0:
            negative = True
        if np.amax(data) > 0:
            positive = True
    crosses_zero = negative and positive
    if not dates:
        if multi_time:
            start_time = times[0][0]
            end_time = start_time
            for time in times:
                end_time = max(end_time, time[-1])
        else:
            start_time = times[0]
            end_time = times[-1]

    if legend_in_centre:
        figsize = (8, 6)
    else:
        figsize = (11, 6)
    fig, ax = plt.subplots(figsize=figsize)
    # Plot each line
    for i in range(len(datas)):
        if multi_time:
            time = times[i]
        else:
            time = times
        if dates:
            ax.plot_date(time,
                         datas[i],
                         '-',
                         color=colours[i],
                         label=labels[i],
                         linewidth=1.5)
        else:
            ax.plot(time,
                    datas[i],
                    '-',
                    color=colours[i],
                    label=labels[i],
                    linewidth=1.5)
            ax.set_xlim(start_time, end_time)

    ax.grid(True)
    if crosses_zero:
        # Add a line at 0
        ax.axhline(color='black')
    if not monthly:
        monthly_ticks(ax)
    plt.title(title, fontsize=18)
    plt.ylabel(units, fontsize=16)
    if legend_in_centre:
        # Make legend
        ax.legend(loc='center')
    else:
        # Move plot over to make room for legend
        box = ax.get_position()
        ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
        # Make legend
        ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

    finished_plot(fig, fig_name=fig_name, dpi=dpi)
Beispiel #6
0
def timeseries_multi_plot(times,
                          datas,
                          labels,
                          colours,
                          linestyles=None,
                          alphas=None,
                          title='',
                          units='',
                          monthly=True,
                          fig_name=None,
                          dpi=None,
                          legend_in_centre=False,
                          legend_outside=True,
                          dates=True,
                          thick_last=False,
                          thick_first=False,
                          first_on_top=False,
                          vline=None,
                          return_fig=False,
                          year_ticks=None):

    # Figure out if time is a list or a single array that applies to all timeseries
    multi_time = isinstance(times, list)
    # Booleans which will tell us whether we need a line at 0 or 100
    negative = False
    positive = False
    lt_100 = False
    gt_100 = False
    for data in datas:
        if np.amin(data) < 0:
            negative = True
        if np.amax(data) > 0:
            positive = True
        if np.amin(data) < 100:
            lt_100 = True
        if np.amax(data) > 100:
            gt_100 = True
    crosses_zero = negative and positive
    crosses_100 = lt_100 and gt_100 and units[0] == '%'
    if multi_time:
        start_time = times[0][0]
        end_time = start_time
        for time in times:
            if time[-1] > end_time:
                end_time = time[-1]
    else:
        start_time = times[0]
        end_time = times[-1]

    plot_legend = labels is not None
    if labels is None:
        labels = [None for i in range(len(datas))]

    if linestyles is None:
        linestyles = ['solid' for n in range(len(labels))]
    if alphas is None:
        alphas = [1 for n in range(len(labels))]

    if legend_outside:
        figsize = (11, 6)
    else:
        figsize = (8, 6)
    fig, ax = plt.subplots(figsize=figsize)
    # Plot each line
    for i in range(len(datas)):
        if multi_time:
            time = times[i]
        else:
            time = times
        if (thick_first and i == 0) or (thick_last and i == len(datas) - 1):
            linewidth = 2
        else:
            linewidth = 1
        if first_on_top and i == 0:
            if dates:
                ax.plot_date(time,
                             datas[i],
                             '-',
                             color=colours[i],
                             label=labels[i],
                             linewidth=linewidth,
                             linestyle=linestyles[i],
                             alpha=alphas[i],
                             zorder=len(datas))
            else:
                ax.plot(time,
                        datas[i],
                        '-',
                        color=colours[i],
                        label=labels[i],
                        linewidth=linewidth,
                        linestyle=linestyles[i],
                        alpha=alphas[i],
                        zorder=len(datas))
        else:
            if dates:
                ax.plot_date(time,
                             datas[i],
                             '-',
                             color=colours[i],
                             label=labels[i],
                             linewidth=linewidth,
                             linestyle=linestyles[i],
                             alpha=alphas[i])
            else:
                ax.plot(time,
                        datas[i],
                        '-',
                        color=colours[i],
                        label=labels[i],
                        linewidth=linewidth,
                        linestyle=linestyles[i],
                        alpha=alphas[i])
        ax.set_xlim(start_time, end_time)

    ax.grid(linestyle='dotted')
    if crosses_zero:
        # Add a line at 0
        ax.axhline(color='black', linestyle='dashed')
    if crosses_100:
        # Add a line at 100%
        ax.axhline(100, color='black', linestyle='dashed')
    if vline is not None:
        if dates:
            vline = datetime.date(vline, 1, 1)
        ax.axvline(vline, color='black', linestyle='dashed')
    if not monthly:
        monthly_ticks(ax)
    if year_ticks is not None:
        if dates:
            year_ticks = [datetime.date(y, 1, 1) for y in year_ticks]
        ax.set_xticks(year_ticks)
    plt.title(title, fontsize=18)
    plt.ylabel(units, fontsize=16)
    if not dates:
        plt.xlabel('Years', fontsize=16)
    if plot_legend:
        if legend_outside:
            # Move plot over to make room for legend
            box = ax.get_position()
            ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
            # Make legend
            ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
        elif legend_in_centre:
            ax.legend(loc='center')
        else:
            ax.legend(loc='best')
    if return_fig:
        return fig, ax
    else:
        finished_plot(fig, fig_name=fig_name, dpi=dpi)