Exemplo n.º 1
0
def run(name_fig, path_result, ed_yh, days_to_plot=365, plot_crit=False):
    """Plot individual enduse
    """
    ed_yh_365 = np.copy(ed_yh.reshape(365, 24))

    nr_of_h_to_plot = len(days_to_plot) * 24

    x_data = range(nr_of_h_to_plot)

    y_calculated = []
    for day in days_to_plot:
        for hour in range(24):
            y_calculated.append(ed_yh_365[day][hour])

    # ----------
    # Plot figure
    # ----------
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(16, 8))

    plt.plot(x_data,
             y_calculated,
             label='model',
             linestyle='--',
             linewidth=0.5,
             fillstyle='full',
             color='blue')

    plt.xlim([0, 8760])
    plt.margins(x=0)
    plt.axis('tight')

    # ----------
    # Labelling
    # ----------
    plt.xlabel("hour", fontsize=10)
    plt.ylabel("uk elec use [GW]", fontsize=10)

    plt.legend(frameon=False)

    plt.savefig(os.path.join(path_result, name_fig))

    if plot_crit:
        plt.show()
        plt.close()
    else:
        plt.close()
Exemplo n.º 2
0
def plot_cross_graphs(base_yr, comparison_year, regions,
                      ed_year_fueltype_regs_yh, reg_load_factor_y,
                      fueltype_int, fueltype_str, fig_name, label_points,
                      plotshow):

    result_dict = defaultdict(dict)

    # -------------------------------------------
    # Get base year modelled demand
    # -------------------------------------------
    for year, fuels in ed_year_fueltype_regs_yh.items():

        if year == base_yr:
            for reg_nr, reg_geocode in enumerate(regions):
                result_dict['demand_by'][reg_geocode] = np.sum(
                    fuels[fueltype_int][reg_nr])
                result_dict['peak_h_demand_by'][reg_geocode] = np.max(
                    fuels[fueltype_int][reg_nr])

            # Demand across all regs
            result_dict['demand_by_all_regs'] = np.sum(fuels[fueltype_int])

            # Peak demand
            fuel_all_reg_yh = np.sum(fuels[fueltype_int],
                                     axis=0)  # Sum across all regions
            result_dict['peak_h_demand_by_all_regs'] = np.max(fuel_all_reg_yh)

            # Calculate national load factor
            fuel_all_fueltype_reg_yh = np.sum(
                fuels, axis=1)  # Sum across all regions per fueltype
            load_factor_y = load_factors.calc_lf_y(fuel_all_fueltype_reg_yh)
            result_dict['lf_by_all_regs_av'] = load_factor_y[fueltype_int]

        elif year == comparison_year:
            for reg_nr, reg_geocode in enumerate(regions):
                result_dict['demand_cy'][reg_geocode] = np.sum(
                    fuels[fueltype_int][reg_nr])
                result_dict['peak_h_demand_cy'][reg_geocode] = np.max(
                    fuels[fueltype_int][reg_nr])

            # Demand across all regs
            result_dict['demand_cy_all_regs'] = np.sum(fuels[fueltype_int])

            # Peak demand
            fuel_all_reg_yh = np.sum(fuels[fueltype_int],
                                     axis=0)  # Sum across all regions
            result_dict['peak_h_demand_cy_all_regs'] = np.max(fuel_all_reg_yh)

            # Calculate national load factor
            fuel_all_fueltype_reg_yh = np.sum(
                fuels, axis=1)  # Sum across all regions per fueltype
            load_factor_y = load_factors.calc_lf_y(fuel_all_fueltype_reg_yh)
            result_dict['lf_cy_all_regs_av'] = load_factor_y[fueltype_int]
        else:
            pass

    # Get load factor
    result_dict['lf_cy'] = {}
    result_dict['lf_by'] = {}
    for year, lf_fueltype_regs in reg_load_factor_y.items():
        print("lf_fueltype_regs")
        print(lf_fueltype_regs.shape)
        if year == base_yr:
            for reg_nr, reg_geocode in enumerate(regions):
                result_dict['lf_by'][reg_geocode] = lf_fueltype_regs[
                    fueltype_int][reg_nr]

        elif year == comparison_year:
            for reg_nr, reg_geocode in enumerate(regions):
                result_dict['lf_cy'][reg_geocode] = lf_fueltype_regs[
                    fueltype_int][reg_nr]
        else:
            pass

    labels = []

    #Base year
    x_values, y_values = [], []
    x_values_0_quadrant, y_values_0_quadrant = [], []
    x_values_1_quadrant, y_values_1_quadrant = [], []
    x_values_2_quadrant, y_values_2_quadrant = [], []
    x_values_3_quadrant, y_values_3_quadrant = [], []

    for reg_nr, reg_geocode in enumerate(regions):

        # Change in load factor
        lf_change_p = ((100 / result_dict['lf_by'][reg_geocode]) *
                       result_dict['lf_cy'][reg_geocode]) - 100

        # Change in peak h deman
        demand_peak_h_p = (
            (100 / result_dict['peak_h_demand_by'][reg_geocode]) *
            result_dict['peak_h_demand_cy'][reg_geocode]) - 100

        # Change in total regional demand
        tot_demand_p = ((100 / result_dict['demand_by'][reg_geocode]) *
                        result_dict['demand_cy'][reg_geocode]) - 100

        x_values.append(lf_change_p)
        #y_values.append(tot_demand_p)
        y_values.append(demand_peak_h_p)

        labels.append(reg_geocode)

        if lf_change_p < 0 and tot_demand_p > 0:
            x_values_0_quadrant.append(lf_change_p)
            y_values_0_quadrant.append(tot_demand_p)
        elif lf_change_p > 0 and tot_demand_p > 0:
            x_values_1_quadrant.append(lf_change_p)
            y_values_1_quadrant.append(tot_demand_p)
        elif lf_change_p > 0 and tot_demand_p < 0:
            x_values_2_quadrant.append(lf_change_p)
            y_values_2_quadrant.append(tot_demand_p)
        else:
            x_values_3_quadrant.append(lf_change_p)
            y_values_3_quadrant.append(tot_demand_p)

    # Add average
    national_tot_cy_p = ((100 / result_dict['lf_by_all_regs_av']) *
                         result_dict['lf_cy_all_regs_av']) - 100
    #national_tot_demand_p = ((100 / result_dict['demand_by_all_regs']) * result_dict['demand_cy_all_regs']) - 100
    national_peak_h_p = ((100 / result_dict['peak_h_demand_by_all_regs']) *
                         result_dict['peak_h_demand_cy_all_regs']) - 100

    x_val_national_lf_demand_cy = [national_tot_cy_p]
    #y_val_national_lf_demand_cy = [national_tot_demand_p]
    y_val_national_lf_demand_cy = [national_peak_h_p]

    # -------------------------------------
    # Plot
    # -------------------------------------
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(9,
                                                          8))  #width, height

    ax = fig.add_subplot(1, 1, 1)

    ax.scatter(x_values_0_quadrant,
               y_values_0_quadrant,
               alpha=0.6,
               color='rosybrown',
               s=8)
    ax.scatter(x_values_1_quadrant,
               y_values_1_quadrant,
               alpha=0.6,
               color='firebrick',
               s=8)
    ax.scatter(x_values_2_quadrant,
               y_values_2_quadrant,
               alpha=0.6,
               color='forestgreen',
               s=8)
    ax.scatter(x_values_3_quadrant,
               y_values_3_quadrant,
               alpha=0.6,
               color='darkolivegreen',
               s=8)

    # Add average
    ax.scatter(x_val_national_lf_demand_cy,
               y_val_national_lf_demand_cy,
               alpha=1.0,
               color='black',
               s=20,
               marker="v",
               linewidth=0.5,
               edgecolor='black',
               label='national')

    # --------
    # Axis
    # --------
    ax.set_xlabel("load factor change (%) {}".format(fueltype_str))
    ax.set_ylabel("change in peak h (%) {}".format(fueltype_str))

    plt.tick_params(
        axis='x',  # changes apply to the x-axis
        which='both',  # both major and minor ticks are affected
        bottom='on',  # ticks along the bottom edge are off
        top='off',  # ticks along the top edge are off
        labelbottom='on')  # labels along the bottom edge are off

    # --------
    # Grd
    # --------
    ax.set_axisbelow(True)
    ax.set_xticks([0], minor=True)
    ax.set_yticks([0], minor=True)
    ax.yaxis.grid(True,
                  which='minor',
                  linewidth=0.7,
                  color='grey',
                  linestyle='--')
    ax.xaxis.grid(True,
                  which='minor',
                  linewidth=0.7,
                  color='grey',
                  linestyle='--')

    # Limit
    #plt.ylim(ymin=0)

    # -----------
    # Labelling
    # -----------
    if label_points:
        for pos, txt in enumerate(labels):
            ax.text(x_values[pos],
                    y_values[pos],
                    txt,
                    horizontalalignment="right",
                    verticalalignment="top",
                    fontsize=6)
    # --------
    # Legend
    # --------
    '''legend(
        loc=3,
        prop={
            'family': 'arial',
            'size': 8},
        frameon=False)'''

    # Tight layout
    plt.margins(x=0)
    plt.tight_layout()
    plt.savefig(fig_name)

    if plotshow:
        plt.show()
    plt.close()
def plot_tot_fueltype_y_over_time(scenario_data,
                                  fueltypes,
                                  fueltypes_to_plot,
                                  fig_name,
                                  txt_name,
                                  plotshow=False):
    """Plot total demand over simulation period for every
    scenario for all regions
    """
    results_txt = []

    # Set figure size
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(9, 8))

    ax = fig.add_subplot(1, 1, 1)

    y_scenario = {}

    for scenario_name, scen_data in scenario_data.items():

        # Read out fueltype specific max h load
        data_years = {}
        for year, fueltype_reg_time in scen_data['ed_fueltype_regs_yh'].items(
        ):

            # Sum all regions
            tot_gwh_fueltype_yh = np.sum(fueltype_reg_time, axis=1)

            # Sum all hours
            tot_gwh_fueltype_y = np.sum(tot_gwh_fueltype_yh, axis=1)

            # Convert to TWh
            tot_gwh_fueltype_y = conversions.gwh_to_twh(tot_gwh_fueltype_y)

            data_years[year] = tot_gwh_fueltype_y

        y_scenario[scenario_name] = data_years

    # -----------------
    # Axis
    # -----------------
    base_yr, year_interval = 2020, 10
    first_scen = list(y_scenario.keys())[0]
    end_yr = list(y_scenario[first_scen].keys())

    major_ticks = np.arange(base_yr, end_yr[-1] + year_interval, year_interval)

    plt.xticks(major_ticks, major_ticks)

    # ----------
    # Plot lines
    # ----------
    #color_list_selection_fueltypes = plotting_styles.color_list_selection()
    color_list_selection = plotting_styles.color_list_scenarios()
    linestyles = ['--', '-', ':', "-.",
                  ".-"]  #linestyles = plotting_styles.linestyles()

    cnt_scenario = -1
    for scenario_name, fuel_fueltype_yrs in y_scenario.items():
        cnt_scenario += 1
        color = color_list_selection[cnt_scenario]
        cnt_linestyle = -1

        for fueltype_str, fueltype_nr in fueltypes.items():

            if fueltype_str in fueltypes_to_plot:
                cnt_linestyle += 1
                # Get fuel per fueltpye for every year
                fuel_fueltype = []
                for entry in list(fuel_fueltype_yrs.values()):
                    fuel_fueltype.append(entry[fueltype_nr])

                plt.plot(
                    list(fuel_fueltype_yrs.keys()),  # years
                    fuel_fueltype,  # yearly data per fueltype
                    color=color,
                    linestyle=linestyles[cnt_linestyle],
                    label="{}_{}".format(scenario_name, fueltype_str))

                # ---
                # Calculate difference in demand from 2015 - 2050
                # ---
                tot_2015 = fuel_fueltype[0]
                tot_2050 = fuel_fueltype[-1]
                diff_abs = tot_2050 - tot_2015
                p_diff_2015_2015 = ((100 / tot_2015) * tot_2050) - 100  # Diff

                txt = "fueltype_str: {} ed_tot_by: {} ed_tot_cy: {}, diff_p: {}, diff_abs: {} scenario: {}".format(
                    fueltype_str, round(tot_2015, 1), round(tot_2050, 1),
                    round(p_diff_2015_2015, 1), round(diff_abs, 1),
                    scenario_name)

                results_txt.append(txt)

    # --------------------------
    # Save model results to txt
    # --------------------------
    write_data.write_list_to_txt(txt_name, results_txt)

    # ----
    # Axis
    # ----
    plt.ylim(ymin=0)

    # ------------
    # Plot legend
    # ------------
    ax.legend(ncol=2,
              frameon=False,
              loc='upper center',
              prop={
                  'family': 'arial',
                  'size': 4
              },
              bbox_to_anchor=(0.5, -0.1))

    # ---------
    # Labels
    # ---------
    font_additional_info = plotting_styles.font_info(size=5)

    plt.ylabel("TWh")
    plt.xlabel("year")

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    plt.savefig(fig_name)

    if plotshow:
        plt.show()

    plt.close()
def plot_tot_y_peak_hour(scenario_data,
                         fig_name,
                         fueltype_str_input,
                         plotshow=False):
    """Plot fueltype specific peak h of all regions
    """
    plt.figure(figsize=basic_plot_functions.cm2inch(14, 8))

    # -----------------
    # Axis
    # -----------------
    base_yr, year_interval = 2015, 5
    first_scen = list(scenario_data.keys())[0]
    end_yr = list(scenario_data[first_scen]['ed_peak_h'].keys())[-1]

    major_ticks = np.arange(base_yr, end_yr + year_interval, year_interval)

    plt.xticks(major_ticks, major_ticks)

    # ----------
    # Plot lines
    # ----------
    color_list_selection = plotting_styles.color_list_selection()

    for scenario_name, fuel_fueltype_yrs in scenario_data.items():

        data_container = []
        for year, fuel_fueltypes in fuel_fueltype_yrs['ed_peak_h'].items():
            data_container.append(fuel_fueltypes[fueltype_str_input])

        # Calculate max peak in end year
        peak_gw = round(data_container[-1], 1)
        peak_p = round((100 / data_container[0]) * data_container[-1], 1)

        # Label
        label_scenario = "{} (peak: GW {} p: {})".format(
            scenario_name, peak_gw, peak_p)

        plt.plot(
            list(fuel_fueltype_yrs['ed_peak_h'].keys()),  # years
            list(data_container),  # yearly data
            color=str(color_list_selection.pop()),
            label=label_scenario)  #scenario_name)

    # ----
    # Axis
    # ----
    plt.ylim(ymin=0)

    # ------------
    # Plot legend
    # ------------
    plt.legend(ncol=1,
               loc=3,
               prop={
                   'family': 'arial',
                   'size': 8
               },
               frameon=False)

    # ---------
    # Labels
    # ---------
    plt.ylabel("GWh")
    plt.xlabel("year")
    plt.title("peak_h {} [GW]".format(fueltype_str_input))

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    plt.savefig(fig_name)

    if plotshow:
        plt.show()
        plt.close()
    else:
        plt.close()
def plot_radar_plot_multiple_lines(dh_profiles,
                                   fig_name,
                                   plot_steps,
                                   scenario_names,
                                   plotshow=False,
                                   lf_y_by=None,
                                   lf_y_cy=None,
                                   list_diff_max_h=None):
    """Plot daily load profile on a radar plot

    Arguments
    ---------
    dh_profile : list
        Dh values to plot
    fig_name : str
        Path to save figure

    SOURCE: https://python-graph-gallery.com/390-basic-radar-chart/
    """
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(9, 14))

    # Get maximum demand of all lines
    max_entry = 0
    for line_entries in dh_profiles:
        max_in_line = max(line_entries)
        if max_in_line > max_entry:
            max_entry = max_in_line

    max_demand = round(max_entry, -1) + 10  # Round to nearest 10 plus add 10

    nr_of_plot_steps = int(max_demand / plot_steps) + 1

    axis_plots_inner = []
    axis_plots_innter_position = []

    # --------------------
    # Ciruclar axis
    # --------------------
    for i in range(nr_of_plot_steps):
        axis_plots_inner.append(plot_steps * i)
        axis_plots_innter_position.append(str(plot_steps * i))

    # Minor ticks
    minor_tick_interval = plot_steps / 2
    minor_ticks = []
    for i in range(nr_of_plot_steps * 2):
        minor_ticks.append(minor_tick_interval * i)

    # ----
    # Select color scheme
    # ----

    # Colors with scenarios
    #color_scenarios = plotting_styles.color_list_scenarios() #Color scheme Fig 13

    # Colors for plotting Fig. 13
    #color_scenarios = plotting_styles.color_list_selection_dm() # Color scheme Fig 13

    # Color sheme multiple scenarios
    color_scenarios = ['orange', 'darkred', '#3b2c85',
                       '#85cfcb']  #Lines for Fig 12

    color_lines = ['black'] + color_scenarios
    #color_lines = color_scenarios
    years = ['2015', '2050']
    linewidth_list = [1.0, 0.7]
    linestyle_list = ['-', '--']
    scenario_names.insert(0, "any")

    # Iterate lines
    cnt = -1
    for dh_profile in dh_profiles:

        if cnt >= 0:
            cnt_year = 1
        else:
            cnt_year = 0
        cnt += 1

        # Line properties
        color_line = color_lines[cnt]
        year_line = years[cnt_year]

        data = {'dh_profile': ['testname']}

        # Key: Label outer circle
        for hour in range(24):
            data[hour] = dh_profile[hour]

        # Set data
        df = pd.DataFrame(data)

        # number of variable
        categories = list(df)[1:]
        N = len(categories)

        # We are going to plot the first line of the data frame.
        # But we need to repeat the first value to close the circular graph:
        values = df.loc[0].drop('dh_profile').values.flatten().tolist()
        values += values[:1]

        # What will be the angle of each axis in the plot? (we divide the plot / number of variable)
        angles = [n / float(N) * 2 * math.pi for n in range(N)]
        angles += angles[:1]

        # Initialise the spider plot
        ax = plt.subplot(111, polar=True)

        # Change axis properties
        ax.yaxis.grid(color='grey', linestyle=':', linewidth=0.4)
        ax.xaxis.grid(color='lightgrey', linestyle=':', linewidth=0.4)

        # Change to clockwise cirection
        ax.set_theta_direction(-1)

        # Set first hour on top
        ax.set_theta_zero_location("N")

        # Draw one axe per variable + add labels labels yet (numbers)
        plt.xticks(angles[:-1], categories, color='black', size=8)

        # Draw ylabels (numbers)
        ax.set_rlabel_position(0)

        # Working alternative
        plt.yticks(axis_plots_inner,
                   axis_plots_innter_position,
                   color="black",
                   size=8)

        #ax.set_yticks(axis_plots_inner, minor=False)
        #ax.tick_params(axis='y', pad=35)
        #ax.set_yticks(minor_ticks, minor=True) #Somehow not working

        # Set limit to size
        plt.ylim(0, max_demand)
        plt.ylim(0, nr_of_plot_steps * plot_steps)

        # Smooth lines
        angles_smoothed, values_smoothed = basic_plot_functions.smooth_data(
            angles, values, spider=True)

        # Plot data
        ax.plot(angles_smoothed,
                values_smoothed,
                color=color_line,
                linestyle=linestyle_list[cnt_year],
                linewidth=linewidth_list[cnt_year],
                label="{} {}".format(year_line, scenario_names[cnt]))

        # Radar area
        ax.fill(angles_smoothed, values_smoothed, color_line, alpha=0.05)

    # ------------
    # Write outs
    # ------------
    font_additional_info = plotting_styles.font_info(size=4)

    for cnt, entry in enumerate(list_diff_max_h):
        plt.text(0.15,
                 0 + cnt / 50,
                 entry,
                 fontsize=2,
                 fontdict=font_additional_info,
                 transform=plt.gcf().transFigure)

    # ------------
    # Legend
    # ------------
    plt.legend(ncol=1,
               bbox_to_anchor=(0.3, -0.05),
               prop={'size': 4},
               frameon=False)

    plt.savefig(fig_name)

    if plotshow:
        plt.show()

    plt.close()
def run(path_to_weather_data, folder_path_weater_stations, path_shapefile,
        fig_path):
    """

    """
    # ----------------------------------------------------------
    # Iterate all calculated weather stations
    # ----------------------------------------------------------
    weather_yrs_stations = read_weather_data.get_all_station_per_weather_yr(
        path_to_weather_data)

    weather_station_coordinates = data_loader.read_weather_stations_raw(
        folder_path_weater_stations)
    # ----------------------------------------------------------
    # Station nr and how many times over simulation period
    # ----------------------------------------------------------
    station_counting_over_yr = {}

    for weather_yr, stations in weather_yrs_stations.items():
        for station in stations:
            try:
                station_counting_over_yr[station].append(weather_yr)
            except:
                station_counting_over_yr[station] = [weather_yr]

    # Count number of years
    for station in station_counting_over_yr:
        station_counting_over_yr[station] = len(
            station_counting_over_yr[station])

    # ----------------------------------------------------------
    # Create dataframe
    #{station_id: all_weather_stations[weather_yr][station_id]}
    #df = pd.DataFrame({'src_id': [...], 'longitude': [...], 'latitude': [...]})
    #  ----------------------------------------------------------
    stations_as_dict = {}
    for station_id in station_counting_over_yr:
        try:
            stations_as_dict[station_id] = {
                'longitude':
                weather_station_coordinates[station_id]['longitude'],
                'latitude':
                weather_station_coordinates[station_id]['latitude'],
                'nr_of_weather_yrs': station_counting_over_yr[station_id]
            }
        except:
            print("The station nr {} coul not be found".format(station_id))

    df = pd.DataFrame.from_dict(stations_as_dict, orient='index')

    df['Coordinates'] = list(zip(df.longitude, df.latitude))
    df['Coordinates'] = df['Coordinates'].apply(Point)

    # Load uk shapefile
    uk_shapefile = gpd.read_file(path_shapefile)

    # Assign correct projection
    crs = {'init': 'epsg:27700'}  #27700 == OSGB_1936_British_National_Grid
    uk_gdf = gpd.GeoDataFrame(uk_shapefile, crs=crs)

    # Plot
    ax = uk_gdf.plot(linewidth=0.3,
                     color='whitesmoke',
                     edgecolor='black',
                     figsize=basic_plot_functions.cm2inch(25, 20))

    # plot coordinates
    crs = {'init': 'epsg:4326'}
    gdf = gpd.GeoDataFrame(df, geometry='Coordinates', crs=crs)
    gdf = gdf.to_crs({'init': 'epsg:27700'})

    field_to_plot = 'nr_of_weather_yrs'
    nr_of_intervals = 6

    bin_values = result_mapping.get_reasonable_bin_values_II(
        data_to_plot=list(gdf[field_to_plot]), nr_of_intervals=nr_of_intervals)

    print("BINS " + str(bin_values))
    gdf, cmap_rgb_colors, color_zero, min_value, max_value = fig_p2_weather_val.user_defined_bin_classification(
        gdf, field_to_plot, bin_values=bin_values,
        cmap_sequential='PuRd')  #'Reds')

    # plot with face color attribute
    gdf.plot(ax=ax,
             markersize=20,
             facecolor=gdf['bin_color'],
             edgecolor='black',
             linewidth=0.3)

    legend_handles = result_mapping.add_simple_legend(bin_values,
                                                      cmap_rgb_colors,
                                                      color_zero,
                                                      patch_form='circle')

    plt.legend(handles=legend_handles,
               title=str(field_to_plot),
               prop={'size': 8},
               loc='center left',
               bbox_to_anchor=(1, 0.5),
               frameon=False)
    try:
        os.remove(fig_path)
    except:
        pass
    plt.savefig(fig_path)


#def plot_map_
Exemplo n.º 7
0
def run(results_every_year, lookups, path_plot_fig):
    """Plots

    Plot peak hour per fueltype over time for

    Arguments
    ---------
    tot_fuel_dh_peak : dict
        year, fueltype, peak_dh

    """
    # Set figure size
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(14, 8))
    ax = fig.add_subplot(1, 1, 1)

    nr_y_to_plot = len(results_every_year)

    legend_entries = []

    # Initialise (number of enduses, number of hours to plot)
    y_init = np.zeros((lookups['fueltypes_nr'], nr_y_to_plot))

    for fueltype_str, fueltype in lookups['fueltypes'].items():
        fueltype_int = tech_related.get_fueltype_int(fueltype_str)

        # Legend
        legend_entries.append(fueltype_str)

        # Read out fueltype specific load
        data_over_years = []
        for model_year_object in results_every_year.values():

            # Sum fuel across all regions
            fuel_all_regs = np.sum(model_year_object, axis=1) # (fueltypes, 8760 hours)

            # Get peak day across all enduses for every region
            _, gw_peak_fueltyp_h = enduse_func.get_peak_day_single_fueltype(fuel_all_regs[fueltype_int])

            # Add peak hour
            data_over_years.append(gw_peak_fueltyp_h)

        y_init[fueltype] = data_over_years

    # ----------
    # Plot lines
    # ----------
    #linestyles = plotting_styles.linestyles()

    years = list(results_every_year.keys())
    for fueltype, _ in enumerate(y_init):
        plt.plot(
            years,
            y_init[fueltype],
            linewidth=0.7)

    ax.legend(
        legend_entries,
        prop={
            'family': 'arial',
            'size': 8},
        frameon=False)

    # -
    # Axis
    # -
    base_yr = 2015
    major_interval = 10
    minor_interval = 5

    # Major ticks
    major_ticks = np.arange(base_yr, years[-1] + major_interval, major_interval)
    ax.set_xticks(major_ticks)

    # Minor ticks
    minor_ticks = np.arange(base_yr, years[-1] + minor_interval, minor_interval)
    ax.set_xticks(minor_ticks, minor=True)

    plt.xlim(2015, years[-1])

    # --------
    # Labeling
    # --------
    plt.ylabel("GW")
    plt.xlabel("year")
    plt.title("ED peak hour, y, all enduses and regions")

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    # Save fig
    fig.savefig(path_plot_fig)
    plt.close()
Exemplo n.º 8
0
def compare_results(name_fig,
                    path_result,
                    y_factored_indo,
                    y_calculated_array,
                    title_left,
                    days_to_plot,
                    plot_crit=False):
    """Compare national electrictiy demand data with model results

    Note
    ----
    RMSE fit criteria : Lower values of RMSE indicate better fit
    https://stackoverflow.com/questions/17197492/root-mean-square-error-in-python

    https://matplotlib.org/examples/lines_bars_and_markers/marker_fillstyle_reference.html
    """
    logging.debug("...compare elec results")
    nr_of_h_to_plot = len(days_to_plot) * 24

    x_data = range(nr_of_h_to_plot)

    y_real_indo_factored = []
    y_calculated_list = []
    y_diff_p = []
    y_diff_abs = []

    for day in days_to_plot:
        for hour in range(24):
            y_calculated_list.append(y_calculated_array[day][hour])
            y_real_indo_factored.append(y_factored_indo[day][hour])

            # Calculate absolute differences
            abs_diff = abs(y_factored_indo[day][hour] -
                           y_calculated_array[day][hour])
            y_diff_abs.append(abs_diff)

            # Calculate difference in percent
            if abs_diff == 0:
                p_diff = 0
            else:
                p_diff = (100 / y_factored_indo[day][hour]
                          ) * y_calculated_array[day][hour] - 100

            y_diff_p.append(p_diff)

    # -------------
    # RMSE
    # -------------
    rmse_val_corrected = basic_functions.rmse(np.array(y_real_indo_factored),
                                              np.array(y_calculated_list))

    # ----------
    # Standard deviation
    # ----------
    standard_dev_real_modelled = np.std(y_diff_p)  # Differences in %
    standard_dev_real_modelled_abs = np.std(y_diff_abs)  # Absolute differences

    logging.info("Standard deviation given as percentage: " +
                 str(standard_dev_real_modelled))
    logging.info("Standard deviation given as GW:         " +
                 str(standard_dev_real_modelled_abs))

    # ---------
    # R squared
    # ---------
    slope, intercept, r_value, p_value, std_err = stats.linregress(
        y_real_indo_factored, y_calculated_list)

    # ----------
    # Plot residuals
    # ----------
    #try:
    #    plot_residual_histogram(
    #        y_diff_p, path_result, "residuals_{}".format(name_fig))
    #except:
    #    pass

    # ----------
    # Plot figure
    # ----------
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(22, 8))

    # smooth line
    x_data_smoothed, y_real_indo_factored_smoothed = basic_plot_functions.smooth_data(
        x_data, y_real_indo_factored, num=40000)

    # plot points
    plt.plot(x_data_smoothed,
             y_real_indo_factored_smoothed,
             label='indo_factored',
             linestyle='-',
             linewidth=0.5,
             fillstyle='full',
             color='black')

    # smooth line
    x_data_smoothed, y_calculated_list_smoothed = basic_plot_functions.smooth_data(
        x_data, y_calculated_list, num=40000)

    plt.plot(x_data_smoothed,
             y_calculated_list_smoothed,
             label='model',
             linestyle='--',
             linewidth=0.5,
             fillstyle='full',
             color='blue')

    plt.xlim([0, 8760])
    plt.margins(x=0)
    plt.axis('tight')

    # --------------------------------------
    # Label x axis in dates
    # --------------------------------------
    major_ticks_days, major_ticks_labels = get_date_strings(days_to_plot,
                                                            daystep=1)

    plt.xticks(major_ticks_days, major_ticks_labels)

    # ----------
    # Labelling
    # ----------
    font_additional_info = plotting_styles.font_info(size=4)

    plt.title('RMSE: {} Std_dev_% {} (+-{} GW) R_2: {}'.format(
        round(rmse_val_corrected, 3), round(standard_dev_real_modelled, 3),
        round(standard_dev_real_modelled_abs, 3), round(r_value, 3)),
              fontdict=font_additional_info,
              loc='right')

    plt.title(title_left, loc='left')

    plt.xlabel("hour", fontsize=10)
    plt.ylabel("uk elec use [GW] for {}".format(title_left), fontsize=10)

    plt.legend(frameon=False)

    plt.savefig(os.path.join(path_result, name_fig))

    if plot_crit:
        plt.show()
    plt.close()
Exemplo n.º 9
0
def scenario_over_time(
        scenario_result_container,
        field_name,
        sim_yrs,
        fig_name,
        result_path,
        plot_points,
        crit_smooth_line=True,
        seperate_legend=False
    ):
    """Plot peak over time
    """
    statistics_to_print = []

    fig = plt.figure(figsize=basic_plot_functions.cm2inch(10, 10)) #width, height

    ax = fig.add_subplot(1, 1, 1)

    for cnt_scenario, i in enumerate(scenario_result_container):
        scenario_name = i['scenario_name']
        national_peak = i[field_name]

        # dataframe with national peak (columns= simulation year, row: Realisation) 
        # Calculate quantiles
        #quantile_95 = 0.68 #0.95 #0.68
        #quantile_05 = 0.32 #0.05 #0.32

        try:
            color = colors[scenario_name]
            marker = marker_styles[scenario_name]
        except KeyError:
            color = list(colors.values())[cnt_scenario]

        try:
            marker = marker_styles[scenario_name]
        except KeyError:
            marker = list(marker_styles.values())[cnt_scenario]

        #print("SCENARIO NAME {}  {}".format(scenario_name, color))

        # Calculate average across all weather scenarios
        mean_national_peak = national_peak.mean(axis=0)

        mean_national_peak_sim_yrs = copy.copy(mean_national_peak)

        statistics_to_print.append("scenario: {} values over years: {}".format(scenario_name, mean_national_peak_sim_yrs))

        # Standard deviation over all realisations
        #df_q_05 = national_peak.quantile(quantile_05)
        #df_q_95 = national_peak.quantile(quantile_95)

        # Number of sigma
        nr_of_sigma = 1
        std_dev = national_peak.std(axis=0)
        two_std_line_pos = mean_national_peak + (nr_of_sigma * std_dev)
        two_std_line_neg = mean_national_peak - (nr_of_sigma * std_dev)

        # Maximum and minium values
        max_values = national_peak.max()
        min_values = national_peak.min()
        median_values = national_peak.median()

        statistics_to_print.append("scenario: {} two_sigma_pos: {}".format(scenario_name, two_std_line_pos))
        statistics_to_print.append("scenario: {} two_sigma_neg: {}".format(scenario_name, two_std_line_neg))
        statistics_to_print.append("--------min-------------- {}".format(scenario_name))
        statistics_to_print.append("{}".format(min_values)) #Get minimum value for every simulation year of all realizations
        statistics_to_print.append("--------max-------------- {}".format(scenario_name))
        statistics_to_print.append("{}".format(max_values))
        statistics_to_print.append("--------median_-------------- {}".format(scenario_name))
        statistics_to_print.append("{}".format(median_values))
        # --------------------
        # Try to smooth lines
        # --------------------
        sim_yrs_smoothed = sim_yrs
        if crit_smooth_line:
            try:
                sim_yrs_smoothed, mean_national_peak_smoothed = basic_plot_functions.smooth_data(sim_yrs, mean_national_peak, num=40000)
                #_, df_q_05_smoothed = basic_plot_functions.smooth_data(sim_yrs, df_q_05, num=40000)
                #_, df_q_95_smoothed = basic_plot_functions.smooth_data(sim_yrs, df_q_95, num=40000)
                _, two_std_line_pos_smoothed = basic_plot_functions.smooth_data(sim_yrs, two_std_line_pos, num=40000)
                _, two_std_line_neg_smoothed = basic_plot_functions.smooth_data(sim_yrs, two_std_line_neg, num=40000)
                _, max_values_smoothed = basic_plot_functions.smooth_data(sim_yrs, max_values, num=40000)
                _, min_values_smoothed = basic_plot_functions.smooth_data(sim_yrs, min_values, num=40000)
                mean_national_peak = pd.Series(mean_national_peak_smoothed, sim_yrs_smoothed)
                #df_q_05 = pd.Series(df_q_05_smoothed, sim_yrs_smoothed)
                #df_q_95 = pd.Series(df_q_95_smoothed, sim_yrs_smoothed)
                two_std_line_pos = pd.Series(two_std_line_pos_smoothed, sim_yrs_smoothed)
                two_std_line_neg = pd.Series(two_std_line_neg_smoothed, sim_yrs_smoothed)

                max_values = pd.Series(max_values_smoothed, sim_yrs_smoothed).values
                min_values = pd.Series(min_values_smoothed, sim_yrs_smoothed).values
            except:
                sim_yrs_smoothed = sim_yrs

        # -----------------------
        # Plot lines
        # ------------------------
        plt.plot(
            mean_national_peak,
            label="{} (mean)".format(scenario_name),
            zorder=1,
            color=color)

        # ------------------------
        # Plot markers
        # ------------------------
        if plot_points:
            plt.scatter(
                sim_yrs,
                mean_national_peak_sim_yrs,
                c=color,
                marker=marker,
                edgecolor='black',
                linewidth=0.5,
                zorder=2,
                s=15,
                clip_on=False) #do not clip points on axis

        # ------------------
        # Start with uncertainty one model step later (=> 2020)
        # ------------------
        start_yr_uncertainty = 2020

        if crit_smooth_line:
            #Get position in array of start year uncertainty
            pos_unc_yr = len(np.where(sim_yrs_smoothed < start_yr_uncertainty)[0])
        else:
            pos_unc_yr = 0
            for cnt, year in enumerate(sim_yrs_smoothed):
                if year == start_yr_uncertainty:
                    pos_unc_yr = cnt

        # select based on index which is year
        #df_q_05 = df_q_05.loc[start_yr_uncertainty:]
        #df_q_95 = df_q_95.loc[start_yr_uncertainty:]
        two_std_line_pos = two_std_line_pos.loc[start_yr_uncertainty:]
        two_std_line_neg = two_std_line_neg.loc[start_yr_uncertainty:]
        sim_yrs_smoothed = sim_yrs_smoothed[pos_unc_yr:]

        min_values = min_values[pos_unc_yr:] #min_values.loc[start_yr_uncertainty:]
        max_values = max_values[pos_unc_yr:] #max_values.loc[start_yr_uncertainty:]

        # --------------------------------------
        # Plottin qunatilse and average scenario
        # --------------------------------------
        #df_q_05.plot.line(color=color, linestyle='--', linewidth=0.1, label='_nolegend_')
        #df_q_95.plot.line(color=color, linestyle='--', linewidth=0.1, label='_nolegend_')

        # Plot standard deviation
        #two_std_line_pos.plot.line(color=color, linestyle='--', linewidth=0.1, label='_nolegend_')
        #two_std_line_neg.plot.line(color=color, linestyle='--', linewidth=0.1, label='_nolegend_')

        # plot min and maximum values
        plt.plot(sim_yrs_smoothed, min_values, color=color, linestyle='--', linewidth=0.3, label='_nolegend_')
        plt.plot(sim_yrs_smoothed, max_values, color=color, linestyle='--', linewidth=0.3, label='_nolegend_')

        plt.fill_between(
            sim_yrs_smoothed,
            list(two_std_line_pos),
            list(two_std_line_neg),
            alpha=0.25,
            facecolor=color)

    plt.xlim(2015, 2050)
    plt.ylim(0)

    # --------
    # Different style
    # --------
    ax = plt.gca()
    ax.grid(which='major', color='black', axis='y', linestyle='--')

    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['top'].set_visible(False)

    plt.tick_params(
        axis='y',          # changes apply to the x-axis
        which='both',      # both major and minor ticks are affected
        bottom=False,      # ticks along the bottom edge are off
        top=False,         # ticks along the top edge are off
        left=False,
        right=False,
        labelbottom=False,
        labeltop=False,
        labelleft=True,
        labelright=False) # labels along the bottom edge are off

    # --------
    # Legend
    # --------
    legend = plt.legend(
        ncol=2,
        prop={'size': 10},
        loc='upper center',
        bbox_to_anchor=(0.5, -0.1),
        frameon=False)
    legend.get_title().set_fontsize(8)

    if seperate_legend:
        basic_plot_functions.export_legend(
            legend,
            os.path.join(result_path, "{}__legend.pdf".format(fig_name[:-4])))
        legend.remove()

    # --------
    # Labeling
    # --------
    plt.ylabel("national peak demand (GW)")
    plt.tight_layout()
    plt.savefig(os.path.join(result_path, fig_name))
    plt.close()

    # Write info to txt
    write_data.write_list_to_txt(
        os.path.join(result_path, fig_name).replace(".pdf", ".txt"),
        statistics_to_print)
Exemplo n.º 10
0
def plot_std_dev_vs_contribution(
        scenario_result_container,
        sim_yrs,
        fig_name,
        fueltypes,
        result_path,
        plot_points=False,
        path_shapefile_input=False,
        unit='TWh',
        crit_smooth_line=True,
        seperate_legend=False):
    
    colors = {
        2020: 'dimgrey',
        2050: 'forestgreen'
    }

    #plot_numbers = {
    #    'h_max': {'row': 0, 'col': 0},
    #    'h_min': {'row': 0, 'col': 1},
    #    'l_max': {'row': 1, 'col': 0},
    #    'l_min': {'row': 1, 'col': 1}}
    plot_numbers = {
        'h_max': 1,
        'h_min': 2,
        'l_max': 3,
        'l_min': 4}

    sizes = {
        2020: 2,
        2050: 5}

    scenarios = ['l_max', 'l_min', 'h_max', 'h_min']

    threshold = 5 # percent

    # -----------
    # Plot 4x4 chart with relative differences
    # ------------
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(20, 20)) #width, height
    for scenario in scenarios:
        plot_nr = plot_numbers[scenario]
        plt.subplot(2, 2, plot_nr)

        mean_2020 = scenario_result_container[2020][scenario]['regional_share_national_peak'].mean(axis=0)
        mean_2050 = scenario_result_container[2050][scenario]['regional_share_national_peak'].mean(axis=0)
        
        std_2020 = scenario_result_container[2020][scenario]['regional_share_national_peak'].std(axis=0)
        std_2050 = scenario_result_container[2050][scenario]['regional_share_national_peak'].std(axis=0)
        
        diff_2020_2050_reg_share = ((100 / mean_2020) * mean_2050) - 100
        diff_2020_2050_reg_share_std = ((100 / std_2020) * std_2050) - 100
        
        if scenario not in ['h_min', 'l_min']:
            plt.ylabel("Δ standard deviation [%]", fontsize=10)
        if scenario not in ['h_max', 'h_min']:
            plt.xlabel("Δ mean of total peak [%]", fontsize=10)

        regions = diff_2020_2050_reg_share.index
        for region in regions.values:
            diff_mean = diff_2020_2050_reg_share.loc[region]
            diff_std = diff_2020_2050_reg_share_std.loc[region]

            color_pos = clasify_color(diff_mean, diff_std, threshold=threshold)

            color = colors_quadrates[color_pos]
            plt.scatter(
                diff_mean,
                diff_std,
                #alpha=0.6,
                color=color,
                edgecolor=color,
                linewidth=0.5,
                s=3)

        plt.xlim(-30, 60)
        plt.ylim(-50, 300)
        plt.title("{}".format(scenario), size=10)
        plt.axhline(y=0, linewidth=0.7, color='grey', linestyle='--')   # horizontal line
        plt.axvline(x=0, linewidth=0.7, color='grey', linestyle='--')    # vertical line

        # -----------------
        # Plot spatial map
        # -----------------
        regions = diff_2020_2050_reg_share.index
        relclassified_values = pd.DataFrame()
        relclassified_values['reclassified'] = 0
        relclassified_values['name'] = regions
        relclassified_values = relclassified_values.set_index(('name'))
        relclassified_values['name'] = regions

        for region in regions.values:
            diff_mean = diff_2020_2050_reg_share.loc[region]
            diff_std = diff_2020_2050_reg_share_std.loc[region]
            relclassified_values.loc[region, 'reclassified'] = clasify_color(diff_mean, diff_std, threshold=threshold)

        fig_3_weather_map.plot_4_cross_map(
            cmap_rgb_colors=colors_quadrates,
            reclassified=relclassified_values,
            result_path=os.path.join(result_path, "spatial_final_{}.pdf".format(scenario)),
            threshold=threshold,
            path_shapefile_input=path_shapefile_input,
            seperate_legend=True)

    plt.savefig(os.path.join(result_path, "cross_chart_relative.pdf"))

    # -----------
    # Plot 4x4 chart with absolute
    # ------------
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(20, 20)) #width, height
    for year, scenario_results in scenario_result_container.items():
        for scenario_name, scenario_results in scenario_results.items():

            plot_nr = plot_numbers[scenario_name]
            plt.subplot(2, 2, plot_nr)

            if year in [2020, 2050]:
                #regions = list(regional_share_national_peak.columns)
                #nr_of_regions = regional_share_national_peak.shape[1]
                #nr_of_realisations = regional_share_national_peak.shape[0]

                # Mean over all realisations
                mean = scenario_results['regional_share_national_peak'].mean(axis=0) / 100

                # Standard deviation over all realisations
                std_dev = scenario_results['regional_share_national_peak'].std(axis=0) / 100

                # Convert standard deviation given as % of peak into GW (multiply national peak per region share across the columns)
                abs_gw = pd.DataFrame()
                national_peak_per_run = scenario_results['peak_hour_demand'].sum(axis=1).to_frame()

                for reg_column in scenario_results['regional_share_national_peak'].columns.values:

                    #reg share * national peak
                    column_national_peak = national_peak_per_run.columns[0]
                    abs_gw[reg_column] = (scenario_results['regional_share_national_peak'][reg_column] / 100) * national_peak_per_run[column_national_peak]

                # Absolute values
                std_dev_gw = abs_gw.std(axis=0)
                mean_gw = abs_gw.mean(axis=0)
                plt.ylim(0, np.max(std_dev_gw))
                plt.ylim(0, 0.06)
                plt.xlim(0, 1.5)

                if scenario_name not in ['h_min', 'l_min']:
                    plt.ylabel("standard deviation [GW]", fontsize=10)
                if scenario_name not in ['h_max', 'h_min']:
                    plt.xlabel("mean of total peak [GW]", fontsize=10)

                plt.title(scenario_name, size=10)
                plt.scatter(
                    list(mean_gw), #list(mean),
                    list(std_dev_gw), #list(std_dev),
                    color=colors[year],
                    s=sizes[year]) #,
                    #label="{} {}".format(year, scenario_name))

    plt.savefig(os.path.join(result_path, "cross_chart_absolute.pdf"))
Exemplo n.º 11
0
def plot_lad_comparison(base_yr,
                        comparison_year,
                        regions,
                        ed_year_fueltype_regs_yh,
                        fueltype_int,
                        fueltype_str,
                        fig_name,
                        label_points=False,
                        plotshow=False):
    """Compare energy demand for regions for the base yeard and
    a future year

    Arguments
    ----------
    comparison_year : int
        Year to compare base year values to

    Note
    -----
    SOURCE OF LADS:
        - Data for northern ireland is not included in that, however in BEIS dataset!
    """
    result_dict = defaultdict(dict)

    # -------------------------------------------
    # Get base year modelled demand
    # -------------------------------------------
    for year, fuels in ed_year_fueltype_regs_yh.items():

        if year == base_yr:
            for region_array_nr, reg_geocode in enumerate(regions):
                gw_per_region_modelled = np.sum(
                    fuels[fueltype_int][region_array_nr])
                result_dict['demand_by'][reg_geocode] = gw_per_region_modelled
        elif year == comparison_year:
            for region_array_nr, reg_geocode in enumerate(regions):
                gw_per_region_modelled = np.sum(
                    fuels[fueltype_int][region_array_nr])
                result_dict['demand_cy'][reg_geocode] = gw_per_region_modelled
        else:
            pass

    logging.info("Comparison: modelled: %s real: %s".format(
        sum(result_dict['demand_by'].values()),
        sum(result_dict['demand_cy'].values())))

    # -----------------
    # Sort results according to size
    # -----------------
    sorted_dict_real = sorted(result_dict['demand_by'].items(),
                              key=operator.itemgetter(1))

    # -------------------------------------
    # Plot
    # -------------------------------------
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(9,
                                                          8))  #width, height

    ax = fig.add_subplot(1, 1, 1)

    x_values = np.arange(0, len(sorted_dict_real), 1)

    y_real_elec_demand = []
    y_modelled_elec_demand = []

    labels = []
    for sorted_region in sorted_dict_real:
        geocode_lad = sorted_region[0]

        y_real_elec_demand.append(result_dict['demand_by'][geocode_lad])
        y_modelled_elec_demand.append(result_dict['demand_cy'][geocode_lad])

        logging.debug(
            "validation for LAD region: %s %s diff: %s",
            result_dict['demand_by'][geocode_lad],
            result_dict['demand_cy'][geocode_lad],
            result_dict['demand_cy'][geocode_lad] -
            result_dict['demand_by'][geocode_lad])

        labels.append(geocode_lad)

    # --------
    # Axis
    # --------
    plt.tick_params(
        axis='x',  # changes apply to the x-axis
        which='both',  # both major and minor ticks are affected
        bottom='off',  # ticks along the bottom edge are off
        top='off',  # ticks along the top edge are off
        labelbottom='off')  # labels along the bottom edge are off

    # ----------------------------------------------
    # Plot
    # ----------------------------------------------
    plt.plot(
        x_values,
        y_real_elec_demand,
        linestyle='None',
        marker='o',
        markersize=1.6,  #1.6
        fillstyle='full',
        markerfacecolor='grey',
        markeredgewidth=0.2,
        color='black',
        label='base year: {}'.format(base_yr))

    plt.plot(x_values,
             y_modelled_elec_demand,
             marker='o',
             linestyle='None',
             markersize=1.6,
             markerfacecolor='white',
             fillstyle='none',
             markeredgewidth=0.5,
             markeredgecolor='blue',
             color='black',
             label='current year: {}'.format(comparison_year))

    # Limit
    plt.ylim(ymin=0)

    # -----------
    # Labelling
    # -----------
    if label_points:
        for pos, txt in enumerate(labels):
            ax.text(x_values[pos],
                    y_modelled_elec_demand[pos],
                    txt,
                    horizontalalignment="right",
                    verticalalignment="top",
                    fontsize=3)

    plt.xlabel("UK regions (excluding northern ireland)")
    plt.ylabel("{} [GWh]".format(fueltype_str))

    # --------
    # Legend
    # --------
    plt.legend(prop={'family': 'arial', 'size': 8}, frameon=False)

    # Tight layout
    plt.margins(x=0)
    plt.tight_layout()
    plt.savefig(fig_name)

    if plotshow:
        plt.show()
        plt.close()
    else:
        plt.close()
Exemplo n.º 12
0
def plot_4_cross_map(
        cmap_rgb_colors,
        reclassified,
        result_path,
        path_shapefile_input,
        threshold=None,
        seperate_legend=False
    ):
    """Plot classifed 4 cross map
    """
    # --------------
    # Use Cartopy to plot geometreis with reclassified faceolor
    # --------------
    plt.figure(figsize=basic_plot_functions.cm2inch(10, 10)) #, dpi=150)
    proj = ccrs.OSGB() #'epsg:27700'
    ax = plt.axes(projection=proj)
    ax.outline_patch.set_visible(False)

    # set up a dict to hold geometries keyed by our key
    geoms_by_key = defaultdict(list)

    # for each records, pick out our key's value from the record
    # and store the geometry in the relevant list under geoms_by_key
    for record in shpreader.Reader(path_shapefile_input).records():
        region_name = record.attributes['name']
        geoms_by_key[region_name].append(record.geometry)

    # now we have all the geometries in lists for each value of our key
    # add them to the axis, using the relevant color as facecolor
    for key, geoms in geoms_by_key.items():
        region_reclassified_value = reclassified.loc[key]['reclassified']
        facecolor = cmap_rgb_colors[region_reclassified_value]
        ax.add_geometries(geoms, crs=proj, edgecolor='black', facecolor=facecolor, linewidth=0.1)

    # --------------
    # Create Legend
    # --------------
    legend_handles = [
        mpatches.Patch(color=cmap_rgb_colors[0], label=str("+- threshold {}".format(threshold))),
        mpatches.Patch(color=cmap_rgb_colors[1], label=str("a")),
        mpatches.Patch(color=cmap_rgb_colors[2], label=str("b")),
        mpatches.Patch(color=cmap_rgb_colors[3], label=str("c")),
        mpatches.Patch(color=cmap_rgb_colors[4], label=str("d"))]

    legend = plt.legend(
        handles=legend_handles,
        #title="test",
        prop={'size': 8},
        loc='upper center',
        bbox_to_anchor=(0.5, -0.05),
        frameon=False)

    if seperate_legend:
        basic_plot_functions.export_legend(
            legend,
            os.path.join(result_path, "{}__legend.pdf".format(result_path)))
        legend.remove()

    # Remove coordinates from figure
    ax.set_yticklabels([])
    ax.set_xticklabels([])
    legend.get_title().set_fontsize(8)

    # --------
    # Labeling
    # --------
    plt.tight_layout()
    plt.savefig(os.path.join(result_path))
    plt.close()
def run(data_input, fueltype_str, fig_name):
    """Plot peak demand and total demand over time in same plot
    """
    statistics_to_print = []

    # Select period and fueltype
    fueltype_int = tech_related.get_fueltype_int(fueltype_str)

    # -----------------------------------------------------------
    # Modelled years
    # -----------------------------------------------------------

    # List of selected data for every weather year (which is then converted to array)
    weather_yrs_total_demand = []
    weather_yrs_peak_demand = []

    nr_weather_yrs = list(data_input.keys())
    statistics_to_print.append("_____________________________")
    statistics_to_print.append("Weather years")
    statistics_to_print.append(str(data_input.keys()))

    # Iterate weather years
    for weather_yr, data_weather_yr in data_input.items():

        total_demands = []
        peak_demands = []
        sim_yrs = []
        for sim_yr in data_weather_yr.keys():
            sim_yrs.append(sim_yr)
            data_input_fueltype = data_weather_yr[sim_yr][
                fueltype_int]  # Select fueltype

            # sum total annual demand and convert gwh to twh
            sum_gwh_y = np.sum(data_input_fueltype)
            sum_thw_y = conversions.gwh_to_twh(sum_gwh_y)

            # Get peak
            peak_h = np.max(data_input_fueltype.reshape(8760))

            total_demands.append(sum_thw_y)
            peak_demands.append(peak_h)

        weather_yrs_total_demand.append(total_demands)
        weather_yrs_peak_demand.append(peak_demands)

    columns = sim_yrs

    # Convert to array
    weather_yrs_total_demand = np.array(weather_yrs_total_demand)
    weather_yrs_peak_demand = np.array(weather_yrs_peak_demand)

    # Calculate std per simulation year
    std_total_demand = list(np.std(weather_yrs_total_demand,
                                   axis=0))  # across columns calculate std
    std_peak_demand = list(np.std(weather_yrs_peak_demand,
                                  axis=0))  # across columns calculate std

    # Create dataframe
    if len(nr_weather_yrs) > 2:

        # Create dataframes
        df_total_demand = pd.DataFrame(weather_yrs_total_demand,
                                       columns=columns)
        df_peak = pd.DataFrame(weather_yrs_peak_demand, columns=columns)

        # Calculate quantiles
        quantile_95 = 0.95
        quantile_05 = 0.05

        # Calculate quantiles
        df_total_demand_q_95 = df_total_demand.quantile(quantile_95)
        df_total_demand_q_05 = df_total_demand.quantile(quantile_05)
        df_peak_q_95 = df_peak.quantile(quantile_95)
        df_peak_q_05 = df_peak.quantile(quantile_05)

        # convert to list
        df_total_demand_q_95 = df_total_demand_q_95.tolist()
        df_total_demand_q_05 = df_total_demand_q_05.tolist()
        df_peak_q_95 = df_peak_q_95.tolist()
        df_peak_q_05 = df_peak_q_05.tolist()
        #df_peak = df_peak.T #All indivdiual values
    else:
        #df_total_demand = weather_yrs_total_demand
        #df_peak = weather_yrs_peak_demand
        pass

    # -------------------
    # Base year data (2015)
    # -------------------
    # total demand
    tot_demand_twh_2015 = []
    for sim_yr, data_sim_yr in data_input[2015].items():
        gwh_2015_y = np.sum(data_sim_yr[fueltype_int])
        twh_2015_y = conversions.gwh_to_twh(gwh_2015_y)
        tot_demand_twh_2015.append(twh_2015_y)

    # peak
    df_peak_2015 = []
    for sim_yr, data_sim_yr in data_input[2015].items():
        peak_gwh_2015_y = np.max(data_sim_yr[fueltype_int])
        df_peak_2015.append(peak_gwh_2015_y)

    # ---------------
    # Smoothing lines
    # ---------------
    if len(nr_weather_yrs) > 2:
        try:
            period_h_smoothed, tot_demand_twh_2015_smoothed = basic_plot_functions.smooth_data(
                columns, tot_demand_twh_2015, num=40000)
            period_h_smoothed, df_total_demand_q_95_smoothed = basic_plot_functions.smooth_data(
                list(columns), df_total_demand_q_95, num=40000)
            period_h_smoothed, df_total_demand_q_05_smoothed = basic_plot_functions.smooth_data(
                columns, df_total_demand_q_05, num=40000)
            period_h_smoothed, df_peak_q_95_smoothed = basic_plot_functions.smooth_data(
                list(columns), df_peak_q_95, num=40000)
            period_h_smoothed, df_peak_q_05_smoothed = basic_plot_functions.smooth_data(
                columns, df_peak_q_05, num=40000)
            period_h_smoothed, df_peak_2015_smoothed = basic_plot_functions.smooth_data(
                columns, df_peak_2015, num=40000)
        except:
            period_h_smoothed = columns
            df_total_demand_q_95_smoothed = df_total_demand_q_95
            df_total_demand_q_05_smoothed = df_total_demand_q_05
            df_peak_q_95_smoothed = df_peak_q_95
            df_peak_q_05_smoothed = df_peak_q_05
            tot_demand_twh_2015_smoothed = tot_demand_twh_2015
            df_peak_2015_smoothed = df_peak_2015
    else:
        try:
            period_h_smoothed, tot_demand_twh_2015_smoothed = basic_plot_functions.smooth_data(
                columns, tot_demand_twh_2015, num=40000)
            period_h_smoothed, df_peak_2015_smoothed = basic_plot_functions.smooth_data(
                columns, df_peak_2015, num=40000)
        except:
            period_h_smoothed = columns
            tot_demand_twh_2015_smoothed = tot_demand_twh_2015
            df_peak_2015_smoothed = df_peak_2015

    # --------------
    # Two axis figure
    # --------------
    fig, ax1 = plt.subplots(figsize=basic_plot_functions.cm2inch(15, 10))

    ax2 = ax1.twinx()

    # Axis label
    ax1.set_xlabel('Years')
    ax2.set_ylabel('Peak hour {} demand (GW)'.format(fueltype_str),
                   color='black')
    ax1.set_ylabel('Total {} demand (TWh)'.format(fueltype_str), color='black')

    # Make the y-axis label, ticks and tick labels match the line color.¨
    color_axis1 = 'lightgrey'
    color_axis2 = 'blue'

    ax1.tick_params('y', colors='black')
    ax2.tick_params('y', colors='black')

    if len(nr_weather_yrs) > 2:

        # -----------------
        # Uncertainty range total demand
        # -----------------
        '''ax1.plot(
            period_h_smoothed,
            df_total_demand_q_05_smoothed,
            color='tomato', linestyle='--', linewidth=0.5, label="0.05_total_demand")'''
        '''ax1.plot(
            period_h_smoothed,
            df_total_demand_q_95_smoothed,
            color=color_axis1, linestyle='--', linewidth=0.5, label="0.95_total_demand")

        ax1.fill_between(
            period_h_smoothed, #x
            df_total_demand_q_95_smoothed,  #y1
            df_total_demand_q_05_smoothed,  #y2
            alpha=.25,
            facecolor=color_axis1,
            label="uncertainty band demand")'''

        # -----------------
        # Uncertainty range peaks
        # -----------------
        ##ax2.plot(period_h_smoothed, df_peak_q_05_smoothed, color=color_axis2, linestyle='--', linewidth=0.5, label="0.05_peak")
        ##ax2.plot(period_h_smoothed, df_peak_q_95_smoothed, color=color_axis2, linestyle='--', linewidth=0.5, label="0.95_peak")
        ax2.plot(period_h_smoothed,
                 df_peak_2015_smoothed,
                 color=color_axis2,
                 linestyle="--",
                 linewidth=0.4)

        # Error bar of bar charts
        ax2.errorbar(columns,
                     df_peak_2015,
                     linewidth=0.5,
                     color='black',
                     yerr=std_peak_demand,
                     linestyle="None")

        # Error bar bar plots
        ax1.errorbar(columns,
                     tot_demand_twh_2015,
                     linewidth=0.5,
                     color='black',
                     yerr=std_total_demand,
                     linestyle="None")
        '''ax2.fill_between(
            period_h_smoothed, #x
            df_peak_q_95_smoothed,  #y1
            df_peak_q_05_smoothed,  #y2
            alpha=.25,
            facecolor="blue",
            label="uncertainty band peak")'''

    # Total demand bar plots
    ##ax1.plot(period_h_smoothed, tot_demand_twh_2015_smoothed, color='tomato', linestyle='-', linewidth=2, label="tot_demand_weather_yr_2015")
    ax1.bar(columns,
            tot_demand_twh_2015,
            width=2,
            alpha=1,
            align='center',
            color=color_axis1,
            label="total {} demand".format(fueltype_str))

    statistics_to_print.append("_____________________________")
    statistics_to_print.append("total demand per model year")
    statistics_to_print.append(str(tot_demand_twh_2015))

    # Line of peak demand
    #ax2.plot(columns, df_peak, color=color_axis2, linestyle='--', linewidth=0.5, label="peak_0.95")
    ax2.plot(period_h_smoothed,
             df_peak_2015_smoothed,
             color=color_axis2,
             linestyle='-',
             linewidth=2,
             label="{} peak demand (base weather yr)".format(fueltype_str))

    statistics_to_print.append("_____________________________")
    statistics_to_print.append("peak demand per model year")
    statistics_to_print.append(str(df_peak_2015))

    # Scatter plots of peak demand
    ax2.scatter(columns,
                df_peak_2015,
                marker='o',
                s=20,
                color=color_axis2,
                alpha=1)

    ax1.legend(prop={
        'family': 'arial',
        'size': 10
    },
               loc='upper center',
               bbox_to_anchor=(0.9, -0.1),
               frameon=False,
               shadow=True)

    ax2.legend(prop={
        'family': 'arial',
        'size': 10
    },
               loc='upper center',
               bbox_to_anchor=(0.1, -0.1),
               frameon=False,
               shadow=True)

    # More space at bottom
    #fig.subplots_adjust(bottom=0.4)
    fig.tight_layout()

    plt.savefig(fig_name)
    plt.close()

    # Write info to txt
    write_data.write_list_to_txt(
        os.path.join(fig_name.replace(".pdf", ".txt")), statistics_to_print)
    print("--")
def plt_regions_peak_h(results_every_year, lookups, regions, path_plot_fig,
                       fueltype_str_to_plot):
    """Plot
    Arguments
    ---------
    """
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(14, 8))

    ax = fig.add_subplot(1, 1, 1)

    nr_y_to_plot = len(results_every_year)

    legend_entries = []

    for fueltype_str, fueltype in lookups['fueltypes'].items():

        if fueltype_str != fueltype_str_to_plot:
            pass
        else:
            # Legend
            # Read out fueltype specific load
            data_over_years = {}
            for reg_nr, reg_geocode in enumerate(regions):
                data_over_years[reg_nr] = []

            for model_year_object in results_every_year.values():

                for reg_nr, reg_geocode in enumerate(regions):

                    # Get peak hour value
                    _, peak_fueltyp_h = enduse_func.get_peak_day_single_fueltype(
                        model_year_object[fueltype][reg_nr])

                    # Add peak hour
                    data_over_years[reg_nr].append(peak_fueltyp_h)

            y_init = data_over_years

    # ----------
    # Plot lines
    # ----------
    #linestyles = plotting_styles.linestyles()
    years = list(results_every_year.keys())

    for reg in y_init:
        plt.plot(
            years,
            y_init[reg],
            #linestyle=linestyles[fueltype],
            color='lightblue',
            linewidth=0.2,
        )

    ax.legend(legend_entries,
              prop={
                  'family': 'arial',
                  'size': 8
              },
              frameon=False)

    # -
    # Axis
    # -
    base_yr = 2015
    major_interval = 10
    minor_interval = 5

    # Major ticks
    major_ticks = np.arange(base_yr, years[-1] + major_interval,
                            major_interval)
    ax.set_xticks(major_ticks)

    # Minor ticks
    minor_ticks = np.arange(base_yr, years[-1] + minor_interval,
                            minor_interval)
    ax.set_xticks(minor_ticks, minor=True)

    plt.xlim(2015, years[-1])

    # --------
    # Labeling
    # --------
    plt.ylabel("GW")
    plt.xlabel("year")
    plt.title("ED peak hour, y, all enduses, single regs")

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    # Save fig
    fig.savefig(path_plot_fig)
    plt.close()
Exemplo n.º 15
0
def plot_cross_graphs_scenarios(base_yr, comparison_year, regions,
                                scenario_data, fueltype_int, fueltype_str,
                                fig_name, label_points, plotshow):

    result_dict = defaultdict(dict)

    # -------------------------------------------
    # Get base year modelled demand of any scenario (becasue base year the same in every scenario)
    # -------------------------------------------$
    all_scenarios = list(scenario_data.keys())
    first_scenario = all_scenarios[0]
    ed_year_fueltype_regs_yh = scenario_data[first_scenario][
        'ed_fueltype_regs_yh']

    for year, fuels in ed_year_fueltype_regs_yh.items():

        if year == base_yr:
            for reg_nr, reg_geocode in enumerate(regions):
                result_dict['demand_by'][reg_geocode] = np.sum(
                    fuels[fueltype_int][reg_nr])
                result_dict['peak_h_demand_by'][reg_geocode] = np.max(
                    fuels[fueltype_int][reg_nr])

            # Demand across all regs
            result_dict['demand_by_all_regs'] = np.sum(fuels[fueltype_int])

            # Peak demand
            fuel_all_reg_yh = np.sum(fuels[fueltype_int],
                                     axis=0)  # Sum across all regions
            result_dict['peak_h_demand_by_all_regs'] = np.max(fuel_all_reg_yh)

            # Calculate national load factor
            fuel_all_fueltype_reg_yh = np.sum(
                fuels, axis=1)  # Sum across all regions per fueltype
            load_factor_y = load_factors.calc_lf_y(fuel_all_fueltype_reg_yh)
            result_dict['lf_by_all_regs_av'] = load_factor_y[fueltype_int]

    for scenario, data in scenario_data.items():
        result_dict['demand_cy'][scenario] = {}
        result_dict['peak_h_demand_cy'][scenario] = {}

        for year, fuels in data['ed_fueltype_regs_yh'].items():
            if year == comparison_year:
                for reg_nr, reg_geocode in enumerate(regions):
                    result_dict['demand_cy'][scenario][reg_geocode] = np.sum(
                        fuels[fueltype_int][reg_nr])
                    result_dict['peak_h_demand_cy'][scenario][
                        reg_geocode] = np.max(fuels[fueltype_int][reg_nr])

                # Demand across all regs
                result_dict['demand_cy_all_regs'][scenario] = np.sum(
                    fuels[fueltype_int])

                # Peak demand
                fuel_all_reg_yh = np.sum(fuels[fueltype_int],
                                         axis=0)  # Sum across all regions
                result_dict['peak_h_demand_cy_all_regs'][scenario] = np.max(
                    fuel_all_reg_yh)

                # Calculate national load factor
                fuel_all_fueltype_reg_yh = np.sum(
                    fuels, axis=1)  # Sum across all regions per fueltype
                load_factor_y = load_factors.calc_lf_y(
                    fuel_all_fueltype_reg_yh)
                result_dict['lf_cy_all_regs_av'][scenario] = load_factor_y[
                    fueltype_int]
            else:
                pass

    # Get load factor of base year
    reg_load_factor_y = scenario_data[first_scenario]['reg_load_factor_y']
    for year, lf_fueltype_regs in reg_load_factor_y.items():
        if year == base_yr:
            for reg_nr, reg_geocode in enumerate(regions):
                result_dict['lf_by'][reg_geocode] = lf_fueltype_regs[
                    fueltype_int][reg_nr]

    for scenario, data in scenario_data.items():
        reg_load_factor_y = scenario_data[scenario]['reg_load_factor_y']
        result_dict['lf_cy'][scenario] = {}

        for year, lf_fueltype_regs in reg_load_factor_y.items():
            if year == comparison_year:
                for reg_nr, reg_geocode in enumerate(regions):
                    result_dict['lf_cy'][scenario][
                        reg_geocode] = lf_fueltype_regs[fueltype_int][reg_nr]
            else:
                pass

    # --------------------------
    # Iterate scenario and plot
    # --------------------------
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(18,
                                                          8))  #width, height

    ax = fig.add_subplot(1, 1, 1)

    color_list = plotting_styles.color_list_scenarios()

    marker_list = plotting_styles.marker_list()

    all_x_values = []
    all_y_values = []

    for scenario_nr, scenario in enumerate(all_scenarios):
        labels = []
        x_values, y_values = [], []

        for reg_nr, reg_geocode in enumerate(regions):

            # Change in load factor
            lf_change_p = ((100 / result_dict['lf_by'][reg_geocode]) *
                           result_dict['lf_cy'][scenario][reg_geocode]) - 100

            # Change in peak h deman
            demand_peak_h_p = (
                (100 / result_dict['peak_h_demand_by'][reg_geocode]) *
                result_dict['peak_h_demand_cy'][scenario][reg_geocode]) - 100

            # Change in total regional demand
            tot_demand_p = (
                (100 / result_dict['demand_by'][reg_geocode]) *
                result_dict['demand_cy'][scenario][reg_geocode]) - 100

            x_values.append(lf_change_p)
            #y_values.append(tot_demand_p)
            y_values.append(demand_peak_h_p)

            #print("ADDING {} {} {}".format(reg_nr, round(lf_change_p, 3), round(demand_peak_h_p, 3)))

            labels.append(reg_geocode)

        # Add average
        national_tot_cy_p = ((100 / result_dict['lf_by_all_regs_av']) *
                             result_dict['lf_cy_all_regs_av'][scenario]) - 100
        #national_tot_demand_p = ((100 / result_dict['demand_by_all_regs']) * result_dict['demand_cy_all_regs'][scenario]) - 100
        national_peak_h_p = (
            (100 / result_dict['peak_h_demand_by_all_regs']) *
            result_dict['peak_h_demand_cy_all_regs'][scenario]) - 100

        x_val_national_lf_demand_cy = [national_tot_cy_p]
        #y_val_national_lf_demand_cy = [national_tot_demand_p]
        y_val_national_lf_demand_cy = [national_peak_h_p]

        all_x_values += x_values
        all_y_values += y_values
        # -------------------------------------
        # Plot
        # -------------------------------------
        color = color_list[scenario_nr]
        marker = marker_list[scenario_nr]

        alpha_value = 0.6
        marker_size = 7
        ax.scatter(
            x_values,
            y_values,
            alpha=alpha_value,
            color=color,
            #marker=marker,
            edgecolor=color,
            linewidth=0.5,
            s=marker_size,
            label=scenario)

        # Add average
        ax.scatter(x_val_national_lf_demand_cy,
                   y_val_national_lf_demand_cy,
                   alpha=1.0,
                   color=color,
                   s=20,
                   marker="v",
                   linewidth=0.5,
                   edgecolor='black',
                   label='national')

    # --------
    # Axis
    # --------
    ax.set_xlabel("load factor change (%) {}".format(fueltype_str))
    ax.set_ylabel("change in peak h (%) {}".format(fueltype_str))

    plt.tick_params(
        axis='x',  # changes apply to the x-axis
        which='both',  # both major and minor ticks are affected
        bottom='on',  # ticks along the bottom edge are off
        top='off',  # ticks along the top edge are off
        labelbottom='on')  # labels along the bottom edge are off

    # --------
    # Grd
    # --------
    ax.set_axisbelow(True)
    #ax.grid(True)
    #ax.set_xticks(minor=False)
    ax.set_xticks([0], minor=True)
    #ax.set_yticks(minor=False)
    ax.set_yticks([0], minor=True)
    #ax.yaxis.grid(True, which='major')
    ax.yaxis.grid(True,
                  which='minor',
                  linewidth=0.7,
                  color='grey',
                  linestyle='--')
    #ax.xaxis.grid(True, which='major')
    ax.xaxis.grid(True,
                  which='minor',
                  linewidth=0.7,
                  color='grey',
                  linestyle='--')

    # Limit
    #plt.ylim(ymin=0)

    # -----------
    # Labelling
    # -----------
    if label_points:
        for pos, txt in enumerate(labels):
            ax.text(x_values[pos],
                    y_values[pos],
                    txt,
                    horizontalalignment="right",
                    verticalalignment="top",
                    fontsize=6)

    # -------
    # Title information
    # -------
    max_lf = round(max(all_x_values), 2)
    min_lf = round(min(all_x_values), 2)
    min_peak_h = round(min(all_y_values), 2)
    max_peak_h = round(max(all_y_values), 2)

    font_additional_info = plotting_styles.font_info(size=4)

    plt.title("max_peak_h: {} min_peak_h: {}, min_lf: {} max_lf: {}".format(
        max_peak_h, min_peak_h, min_lf, max_lf),
              fontdict=font_additional_info)

    # --------
    # Legend
    # --------
    plt.legend(loc='best',
               ncol=1,
               prop={
                   'family': 'arial',
                   'size': 3
               },
               frameon=False)

    # Tight layout
    plt.margins(x=0)
    plt.tight_layout()
    plt.savefig(fig_name)

    if plotshow:
        plt.show()
    plt.close()
Exemplo n.º 16
0
def scenario_over_time(scenario_result_container,
                       sim_yrs,
                       fig_name,
                       result_path,
                       plot_points,
                       crit_smooth_line=True,
                       seperate_legend=False):
    """Plot peak over time
    """
    statistics_to_print = []

    fig = plt.figure(figsize=basic_plot_functions.cm2inch(10,
                                                          10))  #width, height

    ax = fig.add_subplot(1, 1, 1)

    for cnt_scenario, i in enumerate(scenario_result_container):
        scenario_name = i['scenario_name']
        national_peak = i['national_peak']

        # dataframe with national peak (columns= simulation year, row: Realisation)
        # Calculate quantiles
        quantile_95 = 0.95
        quantile_05 = 0.05

        try:
            color = colors[scenario_name]
            marker = marker_styles[scenario_name]
        except KeyError:
            color = list(colors.values())[cnt_scenario]

        try:
            marker = marker_styles[scenario_name]
        except KeyError:
            marker = list(marker_styles.values())[cnt_scenario]

        print("SCENARIO NAME {}  {}".format(scenario_name, color))

        # Calculate average across all weather scenarios
        mean_national_peak = national_peak.mean(axis=0)
        mean_national_peak_sim_yrs = copy.copy(mean_national_peak)

        statistics_to_print.append("scenario: {} values over years: {}".format(
            scenario_name, mean_national_peak_sim_yrs))

        # Standard deviation over all realisations
        df_q_05 = national_peak.quantile(quantile_05)
        df_q_95 = national_peak.quantile(quantile_95)

        statistics_to_print.append("scenario: {} df_q_05: {}".format(
            scenario_name, df_q_05))
        statistics_to_print.append("scenario: {} df_q_95: {}".format(
            scenario_name, df_q_95))

        # --------------------
        # Try to smooth lines
        # --------------------
        sim_yrs_smoothed = sim_yrs
        if crit_smooth_line:
            try:
                sim_yrs_smoothed, mean_national_peak_smoothed = basic_plot_functions.smooth_data(
                    sim_yrs, mean_national_peak, num=40000)
                _, df_q_05_smoothed = basic_plot_functions.smooth_data(
                    sim_yrs, df_q_05, num=40000)
                _, df_q_95_smoothed = basic_plot_functions.smooth_data(
                    sim_yrs, df_q_95, num=40000)

                mean_national_peak = pd.Series(mean_national_peak_smoothed,
                                               sim_yrs_smoothed)
                df_q_05 = pd.Series(df_q_05_smoothed, sim_yrs_smoothed)
                df_q_95 = pd.Series(df_q_95_smoothed, sim_yrs_smoothed)
            except:
                sim_yrs_smoothed = sim_yrs

        # -----------------------
        # Plot lines
        # ------------------------
        plt.plot(mean_national_peak,
                 label="{} (mean)".format(scenario_name),
                 color=color)

        # ------------------------
        # Plot markers
        # ------------------------
        if plot_points:
            plt.scatter(sim_yrs,
                        mean_national_peak_sim_yrs,
                        c=color,
                        marker=marker,
                        edgecolor='black',
                        linewidth=0.5,
                        s=15,
                        clip_on=False)  #do not clip points on axis

        # Plottin qunatilse and average scenario
        df_q_05.plot.line(color=color,
                          linestyle='--',
                          linewidth=0.1,
                          label='_nolegend_')  #, label="0.05")
        df_q_95.plot.line(color=color,
                          linestyle='--',
                          linewidth=0.1,
                          label='_nolegend_')  #, label="0.05")

        plt.fill_between(
            sim_yrs_smoothed,
            list(df_q_95),  #y1
            list(df_q_05),  #y2
            alpha=0.25,
            facecolor=color,
        )

    plt.xlim(2015, 2050)
    plt.ylim(0)

    # --------
    # Different style
    # --------
    ax = plt.gca()
    ax.grid(which='major', color='black', axis='y', linestyle='--')

    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['top'].set_visible(False)

    plt.tick_params(
        axis='y',  # changes apply to the x-axis
        which='both',  # both major and minor ticks are affected
        bottom=False,  # ticks along the bottom edge are off
        top=False,  # ticks along the top edge are off
        left=False,
        right=False,
        labelbottom=False,
        labeltop=False,
        labelleft=True,
        labelright=False)  # labels along the bottom edge are off

    # --------
    # Legend
    # --------
    legend = plt.legend(
        #title="tt",
        ncol=2,
        prop={'size': 10},
        loc='upper center',
        bbox_to_anchor=(0.5, -0.1),
        frameon=False)
    legend.get_title().set_fontsize(8)

    if seperate_legend:
        basic_plot_functions.export_legend(
            legend,
            os.path.join(result_path, "{}__{}__legend.pdf".format(fig_name)))
        legend.remove()

    # --------
    # Labeling
    # --------
    plt.ylabel("national peak demand (GW)")
    #plt.xlabel("year")
    #plt.title("Title")

    plt.tight_layout()

    plt.savefig(os.path.join(result_path, fig_name))
    plt.close()

    # Write info to txt
    write_data.write_list_to_txt(
        os.path.join(result_path, fig_name).replace(".pdf", ".txt"),
        statistics_to_print)
Exemplo n.º 17
0
def spatial_validation_multiple(reg_coord,
                                input_data,
                                regions,
                                fueltype_str,
                                fig_name,
                                label_points=False,
                                plotshow=False):
    """Compare gas/elec demand for LADs

    Arguments
    ----------
    lad_infos_shapefile : dict
        Infos of shapefile (dbf / csv)
    ed_fueltype_regs_yh : object
        Regional fuel Given as GWh (?)
    subnational_real : dict
        for electricity: Sub-national electrcity demand given as GWh

    Note
    -----
    SOURCE OF LADS:
        - Data for northern ireland is not included in that, however in BEIS dataset!
    """
    logging.debug("... Validation of spatial disaggregation")

    color_list = ['firebrick', 'darkseagreen']
    label_list = ['domestic', 'non_domestic']

    # -------------------------------------
    # Plot
    # -------------------------------------
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(9,
                                                          8))  #width, height

    ax = fig.add_subplot(1, 1, 1)

    cnt_color = 0
    for i in input_data:
        subnational_modelled = i[0]
        subnational_real = i[1]

        result_dict = {}
        result_dict['real_demand'] = {}
        result_dict['modelled_demand'] = {}

        # -------------------------------------------
        # Match ECUK sub-regional demand with geocode
        # -------------------------------------------
        for region in regions:
            for reg_geocode in reg_coord:
                if reg_geocode == region:
                    try:
                        # Test wheter data is provided for LAD or owtherwise ignore
                        if subnational_real[reg_geocode] == 0:
                            pass
                        else:
                            # --Sub Regional Electricity demand (as GWh)
                            result_dict['real_demand'][
                                reg_geocode] = subnational_real[reg_geocode]
                            result_dict['modelled_demand'][
                                reg_geocode] = subnational_modelled[
                                    reg_geocode]
                    except KeyError:
                        logging.debug(
                            "Sub-national spatial validation: No fuel for region %s",
                            reg_geocode)

        # --------------------
        # Calculate statistics
        # --------------------
        diff_real_modelled_p = []
        diff_real_modelled_abs = []

        y_real_demand = []
        y_modelled_demand = []

        # -----------------
        # Sort results according to size
        # -----------------
        sorted_dict_real = sorted(result_dict['real_demand'].items(),
                                  key=operator.itemgetter(1))

        #for reg_geocode in regions:
        for reg_geocode, _ in sorted_dict_real:
            # Test if real and modelled data are both available
            try:
                real = result_dict['real_demand'][reg_geocode]
                modelled = result_dict['modelled_demand'][reg_geocode]

                diff_real_modelled_p.append(
                    abs(100 -
                        ((100 / real) * modelled)))  # Average abs deviation
                diff_real_modelled_abs.append(real - modelled)

                y_real_demand.append(real)
                y_modelled_demand.append(modelled)
            except KeyError:
                pass

        # Calculate the average deviation between reald and modelled
        av_deviation_real_modelled = np.average(
            diff_real_modelled_p)  # average deviation
        median_absolute_deviation = np.median(
            diff_real_modelled_p)  # median deviation

        # Calculate standard deviation
        std_dev_p = np.std(diff_real_modelled_p)  # Given as percent
        std_dev_abs = np.std(diff_real_modelled_abs)  # Given as energy unit

        x_values = np.arange(0, len(y_real_demand), 1)

        labels = []
        for sorted_region in sorted_dict_real:
            if sorted_region in y_real_demand:
                geocode_lad = sorted_region[0]

                logging.debug(
                    "validation %s LAD %s: real: %s modelled: %s  modelled percentage: %s (%sp diff)",
                    fueltype_str, geocode_lad,
                    round(result_dict['real_demand'][geocode_lad], 4),
                    round(result_dict['modelled_demand'][geocode_lad], 4),
                    round(
                        100 / result_dict['real_demand'][geocode_lad] *
                        result_dict['modelled_demand'][geocode_lad], 4),
                    round(
                        100 - (100 / result_dict['real_demand'][geocode_lad] *
                               result_dict['modelled_demand'][geocode_lad]),
                        4))

                labels.append(geocode_lad)

        # Calculate r_squared
        _slope, _intercept, r_value, _p_value, _std_err = stats.linregress(
            y_real_demand, y_modelled_demand)

        # --------
        # Axis
        # --------
        plt.tick_params(
            axis='x',  # changes apply to the x-axis
            which='both',  # both major and minor ticks are affected
            bottom='off',  # ticks along the bottom edge are off
            top='off',  # ticks along the top edge are off
            labelbottom='off')  # labels along the bottom edge are off

        # ----------------------------------------------
        # Plot
        # ----------------------------------------------
        plt.plot(x_values,
                 y_real_demand,
                 linestyle='None',
                 marker='o',
                 alpha=0.7,
                 markersize=1.6,
                 fillstyle='full',
                 markerfacecolor='grey',
                 markeredgewidth=0.2,
                 color=color_list[cnt_color],
                 markeredgecolor=color_list[cnt_color],
                 label='actual')

        plt.plot(x_values,
                 y_modelled_demand,
                 marker='o',
                 linestyle='None',
                 markersize=1.6,
                 alpha=0.7,
                 markerfacecolor='white',
                 fillstyle='none',
                 markeredgewidth=0.5,
                 markeredgecolor=color_list[cnt_color],
                 color=color_list[cnt_color],
                 label='model' + label_list[cnt_color])

        # -----------
        # Labelling
        # -----------
        if label_points:
            for pos, txt in enumerate(labels):
                ax.text(x_values[pos],
                        y_modelled_demand[pos],
                        txt,
                        horizontalalignment="right",
                        verticalalignment="top",
                        fontsize=1)

        font_additional_info = plotting_styles.font_info(
            size=3, color=color_list[cnt_color])

        title_info = (
            'R_2: {}, std_%: {} (GWh {}), av_diff_%: {} median_abs_dev: {}'.
            format(round(r_value, 2), round(std_dev_p, 2),
                   round(std_dev_abs, 2), round(av_deviation_real_modelled, 2),
                   round(median_absolute_deviation, 2)))

        plt.text(0.4,
                 0.9 - cnt_color / 10,
                 title_info,
                 ha='center',
                 va='center',
                 transform=ax.transAxes,
                 fontdict=font_additional_info)

        cnt_color = +1

    plt.xlabel("UK regions (excluding northern ireland)")
    plt.ylabel("{} [GWh]".format(fueltype_str))
    plt.ylim(ymin=0)

    # --------
    # Legend
    # --------
    plt.legend(prop={'family': 'arial', 'size': 6}, frameon=False)

    # Tight layout
    plt.margins(x=0)
    plt.tight_layout()

    plt.savefig(fig_name)

    if plotshow:
        plt.show()
    else:
        plt.close()
def run(years_simulated,
        results_enduse_every_year,
        enduses,
        color_list,
        fig_name,
        plot_legend=True):
    """Plots stacked energy demand

    Arguments
    ----------
    years_simulated : list
        Simulated years
    results_enduse_every_year : dict
        Results [year][enduse][fueltype_array_position]

    enduses :
    fig_name : str
        Figure name

    Note
    ----
        -   Sum across all fueltypes
        -   Not possible to plot single year

    https://matplotlib.org/examples/pylab_examples/stackplot_demo.html
    """
    print("...plot stacked enduses")
    nr_of_modelled_years = len(years_simulated)

    x_data = np.array(years_simulated)

    y_value_arrays = []
    legend_entries = []

    for enduse in enduses:
        legend_entries.append(enduse)

        y_values_enduse_yrs = np.zeros((nr_of_modelled_years))

        for year_array_nr, model_year in enumerate(
                results_enduse_every_year.keys()):

            # Sum across all fueltypes
            tot_across_fueltypes = np.sum(
                results_enduse_every_year[model_year][enduse])

            # Conversion: Convert GWh per years to GW
            yearly_sum_twh = conversions.gwh_to_twh(tot_across_fueltypes)

            logging.debug("... model_year {} enduse {}  twh {}".format(
                model_year, enduse, np.sum(yearly_sum_twh)))

            if yearly_sum_twh < 0:
                raise Exception("no minus values allowed {}  {}  {}".format(
                    enduse, yearly_sum_twh, model_year))

            y_values_enduse_yrs[year_array_nr] = yearly_sum_twh

        # Add array with values for every year to list
        y_value_arrays.append(y_values_enduse_yrs)

    # Try smoothing line
    try:
        x_data_smoothed, y_value_arrays_smoothed = basic_plot_functions.smooth_data(
            x_data, y_value_arrays, num=40000)
    except:
        x_data_smoothed = x_data
        y_value_arrays_smoothed = y_value_arrays

    # Convert to stacked
    y_stacked = np.row_stack((y_value_arrays_smoothed))

    # Set figure size
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(8, 8))

    ax = fig.add_subplot(1, 1, 1)

    # ----------
    # Stack plot
    # ----------
    color_stackplots = color_list[:len(enduses)]

    ax.stackplot(x_data_smoothed,
                 y_stacked,
                 alpha=0.8,
                 colors=color_stackplots)

    if plot_legend:
        plt.legend(legend_entries,
                   prop={
                       'family': 'arial',
                       'size': 5
                   },
                   ncol=2,
                   loc='upper center',
                   bbox_to_anchor=(0.5, -0.1),
                   frameon=False,
                   shadow=True)

    # -------
    # Axis
    # -------
    year_interval = 10
    major_ticks = np.arange(years_simulated[0],
                            years_simulated[-1] + year_interval, year_interval)

    plt.xticks(major_ticks, major_ticks)

    plt.ylim(ymax=500)
    #yticks = [100, 200, 300, 400, 500]
    #plt.yticks(yticks, yticks)
    # -------
    # Labels
    # -------
    plt.ylabel("TWh", fontsize=10)
    plt.xlabel("Year", fontsize=10)
    #plt.title("ED whole UK", fontsize=10)

    # Tight layout
    fig.tight_layout()

    plt.margins(x=0)
    plt.savefig(fig_name)
    plt.close()
Exemplo n.º 19
0
def compare_peak(name_fig, path_result, real_elec_2015_peak, modelled_peak_dh,
                 peak_day):
    """Compare peak electricity day with calculated peak energy demand

    Arguments
    ---------
    name_fig : str
        Name of figure
    local_paths : dict
        Paths
    real_elec_2015_peak : array
        Real data of peak day
    modelled_peak_dh : array
        Modelled peak day
    """
    logging.debug("...compare elec peak results")

    real_elec_peak = np.copy(real_elec_2015_peak)
    # -------------------------------
    # Compare values
    # -------------------------------
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(8, 8))

    # smooth line
    x_smoothed, y_modelled_peak_dh_smoothed = basic_plot_functions.smooth_data(
        range(24), modelled_peak_dh, num=500)

    plt.plot(x_smoothed,
             y_modelled_peak_dh_smoothed,
             color='blue',
             linestyle='--',
             linewidth=0.5,
             label='model')

    x_smoothed, real_elec_peak_smoothed = basic_plot_functions.smooth_data(
        range(24), real_elec_peak, num=500)

    plt.plot(x_smoothed,
             real_elec_peak_smoothed,
             color='black',
             linestyle='-',
             linewidth=0.5,
             label='validation')

    #raise Exception
    # Calculate hourly differences in %
    diff_p_h = np.round((100 / real_elec_peak) * modelled_peak_dh, 1)

    # Calculate maximum difference
    max_h_real = np.max(real_elec_peak)
    max_h_modelled = np.max(modelled_peak_dh)

    max_h_diff = round((100 / max_h_real) * max_h_modelled, 2)
    max_h_diff_gwh = round((abs(100 - max_h_diff) / 100) * max_h_real, 2)

    # Y-axis ticks
    plt.xlim(0, 23)
    plt.yticks(range(0, 60, 10))

    # because position 0 in list is 01:00, the labelling starts with 1
    plt.xticks([0, 5, 11, 17, 23], [1, 6, 12, 18, 24])  #ticks, labels

    plt.legend(frameon=False)

    # Labelling
    date_yearday = date_prop.yearday_to_date(2015, peak_day)
    plt.title("peak comparison {}".format(date_yearday))

    plt.xlabel("h (max {} ({} GWH)".format(max_h_diff, max_h_diff_gwh))
    plt.ylabel("uk electrictiy use [GW]")

    plt.text(
        5,  #position
        5,  #position
        diff_p_h,
        fontdict={
            'family': 'arial',
            'color': 'black',
            'weight': 'normal',
            'size': 4
        })

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    # Save fig
    plt.savefig(os.path.join(path_result, name_fig))
    plt.close()
def run_fig_spatial_distribution_of_peak(
        scenarios,
        path_to_folder_with_scenarios,
        path_shapefile,
        sim_yrs,
        field_to_plot,
        unit,
        fig_path,
        fueltype_str='electricity'
    ):
    """
    """
    weather_yrs = []
    calculated_yrs_paths = []
    fueltype_int = tech_related.get_fueltype_int(fueltype_str)

    for scenario in scenarios:
        path_scenario = os.path.join(path_to_folder_with_scenarios, scenario)
        all_result_folders = os.listdir(path_scenario)

        for result_folder in all_result_folders:
            try:
                split_path_name = result_folder.split("__")
                weather_yr = int(split_path_name[0])
                weather_yrs.append(weather_yr)
                tupyle_yr_path = (weather_yr, os.path.join(path_scenario))
                calculated_yrs_paths.append(tupyle_yr_path)
            except ValueError:
                pass

    for simulation_yr in sim_yrs:
        container = {}
        container['abs_demand_in_peak_h_pp'] = {}
        container['abs_demand_in_peak_h'] = {}
        container['p_demand_in_peak_h'] = {}

        for weather_yr, path_data_ed in calculated_yrs_paths:
            print("... prepare data {} {}".format(weather_yr, path_data_ed))

            path_to_weather_yr = os.path.join(path_data_ed, "{}__{}".format(weather_yr, 'all_stations'))

            data = {}
            data['lookups'] = lookup_tables.basic_lookups()
            data['enduses'], data['assumptions'], regions = data_loader.load_ini_param(os.path.join(path_data_ed))
            data['assumptions']['seasons'] = date_prop.get_season(year_to_model=2015)
            data['assumptions']['model_yeardays_daytype'], data['assumptions']['yeardays_month'], data['assumptions']['yeardays_month_days'] = date_prop.get_yeardays_daytype(year_to_model=2015)

            # Population 
            population_data = read_data.read_scenaric_population_data(os.path.join(path_data_ed, 'model_run_pop'))

            results_container = read_data.read_in_results(
                os.path.join(path_to_weather_yr, 'model_run_results_txt'),
                data['assumptions']['seasons'],
                data['assumptions']['model_yeardays_daytype'])

            # ---------------------------------------------------
            # Calculate hour with national peak demand
            # This may be different depending on the weather yr
            # ---------------------------------------------------
            ele_regions_8760 = results_container['ed_fueltype_regs_yh'][simulation_yr][fueltype_int]
            sum_all_regs_fueltype_8760 = np.sum(ele_regions_8760, axis=0) # Sum for every hour

            max_day = int(basic_functions.round_down((np.argmax(sum_all_regs_fueltype_8760) / 24), 1))
            max_h = np.argmax(sum_all_regs_fueltype_8760)
            max_demand = np.max(sum_all_regs_fueltype_8760)

            # Calculate the national peak demand in GW
            national_peak_GW = np.max(sum_all_regs_fueltype_8760)

            # ------------------------------------------------------
            # Calculate the contribution of the regional peak demand
            # ------------------------------------------------------
            demand_in_peak_h = ele_regions_8760[:, max_h]

            if unit == 'GW':
                container['abs_demand_in_peak_h'][weather_yr] = demand_in_peak_h
            elif unit == 'kW':
                container['abs_demand_in_peak_h'][weather_yr] = demand_in_peak_h * 1000000 # Convert to KWh
            else:
                # Use GW as default
                container['abs_demand_in_peak_h'][weather_yr] = demand_in_peak_h

            container['abs_demand_in_peak_h_pp'][weather_yr] = demand_in_peak_h / population_data[simulation_yr]
    
            # Relative fraction of regional demand in relation to peak
            container['p_demand_in_peak_h'][weather_yr] = (demand_in_peak_h / national_peak_GW ) * 100 # given as percent

            print("=================================")
            print("{}  {}  {}  {}".format(
                simulation_yr,
                weather_yr,
                np.sum(ele_regions_8760),
                national_peak_GW))

        # --------------
        # Create dataframe with all weather yrs calculatiosn for every region
            
        #               region1, region2, region3
        # weather yr1
        # weather yr2
        # --------------
        # Convert regional data to dataframe
        abs_demand_in_peak_h_pp = np.array(list(container['abs_demand_in_peak_h_pp'].values()))
        abs_demand_peak_h = np.array(list(container['abs_demand_in_peak_h'].values()))
        p_demand_peak_h = np.array(list(container['p_demand_in_peak_h'].values()))

        # Absolute demand
        df_abs_peak_demand = pd.DataFrame(
            abs_demand_peak_h,
            columns=regions,
            index=list(container['abs_demand_in_peak_h'].keys()))

        # Relative demand
        df_p_peak_demand = pd.DataFrame(
            p_demand_peak_h,
            columns=regions,
            index=list(container['p_demand_in_peak_h'].keys()))
        
        df_abs_demand_in_peak_h_pp = pd.DataFrame(
            abs_demand_in_peak_h_pp,
            columns=regions,
            index=list(container['abs_demand_in_peak_h_pp'].keys()))

        # Absolute peak value - mean
        max_peak_h_across_weather_yrs = df_abs_peak_demand.max()
        average_across_weather_yrs = df_abs_peak_demand.mean()
        diff_peak_h_minus_mean = max_peak_h_across_weather_yrs - average_across_weather_yrs

        for index, row in df_p_peak_demand.iterrows():
            print("Weather yr: {} Total p: {}".format(index, np.sum(row)))
            assert round(np.sum(row), 4) == 100.0

        # ----------------------------
        # Calculate standard deviation
        # ----------------------------
        std_deviation_df_abs_demand_in_peak_h_pp = df_abs_demand_in_peak_h_pp.std()
        std_deviation_abs_demand_peak_h = df_abs_peak_demand.std()
        std_deviation_p_demand_peak_h = df_p_peak_demand.std()

        print("=========")
        print("National stats")
        print("=========")
        print("Sum of std of absolut peak demand:  " + str(np.sum(std_deviation_abs_demand_peak_h)))

        # --------------------
        # Create map
        # --------------------
        regional_statistics_columns = [
            'name',
            'std_deviation_p_demand_peak_h',
            'std_deviation_abs_demand_peak_h',
            'std_deviation_df_abs_demand_in_peak_h_pp',
            'diff_peak_h_minus_mean']

        df_stats = pd.DataFrame(columns=regional_statistics_columns)

        for region_name in regions:

            # 'name', 'absolute_GW', 'p_GW_peak'
            line_entry = [[
                region_name,
                std_deviation_p_demand_peak_h[region_name],
                std_deviation_abs_demand_peak_h[region_name],
                std_deviation_df_abs_demand_in_peak_h_pp[region_name],
                diff_peak_h_minus_mean[region_name],
                ]]

            line_df = pd.DataFrame(line_entry, columns=regional_statistics_columns)
            df_stats = df_stats.append(line_df)

        # Load uk shapefile
        uk_shapefile = gpd.read_file(path_shapefile)

        # Merge stats to geopanda
        shp_gdp_merged = uk_shapefile.merge(
            df_stats,
            on='name')

        # Assign projection
        crs = {'init': 'epsg:27700'} #27700: OSGB_1936_British_National_Grid
        uk_gdf = gpd.GeoDataFrame(shp_gdp_merged, crs=crs)

        ax = uk_gdf.plot(
            figsize=basic_plot_functions.cm2inch(25, 20))

        nr_of_intervals = 6

        bin_values = result_mapping.get_reasonable_bin_values_II(
            data_to_plot=list(uk_gdf[field_to_plot]),
            nr_of_intervals=nr_of_intervals)
        # Maual bins
        #bin_values = [0, 0.005, 0.01, 0.015, 0.02, 0.025, 0.03]
        print(float(uk_gdf[field_to_plot].max()))
        print("BINS " + str(bin_values))

        uk_gdf, cmap_rgb_colors, color_zero, min_value, max_value = fig_p2_weather_val.user_defined_bin_classification(
            uk_gdf,
            field_to_plot,
            bin_values=bin_values)

        # plot with face color attribute
        uk_gdf.plot(
            ax=ax,
            facecolor=uk_gdf['bin_color'],
            edgecolor='black',
            linewidth=0.5)

        legend_handles = result_mapping.add_simple_legend(
            bin_values,
            cmap_rgb_colors,
            color_zero)

        plt.legend(
            handles=legend_handles,
            title="{}  [{}]".format(field_to_plot, unit),
            prop={'size': 8},
            #loc='upper center', bbox_to_anchor=(0.5, -0.05),
            loc='center left', bbox_to_anchor=(1, 0.5),
            frameon=False)

        # PLot bins on plot
        '''plt.text(
            -20,
            -20,
            bin_values[:-1], #leave away maximum value
            fontsize=8)'''

        plt.tight_layout()

        fig_out_path = os.path.join(fig_path, str(field_to_plot) + "__" + str(simulation_yr) + ".pdf")
        plt.savefig(fig_out_path)
def run(path_fig_folder,
        path_plot_fig,
        calc_av_lp_modelled,
        calc_av_lp_real,
        calc_lp_modelled=None,
        calc_lp_real=None,
        plot_peak=False,
        plot_radar=False,
        plot_all_entries=False,
        plot_max_min_polygon=True,
        plotshow=False,
        max_y_to_plot=60,
        fueltype_str=False,
        year=False):
    """Plotting average saisonal loads for each daytype. As an input
    GWh is provided, which for each h is cancelled out to GW.

    https://stackoverflow.com/questions/4325733/save-a-subplot-in-matplotlib
    http://matthiaseisen.com/matplotlib/shapes/reg-polygon/
    """
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(14, 25))

    # ax = fig.add_subplot(nrows=4, ncols=2)

    plot_nr = 0
    row = -1
    for season in calc_av_lp_modelled:
        row += 1
        col = -1
        for daytype in calc_av_lp_modelled[season]:
            col += 1
            plot_nr += 1

            axes = plt.subplot(4, 2, plot_nr)

            # ------------------
            # Plot average
            # ------------------
            x_values = range(24)

            plt.plot(x_values,
                     list(calc_av_lp_real[season][daytype]),
                     color='black',
                     label='av_real or av by',
                     linestyle='--',
                     linewidth=0.5)

            plt.plot(x_values,
                     list(calc_av_lp_modelled[season][daytype]),
                     color='blue',
                     label='av_modelled or av cy',
                     linestyle='--',
                     linewidth=0.5)

            # --------------
            # Radar plots
            # --------------
            if plot_radar:
                name_spider_plot = os.path.join(
                    path_fig_folder,
                    "spider_{}_{}_{}_{}_.pdf".format(year, fueltype_str,
                                                     season, daytype))

                plot_radar_plot(list(calc_av_lp_modelled[season][daytype]),
                                name_spider_plot,
                                plot_steps=20,
                                plotshow=False)

            # ------------------
            # Plot every single line
            # ------------------
            if plot_all_entries:
                for entry in range(len(calc_lp_real[season][daytype])):
                    plt.plot(x_values,
                             list(calc_lp_real[season][daytype][entry]),
                             color='grey',
                             markersize=0.5,
                             alpha=0.2)

                    plt.plot(x_values,
                             list(calc_lp_modelled[season][daytype][entry]),
                             color='blue',
                             markersize=0.5,
                             alpha=0.2)

            # ----------
            # Plot max_min range polygons
            # ----------
            if plot_max_min_polygon:

                # ----Draw real
                min_max_polygon = []
                upper_boundary = []
                lower_bdoundary = []

                # Get min and max of all entries for hour
                for hour in range(24):
                    min_y = np.min(calc_lp_real[season][daytype][:, hour],
                                   axis=0)
                    max_y = np.max(calc_lp_real[season][daytype][:, hour],
                                   axis=0)
                    upper_boundary.append((hour, min_y))
                    lower_bdoundary.append((hour, max_y))

                # create correct sorting to draw filled polygon
                min_max_polygon = fig_lf.order_polygon(upper_boundary,
                                                       lower_bdoundary)
                #min_max_polygon = create_min_max_polygon_from_lines(reg_load_factor_y)

                polygon = plt.Polygon(min_max_polygon,
                                      color='grey',
                                      alpha=0.2,
                                      edgecolor=None,
                                      linewidth=0,
                                      fill='True')

                axes.add_patch(polygon)

                # -----Draw modelled
                min_max_polygon = []
                upper_boundary = []
                lower_bdoundary = []

                # Get min and max of all entries for hour
                for hour in range(24):
                    min_y = np.min(calc_lp_modelled[season][daytype][:, hour],
                                   axis=0)
                    max_y = np.max(calc_lp_modelled[season][daytype][:, hour],
                                   axis=0)
                    upper_boundary.append((hour, min_y))
                    lower_bdoundary.append((hour, max_y))

                # create correct sorting to draw filled polygon
                min_max_polygon = fig_lf.order_polygon(upper_boundary,
                                                       lower_bdoundary)

                polygon = plt.Polygon(min_max_polygon,
                                      color='blue',
                                      alpha=0.2,
                                      edgecolor=None,
                                      linewidth=0,
                                      fill='True')

                axes.add_patch(polygon)

            # --------------------
            # Get load shape within season with highest houly load
            # --------------------
            if plot_peak:

                # Get row with maximum hourly value
                day_with_max_h = np.argmax(
                    np.max(calc_lp_real[season][daytype], axis=1))

                plt.plot(x_values,
                         list(calc_lp_real[season][daytype][day_with_max_h]),
                         color='grey',
                         markersize=1.0,
                         label='real_peak or by peak',
                         linestyle='-.',
                         linewidth=0.5)

                # Get row with maximum hourly value
                day_with_max_h = np.argmax(
                    np.max(calc_lp_modelled[season][daytype], axis=1))

                plt.plot(
                    x_values,
                    list(calc_lp_modelled[season][daytype][day_with_max_h]),
                    color='blue',
                    markersize=1.0,
                    label='modelled_peak or cy peak',
                    linestyle='-.',
                    linewidth=0.5)

            # -----------------
            # Axis
            # -----------------
            plt.ylim(0, max_y_to_plot)
            plt.xlim(0, 23)

            # Tight layout
            plt.tight_layout()
            plt.margins(x=0)

            # Calculate RMSE
            rmse = basic_functions.rmse(calc_av_lp_modelled[season][daytype],
                                        calc_av_lp_real[season][daytype])

            # Calculate R_squared
            slope, intercept, r_value, p_value, std_err = stats.linregress(
                calc_av_lp_modelled[season][daytype],
                calc_av_lp_real[season][daytype])

            # Calculate standard deviation
            std_dev_p = np.std(calc_av_lp_real[season][daytype] -
                               calc_av_lp_modelled[season][daytype])
            std_dev_abs = np.std(
                abs(calc_av_lp_real[season][daytype] -
                    calc_av_lp_modelled[season][daytype]))

            # -----------
            # Labelling
            # -----------
            font_additional_info = plotting_styles.font_info()

            title_info = ('{}, {}'.format(season, daytype))
            plt.text(
                1, 0.55, "RMSE: {}, R_squared: {}, std: {} (+- {})".format(
                    round(rmse, 2),
                    round(r_value, 2),
                    round(std_dev_p, 2),
                    round(std_dev_abs, 2),
                    fontdict=font_additional_info))
            plt.title(title_info, loc='left', fontdict=font_additional_info)

    # ------------
    # Plot legend
    # ------------
    plt.legend(ncol=1,
               loc=2,
               prop={
                   'family': 'arial',
                   'size': 5
               },
               frameon=False)

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    fig.savefig(path_plot_fig)

    if plotshow:
        plt.show()
        plt.close()
    else:
        plt.close()
Exemplo n.º 22
0
def run(lookups, years_simulated, results_enduse_every_year, rs_enduses,
        ss_enduses, is_enduses, fig_name):
    """Plots summarised endues for the three sectors. Annual
    GWh are converted into GW.

    Arguments
    ----------
    data : dict
        Data container
    results_objects :

    enduses_data :

    Note
    ----
        -   Sum across all fueltypes

    # INFO Cannot plot a single year?
    """
    logging.info("plot annual demand for enduses for all submodels")

    submodels = ['residential', 'service', 'industry']
    nr_submodels = len(submodels)

    x_data = years_simulated
    y_data = np.zeros((nr_submodels, len(years_simulated)))

    # Set figure size
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(8, 8))

    ax = fig.add_subplot(1, 1, 1)

    for model_year, data_model_run in enumerate(
            results_enduse_every_year.values()):

        submodel = 0
        for fueltype_int in range(lookups['fueltypes_nr']):
            for enduse in rs_enduses:

                # Conversion: Convert gwh per years to gw
                yearly_sum_gw = np.sum(data_model_run[enduse][fueltype_int])
                yearly_sum_twh = conversions.gwh_to_twh(yearly_sum_gw)
                y_data[submodel][model_year] += yearly_sum_twh  #yearly_sum_gw

        submodel = 1
        for fueltype_int in range(lookups['fueltypes_nr']):
            for enduse in ss_enduses:

                # Conversion: Convert gwh per years to gw
                yearly_sum_gw = np.sum(data_model_run[enduse][fueltype_int])
                yearly_sum_twh = conversions.gwh_to_twh(yearly_sum_gw)
                y_data[submodel][model_year] += yearly_sum_twh  #yearly_sum_gw

        submodel = 2
        for fueltype_int in range(lookups['fueltypes_nr']):
            for enduse in is_enduses:

                # Conversion: Convert gwh per years to gw
                yearly_sum_gw = np.sum(data_model_run[enduse][fueltype_int])
                yearly_sum_twh = conversions.gwh_to_twh(yearly_sum_gw)
                y_data[submodel][model_year] += yearly_sum_twh  #yearly_sum_gw

    # Convert to stack
    y_stacked = np.row_stack((y_data))

    color_stackplots = ['darkturquoise', 'orange', 'firebrick']

    # ----------
    # Stack plot
    # ----------
    ax.stackplot(x_data, y_stacked, colors=color_stackplots)

    # ------------
    # Plot color legend with colors for every SUBMODEL
    # ------------
    plt.legend(submodels,
               ncol=1,
               prop={
                   'family': 'arial',
                   'size': 5
               },
               loc='best',
               frameon=False)

    # -------
    # Axis
    # -------
    plt.xticks(years_simulated, years_simulated)
    plt.axis('tight')

    # -------
    # Labels
    # -------
    plt.ylabel("TWh")
    plt.xlabel("year")
    plt.title("UK ED per sector")

    # Tight layout
    plt.margins(x=0)
    fig.tight_layout()

    # Save fig
    plt.savefig(fig_name)
    plt.close()
def plot_LAD_comparison_scenarios(scenario_data,
                                  year_to_plot,
                                  fig_name,
                                  plotshow=True):
    """Plot chart comparing total annual demand for all LADs

    Arguments
    ---------
    scenario_data : dict
        Scenario name, scenario data
    year_to_plot : int
        Year to plot different LAD values
    fig_name : str
        Path to out pdf figure
    plotshow : bool
        Plot figure or not

    Info
    -----
    if scenario name starts with _ the legend does not work
    """

    # Get first scenario in dict
    all_scenarios = list(scenario_data.keys())
    first_scenario = str(all_scenarios[:1][0])

    # ----------------
    # Sort regions according to size
    # -----------------
    regions = {}
    for fueltype, fuels_regs in enumerate(
            scenario_data[first_scenario]['ed_fueltype_regs_yh'][2015]):

        for region_array_nr, fuel_reg in enumerate(fuels_regs):
            try:
                regions[region_array_nr] += np.sum(fuel_reg)
            except KeyError:
                regions[region_array_nr] = np.sum(fuel_reg)

    sorted_regions = sorted(regions.items(), key=operator.itemgetter(1))

    sorted_regions_nrs = []
    for sort_info in sorted_regions:
        sorted_regions_nrs.append(sort_info[0])

    # Labels
    labels = []
    for sorted_region in sorted_regions_nrs:
        geocode_lad = sorted_region  # If actual LAD name, change this
        labels.append(geocode_lad)

    # -------------------------------------
    # Plot
    # -------------------------------------
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(9, 8))

    ax = fig.add_subplot(1, 1, 1)

    x_values = np.arange(0, len(sorted_regions_nrs), 1)

    # ----------------------------------------------
    # Plot base year values
    # ----------------------------------------------
    base_year_data = []
    for reg_array_nr in sorted_regions_nrs:
        base_year_data.append(regions[reg_array_nr])
    total_base_year_sum = sum(base_year_data)

    plt.plot(x_values,
             base_year_data,
             linestyle='None',
             marker='o',
             markersize=1.6,
             fillstyle='full',
             markerfacecolor='grey',
             markeredgewidth=0.4,
             color='black',
             label='actual_by ({})'.format(total_base_year_sum))

    # ----------------------------------------------
    # Plot all future scenario values
    # ----------------------------------------------
    color_list = plotting_styles.color_list()

    for scenario_nr, (scenario_name,
                      fuel_data) in enumerate(scenario_data.items()):

        sorted_year_data = []
        for reg_array_nr in sorted_regions_nrs:
            tot_fuel_across_fueltypes = 0
            for fueltype, fuel_regs in enumerate(
                    fuel_data['ed_fueltype_regs_yh'][year_to_plot]):
                tot_fuel_across_fueltypes += np.sum(fuel_regs[reg_array_nr])

            sorted_year_data.append(tot_fuel_across_fueltypes)

        tot_fuel_all_reg = np.sum(
            fuel_data['ed_fueltype_regs_yh'][year_to_plot])
        print("TOTAL FUEL in GWH " + str(tot_fuel_all_reg))

        # Calculate total annual demand
        tot_demand = sum(sorted_year_data)
        scenario_name = "{} (tot: {} [GWh])".format(scenario_name,
                                                    round(tot_demand, 2))

        plt.plot(x_values,
                 sorted_year_data,
                 linestyle='None',
                 marker='o',
                 markersize=1.6,
                 fillstyle='full',
                 markerfacecolor=color_list[scenario_nr],
                 markeredgewidth=0.4,
                 color=color_list[scenario_nr],
                 label=scenario_name)

    # --------
    # Axis
    # --------
    plt.tick_params(
        axis='x',  # changes apply to the x-axis
        which='both',  # both major and minor ticks are affected
        bottom='off',  # ticks along the bottom edge are off
        top='off',  # ticks along the top edge are off
        labelbottom='off')  # labels along the bottom edge are off

    # Limit
    plt.ylim(ymin=0)

    # -----------
    # Labelling
    # -----------
    label_points = False
    if label_points:
        for pos, txt in enumerate(labels):
            ax.text(x_values[pos],
                    sorted_year_data[pos],
                    txt,
                    horizontalalignment="right",
                    verticalalignment="top",
                    fontsize=3)

    plt.title("TEST", loc='left', fontdict=plotting_styles.font_info())

    plt.xlabel("UK regions (excluding northern ireland)")
    plt.ylabel("[GWh]")

    # --------
    # Legend
    # --------
    plt.legend(prop={'family': 'arial', 'size': 8}, frameon=False)

    # Tight layout
    plt.margins(x=0)
    plt.tight_layout()
    plt.savefig(fig_name)

    if plotshow:
        plt.show()
    else:
        plt.close()
Exemplo n.º 24
0
def plot_seasonal_lf(fueltype_int,
                     fueltype_str,
                     load_factors_seasonal,
                     reg_nrs,
                     path_plot_fig,
                     plot_individ_lines=False,
                     plot_max_min_polygon=True):
    """Plot load factors per region for every year

    Arguments
    --------
    fueltype_int : int
        Fueltype_int to print (see lookup)
    fueltype_str : str
        Fueltype string to print
    load_factors_seasonal : dict
        Seasonal load factors per season
    reg_nrs : int
        Number of region
    """
    print("... plotting seasonal load factors")

    # Set figure size
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(8, 8))
    ax = fig.add_subplot(1, 1, 1)

    # Settings
    color_list = {
        'winter': 'midnightblue',
        'summer': 'olive',
        'spring': 'darkgreen',
        'autumn': 'gold'
    }

    classes = list(color_list.keys())
    #class_colours = list(color_list.values())

    # ------------
    # Iterate regions and plot load factors for every region
    # ------------
    if plot_individ_lines:
        for reg_nr in range(reg_nrs):
            for season, lf_fueltypes_season in load_factors_seasonal.items():

                x_values_season_year = []
                y_values_season_year = []
                for year, lf_fueltype_reg in lf_fueltypes_season.items():
                    x_values_season_year.append(year)
                    y_values_season_year.append(
                        lf_fueltype_reg[fueltype_int][reg_nr])

                # plot individual saisonal data point
                plt.plot(x_values_season_year,
                         y_values_season_year,
                         color=color_list[season],
                         linewidth=0.2,
                         alpha=0.2)

    # -----------------
    # Plot min_max_area
    # -----------------
    if plot_max_min_polygon:

        for season, lf_fueltypes_season in load_factors_seasonal.items():
            upper_boundary = []
            lower_bdoundary = []

            min_max_polygon = create_min_max_polygon_from_lines(
                lf_fueltypes_season)
            '''for year_nr, lf_fueltype_reg in lf_fueltypes_season.items():

                # Get min and max of all entries of year of all regions
                min_y = np.min(lf_fueltype_reg[fueltype_int])
                max_y = np.max(lf_fueltype_reg[fueltype_int])
                upper_boundary.append((year_nr, min_y))
                lower_bdoundary.append((year_nr, max_y))

            # create correct sorting to draw filled polygon
            min_max_polygon = order_polygon(upper_boundary, lower_bdoundary)'''

            polygon = plt.Polygon(min_max_polygon,
                                  color=color_list[season],
                                  alpha=0.2,
                                  edgecolor=None,
                                  linewidth=0,
                                  fill='True')

            ax.add_patch(polygon)

    # ------------------------------------
    # Calculate average per season for all regions
    # and plot average line a bit thicker
    # ------------------------------------
    for season in classes:
        years = []
        average_season_year_years = []
        for year in load_factors_seasonal[season].keys():
            average_season_year = []

            # Iterate over regions
            for reg_nr in range(reg_nrs):
                average_season_year.append(
                    load_factors_seasonal[season][year][fueltype_int][reg_nr])

            years.append(int(year))
            average_season_year_years.append(np.average(average_season_year))

        # plot average
        plt.plot(years,
                 average_season_year_years,
                 color=color_list[season],
                 linewidth=0.5,
                 linestyle='--',
                 alpha=1.0,
                 markersize=0.5,
                 marker='o',
                 label=season)

        # Plot markers for average line
        '''plt.plot(
            years,
            average_season_year_years,
            color=color_list[season],
            markersize=0.5,
            linewidth=0.5,
            marker='o')'''

    # -----------------
    # Axis
    # -----------------
    plt.ylim(0, 100)

    # -----------------
    # Axis labelling and ticks
    # -----------------
    plt.xlabel("years")
    plt.ylabel("load factor {} [%]".format(fueltype_str))

    base_yr = 2015
    minor_interval = 5
    major_interval = 10

    # Major ticks
    major_ticks = np.arange(base_yr, years[-1] + major_interval,
                            major_interval)
    ax.set_xticks(major_ticks)
    #ax.set_xlabel(major_ticks)

    # Minor ticks
    minor_ticks = np.arange(base_yr, years[-1] + minor_interval,
                            minor_interval)
    ax.set_xticks(minor_ticks, minor=True)
    #ax.set_xlabel(minor_ticks)

    # ------------
    # Plot color legend with colors for every season
    # ------------
    plt.legend(ncol=2,
               prop={
                   'family': 'arial',
                   'size': 5
               },
               loc='best',
               frameon=False)

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    # Save fig
    plt.savefig(path_plot_fig)
    plt.close()
def plot_heat_pump_chart(lookups,
                         regions,
                         scenario_data,
                         fig_name,
                         fueltype_str_input,
                         plotshow=False):
    """
    Compare share of element on x axis (provided in name of scenario)
    with load factor


    Info
    -----
    Run scenarios with different value in scenarion name

    e.g. 0.1 heat pump --> scen_0.1
    """
    year_to_plot = 2050

    # Collect value to display on axis
    result_dict = {
    }  # {scenario_value:  {year: {fueltype: np.array(reg, value))}}

    for scenario_name, scenario_data in scenario_data.items():
        print("Scenario to process: " + str(scenario_name))
        # Scenario value
        value_scenario = float(scenario_name.split("__")[1])

        # Get peak for all regions {year: {fueltype: np.array(reg,value))}
        y_lf_fueltype = {}

        # Load factor
        '''for year, data_lf_fueltypes in scenario_data['reg_load_factor_y'].items(): # {scenario_value: np.array((regions, result_value))}
            if year != year_to_plot:
                continue

            y_lf_fueltype[year] = {}
            for fueltype_int, data_lf in enumerate(data_lf_fueltypes):
                fueltype_str = tech_related.get_fueltype_str(lookups['fueltypes'], fueltype_int)

                # Select only fueltype data
                if fueltype_str == fueltype_str_input:
                    y_lf_fueltype[year] = data_lf
                else:
                    pass
        '''
        # PEAK VALUE ed_peak_regs_h ed_peak_h
        #for year, data_lf_fueltypes in scenario_data['ed_peak_regs_h'].items():
        for year, data_lf_fueltypes in scenario_data['ed_peak_h'].items():
            if year != year_to_plot:
                continue

            y_lf_fueltype[year] = {}
            for fueltype_str, data_lf in data_lf_fueltypes.items():

                # Select only fueltype data
                if fueltype_str == fueltype_str_input:
                    y_lf_fueltype[year] = data_lf
                else:
                    pass

        result_dict[value_scenario] = y_lf_fueltype

    # Sort dict and convert to OrderedDict
    result_dict = collections.OrderedDict(sorted(result_dict.items()))

    #-----
    # Plot
    # -----

    # Criteria to plot maximum boundaries
    plot_max_min_polygon = False
    plot_all_regs = False

    # Set figure size
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(16, 8))
    ax = fig.add_subplot(1, 1, 1)

    # -----------------
    # Axis
    # -----------------
    # Percentages on x axis
    major_ticks = list(result_dict.keys())

    plt.xticks(major_ticks, major_ticks)

    # ----------
    # Plot lines
    # ----------
    color_list_selection = plotting_styles.get_colorbrewer_color(
        color_prop='sequential',  #sequential
        color_palette='PuBu_4',
        inverse=False
    )  # #https://jiffyclub.github.io/palettable/colorbrewer/sequential/

    # all percent values
    all_percent_values = list(result_dict.keys())

    # Nr of years
    for _percent_value, fuel_fueltype_yrs in result_dict.items():
        years = list(fuel_fueltype_yrs.keys())
        break

    legend_entries = []
    for year in years:
        color_scenario = color_list_selection.pop()

        legend_entries.append("mean {}".format(year))

        # ----------------
        # For every region
        # ----------------
        '''for reg_nr, _ in enumerate(regions):
            year_data = []
            for _percent_value, fuel_fueltype_yrs in result_dict.items():
                year_data.append(fuel_fueltype_yrs[year][reg_nr])

            # Paste out if not individual regions and set plot_max_min_polygon to True
            if plot_all_regs:
                plt.plot(
                    list(all_percent_values),
                    list(year_data),
                    color=str(color_scenario))'''

        # --------------------
        # Plot max min polygon
        # --------------------
        if plot_max_min_polygon:

            # Create value {x_vals: [y_vals]}
            x_y_values = {}
            for _percent_value, fuel_fueltype_yrs in result_dict.items():
                x_y_values[_percent_value] = []
                for reg_nr, _ in enumerate(regions):
                    x_y_values[_percent_value].append(
                        result_dict[_percent_value][year])

            # Create polygons
            min_max_polygon = plotting_results.create_min_max_polygon_from_lines(
                x_y_values)

            polygon = plt.Polygon(min_max_polygon,
                                  color=color_scenario,
                                  alpha=0.2,
                                  edgecolor=None,
                                  linewidth=0,
                                  fill='True')

            ax.add_patch(polygon)

        # Average across all regs
        year_data = []
        for _percent_value, fuel_fueltype_yrs in result_dict.items():

            regs = fuel_fueltype_yrs[year]

            # --------------------------------------
            # Average load factor across all regions
            # --------------------------------------
            lf_peak_across_all_regs = np.average(regs)
            year_data.append(lf_peak_across_all_regs)

        plt.plot(list(all_percent_values),
                 list(year_data),
                 color=str(color_scenario))
    # ----
    # Axis
    # ----
    plt.ylim(ymin=0)
    plt.ylim(ymax=80)
    #plt.ylim(ymax=1.2)
    plt.xlim(xmin=0, xmax=60)

    # ------------
    # Plot legend
    # ------------
    plt.legend(legend_entries,
               ncol=1,
               loc=3,
               prop={
                   'family': 'arial',
                   'size': 10
               },
               frameon=False)

    # ---------
    # Labels
    # ---------
    plt.xlabel("heat pump residential heating [%]")
    #plt.ylabel("load factor [%] [{}]".format(fueltype_str_input))
    plt.ylabel("Peak demand h [GW] {}".format(fueltype_str_input))
    #plt.title("impact of changing residential heat pumps to load factor")

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    plt.savefig(fig_name)

    if plotshow:
        plt.show()
        plt.close()
    else:
        plt.close()
Exemplo n.º 26
0
def plot_lf_y(fueltype_int,
              fueltype_str,
              reg_load_factor_y,
              reg_nrs,
              path_plot_fig,
              plot_individ_lines=False,
              plot_max_min_polygon=True):
    """Plot load factors per region for every year

    Arguments
    --------

    """
    print("... plotting load factors")

    # Set figure size
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(8, 8))

    ax = fig.add_subplot(1, 1, 1)

    if plot_individ_lines:
        # Line plot for every region over years
        for reg_nr in range(reg_nrs):
            x_values_year = []
            y_values_year = []

            for year, lf_fueltype_reg in reg_load_factor_y.items():
                x_values_year.append(year)
                y_values_year.append(lf_fueltype_reg[fueltype_int][reg_nr])

            plt.plot(x_values_year, y_values_year, linewidth=0.2, color='grey')

    if plot_max_min_polygon:
        '''lower_bdoundary = []
        upper_boundary = []

        for year_nr, lf_fueltype_reg in reg_load_factor_y.items():

            # Get min and max of all entries of year of all regions
            min_y = np.min(lf_fueltype_reg[fueltype_int])
            max_y = np.max(lf_fueltype_reg[fueltype_int])
            upper_boundary.append((year_nr, min_y))
            lower_bdoundary.append((year_nr, max_y))

        # create correct sorting to draw filled polygon
        min_max_polygon = order_polygon(upper_boundary, lower_bdoundary)'''

        min_max_polygon = create_min_max_polygon_from_lines(reg_load_factor_y)

        polygon = plt.Polygon(min_max_polygon,
                              color='grey',
                              alpha=0.2,
                              edgecolor=None,
                              linewidth=0,
                              fill='True')

        ax.add_patch(polygon)
    # -----------------
    # Axis
    # -----------------
    plt.ylim(0, 100)

    # -----------------
    # Axis labelling
    # -----------------
    plt.xlabel("years")
    plt.ylabel("load factor, fueltpye {} [%]".format(fueltype_str))

    years = list(reg_load_factor_y.keys())
    base_yr = 2015

    # Major ticks
    major_interval = 10
    major_ticks = np.arange(base_yr, years[-1] + major_interval,
                            major_interval)
    ax.set_xticks(major_ticks)

    # Minor ticks
    minor_interval = 5
    minor_ticks = np.arange(base_yr, years[-1] + minor_interval,
                            minor_interval)
    ax.set_xticks(minor_ticks, minor=True)

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    plt.savefig(path_plot_fig)
    plt.close()
def plot_reg_y_over_time(scenario_data, fig_name, plotshow=False):
    """Plot total demand over simulation period for every
    scenario for all regions
    """
    # Set figure size
    plt.figure(figsize=basic_plot_functions.cm2inch(14, 8))

    y_scenario = {}

    for scenario_name, scen_data in scenario_data.items():

        data_years_regs = {}
        for year, fueltype_reg_time in scen_data['ed_fueltype_regs_yh'].items(
        ):
            data_years_regs[year] = {}

            for _fueltype, regions_fuel in enumerate(fueltype_reg_time):
                for region_nr, region_fuel in enumerate(regions_fuel):

                    # Sum all regions and fueltypes
                    reg_gwh_fueltype_y = np.sum(region_fuel)

                    try:
                        data_years_regs[year][region_nr] += reg_gwh_fueltype_y
                    except:
                        data_years_regs[year][region_nr] = reg_gwh_fueltype_y

        y_scenario[scenario_name] = data_years_regs

    # -----------------
    # Axis
    # -----------------
    base_yr, year_interval = 2015, 5
    first_scen = list(y_scenario.keys())[0]
    end_yr = list(y_scenario[first_scen].keys())[-1]

    major_ticks = np.arange(base_yr, end_yr + year_interval, year_interval)

    plt.xticks(major_ticks, major_ticks)

    # ----------
    # Plot lines
    # ----------
    color_list_selection = plotting_styles.color_list_scenarios()
    cnt = -1
    for scenario_name, fuel_fueltype_yrs in y_scenario.items():

        cnt += 1

        color_scenario = color_list_selection[cnt]

        for year, regs in fuel_fueltype_yrs.items():
            nr_of_reg = len(regs.keys())
            break

        for reg_nr in range(nr_of_reg):
            reg_data = []
            for year, regions_fuel in fuel_fueltype_yrs.items():
                reg_data.append(regions_fuel[reg_nr])

            plt.plot(list(fuel_fueltype_yrs.keys()),
                     list(reg_data),
                     label="{}".format(scenario_name),
                     color=str(color_scenario))
    # ----
    # Axis
    # ----
    plt.ylim(ymin=0)

    # ------------
    # Plot legend
    # ------------
    '''plt.legend(
        ncol=2,
        loc=3,
        prop={
            'family': 'arial',
            'size': 10},
        frameon=False)'''

    # ---------
    # Labels
    # ---------
    font_additional_info = plotting_styles.font_info(size=5)

    plt.ylabel("GWh")
    plt.xlabel("year")
    plt.title("tot_y", fontdict=font_additional_info)

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    plt.savefig(fig_name)

    if plotshow:
        plt.show()
        plt.close()
    else:
        plt.close()
Exemplo n.º 28
0
def plotting_weather_data(path):
    """

    Things to plot

    - annual t_min of all realizations
    - annual t_max of all realizations

    - annual maximum t_max
    - annual minimum t_min
    """
    sim_yrs = range(2015, 2051, 5)
    weather_reationzations = ["NF{}".format(i) for i in range(1, 101, 1)]

    container_weather_stations = {}
    container_temp_data = {}

    # All used weather stations from model run
    used_stations = [
        'station_id_253', 'station_id_252', 'station_id_253', 'station_id_252',
        'station_id_252', 'station_id_328', 'station_id_329', 'station_id_305',
        'station_id_282', 'station_id_335', 'station_id_335', 'station_id_359',
        'station_id_358', 'station_id_309', 'station_id_388', 'station_id_418',
        'station_id_420', 'station_id_389', 'station_id_433', 'station_id_385',
        'station_id_374', 'station_id_481', 'station_id_481', 'station_id_480',
        'station_id_466', 'station_id_531', 'station_id_532', 'station_id_535',
        'station_id_535', 'station_id_484', 'station_id_421', 'station_id_472',
        'station_id_526', 'station_id_525', 'station_id_526', 'station_id_504',
        'station_id_503', 'station_id_504', 'station_id_505', 'station_id_504',
        'station_id_504', 'station_id_455', 'station_id_548', 'station_id_546',
        'station_id_537', 'station_id_545', 'station_id_236', 'station_id_353',
        'station_id_352', 'station_id_384', 'station_id_510', 'station_id_527',
        'station_id_550', 'station_id_501', 'station_id_456', 'station_id_472',
        'station_id_201', 'station_id_470', 'station_id_487', 'station_id_505',
        'station_id_486', 'station_id_457', 'station_id_533', 'station_id_458',
        'station_id_441', 'station_id_440', 'station_id_473', 'station_id_217',
        'station_id_247', 'station_id_199', 'station_id_232', 'station_id_234',
        'station_id_478', 'station_id_248', 'station_id_388', 'station_id_377',
        'station_id_376', 'station_id_376', 'station_id_388', 'station_id_354',
        'station_id_376', 'station_id_388', 'station_id_515', 'station_id_514',
        'station_id_514', 'station_id_531', 'station_id_532', 'station_id_494',
        'station_id_512', 'station_id_535', 'station_id_535', 'station_id_517',
        'station_id_534', 'station_id_533', 'station_id_549', 'station_id_549',
        'station_id_550', 'station_id_549', 'station_id_508', 'station_id_490',
        'station_id_507', 'station_id_526', 'station_id_508', 'station_id_491',
        'station_id_507', 'station_id_489', 'station_id_509', 'station_id_526',
        'station_id_545', 'station_id_492', 'station_id_490', 'station_id_451',
        'station_id_467', 'station_id_450', 'station_id_451', 'station_id_466',
        'station_id_451', 'station_id_521', 'station_id_538', 'station_id_537',
        'station_id_537', 'station_id_522', 'station_id_546', 'station_id_536',
        'station_id_522', 'station_id_520', 'station_id_537', 'station_id_488',
        'station_id_487', 'station_id_488', 'station_id_472', 'station_id_487',
        'station_id_487', 'station_id_551', 'station_id_544', 'station_id_419',
        'station_id_525', 'station_id_552', 'station_id_525', 'station_id_543',
        'station_id_542', 'station_id_552', 'station_id_543', 'station_id_544',
        'station_id_542', 'station_id_542', 'station_id_306', 'station_id_305',
        'station_id_282', 'station_id_306', 'station_id_406', 'station_id_264',
        'station_id_306', 'station_id_283', 'station_id_284', 'station_id_306',
        'station_id_305', 'station_id_304', 'station_id_283', 'station_id_418',
        'station_id_403', 'station_id_419', 'station_id_418', 'station_id_403',
        'station_id_402', 'station_id_392', 'station_id_379', 'station_id_391',
        'station_id_422', 'station_id_404', 'station_id_379', 'station_id_443',
        'station_id_444', 'station_id_445', 'station_id_423', 'station_id_469',
        'station_id_425', 'station_id_444', 'station_id_461', 'station_id_438',
        'station_id_437', 'station_id_439', 'station_id_438', 'station_id_455',
        'station_id_454', 'station_id_438', 'station_id_284', 'station_id_268',
        'station_id_286', 'station_id_266', 'station_id_288', 'station_id_270',
        'station_id_333', 'station_id_389', 'station_id_378', 'station_id_389',
        'station_id_389', 'station_id_377', 'station_id_390', 'station_id_403',
        'station_id_469', 'station_id_485', 'station_id_484', 'station_id_469',
        'station_id_499', 'station_id_498', 'station_id_516', 'station_id_497',
        'station_id_479', 'station_id_400', 'station_id_387', 'station_id_401',
        'station_id_374', 'station_id_400', 'station_id_386', 'station_id_375',
        'station_id_401', 'station_id_491', 'station_id_459', 'station_id_492',
        'station_id_476', 'station_id_475', 'station_id_477', 'station_id_462',
        'station_id_523', 'station_id_523', 'station_id_522', 'station_id_523',
        'station_id_523', 'station_id_523', 'station_id_505', 'station_id_522',
        'station_id_541', 'station_id_539', 'station_id_523', 'station_id_417',
        'station_id_417', 'station_id_437', 'station_id_436', 'station_id_436',
        'station_id_548', 'station_id_547', 'station_id_488', 'station_id_539',
        'station_id_540', 'station_id_540', 'station_id_540', 'station_id_547',
        'station_id_416', 'station_id_434', 'station_id_435', 'station_id_434',
        'station_id_434', 'station_id_415', 'station_id_488', 'station_id_488',
        'station_id_489', 'station_id_329', 'station_id_330', 'station_id_330',
        'station_id_330', 'station_id_330', 'station_id_329', 'station_id_354',
        'station_id_330', 'station_id_329', 'station_id_329', 'station_id_328',
        'station_id_328', 'station_id_328', 'station_id_304', 'station_id_327',
        'station_id_331', 'station_id_357', 'station_id_356', 'station_id_355',
        'station_id_221', 'station_id_221', 'station_id_221', 'station_id_237',
        'station_id_416', 'station_id_417', 'station_id_416', 'station_id_416',
        'station_id_417', 'station_id_400', 'station_id_400', 'station_id_307',
        'station_id_307', 'station_id_331', 'station_id_308', 'station_id_332',
        'station_id_221', 'station_id_506', 'station_id_507', 'station_id_506',
        'station_id_525', 'station_id_506', 'station_id_524', 'station_id_506',
        'station_id_524', 'station_id_505', 'station_id_506', 'station_id_524',
        'station_id_506', 'station_id_506', 'station_id_506', 'station_id_505',
        'station_id_507', 'station_id_505', 'station_id_505', 'station_id_506',
        'station_id_506', 'station_id_523', 'station_id_524', 'station_id_524',
        'station_id_524', 'station_id_506', 'station_id_507', 'station_id_505',
        'station_id_524', 'station_id_524', 'station_id_506', 'station_id_506',
        'station_id_524', 'station_id_506', 'station_id_172', 'station_id_193',
        'station_id_173', 'station_id_133', 'station_id_130', 'station_id_168',
        'station_id_194', 'station_id_152', 'station_id_170', 'station_id_214',
        'station_id_195', 'station_id_103', 'station_id_124', 'station_id_110',
        'station_id_176', 'station_id_136', 'station_id_125', 'station_id_121',
        'station_id_9', 'station_id_111', 'station_id_105', 'station_id_36',
        'station_id_108', 'station_id_52', 'station_id_119', 'station_id_4',
        'station_id_94', 'station_id_159', 'station_id_137', 'station_id_102',
        'station_id_77', 'station_id_303', 'station_id_2', 'station_id_154',
        'station_id_64', 'station_id_89', 'station_id_124', 'station_id_109',
        'station_id_109', 'station_id_123', 'station_id_86', 'station_id_105',
        'station_id_110', 'station_id_110', 'station_id_448', 'station_id_348',
        'station_id_349', 'station_id_350', 'station_id_351', 'station_id_372',
        'station_id_394', 'station_id_449', 'station_id_464', 'station_id_407',
        'station_id_428', 'station_id_446', 'station_id_446', 'station_id_463',
        'station_id_463', 'station_id_464', 'station_id_447', 'station_id_448',
        'station_id_448', 'station_id_396', 'station_id_447'
    ]

    # Load full data
    for weather_realisation in weather_reationzations:
        print("weather_realisation: " + str(weather_realisation))
        path_weather_data = path

        weather_stations, temp_data = data_loader.load_temp_data(
            {},
            sim_yrs=sim_yrs,
            weather_realisation=weather_realisation,
            path_weather_data=path_weather_data,
            same_base_year_weather=False,
            crit_temp_min_max=True,
            load_np=False,
            load_parquet=False,
            load_csv=True)

        # Load only data from selected weather stations
        temp_data_used = {}
        for year in sim_yrs:
            temp_data_used[year] = {}
            all_station_data = temp_data[year].keys()
            for station in all_station_data:
                if station in used_stations:
                    temp_data_used[year][station] = temp_data[year][station]

        container_weather_stations[weather_realisation] = weather_stations
        container_temp_data[weather_realisation] = temp_data_used

    # Create plot with daily min
    print("... creating min max plot")
    t_min_average_every_day = []
    t_max_average_every_day = []
    t_min_min_every_day = []
    t_max_max_every_day = []
    std_dev_t_min = []
    std_dev_t_max = []
    std_dev_t_min_min = []
    std_dev_t_max_max = []

    for year in sim_yrs:

        for realization in container_weather_stations.keys():
            t_min_average_stations = []
            t_max_average_stations = []
            t_min_min_average_stations = []
            t_max_max_average_stations = []
            stations_data = container_temp_data[realization][year]
            for station in stations_data.keys():
                t_min_annual_average = np.average(
                    stations_data[station]['t_min'])
                t_max_annual_average = np.average(
                    stations_data[station]['t_max'])
                t_min_min_stations = np.min(stations_data[station]['t_min'])
                t_max_max_stations = np.max(stations_data[station]['t_max'])

                t_min_average_stations.append(
                    t_min_annual_average)  #average cross all stations
                t_max_average_stations.append(
                    t_max_annual_average)  #average cross all stations
                t_min_min_average_stations.append(t_min_min_stations)
                t_max_max_average_stations.append(t_max_max_stations)

        av_t_min = np.average(
            t_min_average_stations)  #average across all realizations
        av_t_max = np.average(
            t_max_average_stations)  #average across all realizations
        av_min_t_min = np.average(
            t_min_min_average_stations)  #average across all realizations
        av_max_t_max = np.average(
            t_max_max_average_stations)  #average across all realizations

        std_t_min = np.std(t_min_average_stations)
        std_t_max = np.std(t_max_average_stations)
        std_t_min_min = np.std(t_min_min_average_stations)
        std_t_max_max = np.std(t_max_max_average_stations)

        t_min_average_every_day.append(av_t_min)
        t_max_average_every_day.append(av_t_max)
        t_min_min_every_day.append(av_min_t_min)
        t_max_max_every_day.append(av_max_t_max)

        std_dev_t_min.append(std_t_min)
        std_dev_t_max.append(std_t_max)
        std_dev_t_min_min.append(std_t_min_min)
        std_dev_t_max_max.append(std_t_max_max)

    # Plot variability
    fig = plt.figure(figsize=basic_plot_functions.cm2inch(9,
                                                          6))  #width, height

    colors = {
        't_min': 'steelblue',
        't_max': 'tomato',
        't_min_min': 'peru',
        't_max_max': 'r'
    }

    # plot
    plt.plot(sim_yrs,
             t_min_average_every_day,
             color=colors['t_min'],
             label="t_min")
    plt.plot(sim_yrs,
             t_max_average_every_day,
             color=colors['t_max'],
             label="t_max")
    plt.plot(sim_yrs,
             t_min_min_every_day,
             color=colors['t_min_min'],
             label="t_min_min")
    plt.plot(sim_yrs,
             t_max_max_every_day,
             color=colors['t_max_max'],
             label="t_max_max")

    # Variations
    plt.fill_between(
        sim_yrs,
        list(
            np.array(t_min_average_every_day) - (2 * np.array(std_dev_t_min))),
        list(
            np.array(t_min_average_every_day) + (2 * np.array(std_dev_t_min))),
        color=colors['t_min'],
        alpha=0.25)

    plt.fill_between(
        sim_yrs,
        list(
            np.array(t_max_average_every_day) - (2 * np.array(std_dev_t_max))),
        list(
            np.array(t_max_average_every_day) + (2 * np.array(std_dev_t_max))),
        color=colors['t_max'],
        alpha=0.25)

    plt.fill_between(
        sim_yrs,
        list(
            np.array(t_min_min_every_day) - (2 * np.array(std_dev_t_min_min))),
        list(
            np.array(t_min_min_every_day) + (2 * np.array(std_dev_t_min_min))),
        color=colors['t_min_min'],
        alpha=0.25)

    plt.fill_between(
        sim_yrs,
        list(
            np.array(t_max_max_every_day) - (2 * np.array(std_dev_t_max_max))),
        list(
            np.array(t_max_max_every_day) + (2 * np.array(std_dev_t_max_max))),
        color=colors['t_max_max'],
        alpha=0.25)

    # Legend
    legend = plt.legend(ncol=2,
                        prop={'size': 10},
                        loc='upper center',
                        bbox_to_anchor=(0.5, -0.1),
                        frameon=False)
    legend.get_title().set_fontsize(8)

    result_path = "C:/_scrap/"
    seperate_legend = True
    if seperate_legend:
        basic_plot_functions.export_legend(
            legend,
            os.path.join(result_path, "{}__legend.pdf".format(result_path)))
        legend.remove()

    plt.legend(ncol=2)
    plt.xlabel("Year")
    plt.ylabel("Temperature (°C)")

    plt.tight_layout()
    plt.margins(x=0)

    fig.savefig(os.path.join(result_path, "test.pdf"))
def plot_tot_y_over_time(scenario_data, fig_name, plotshow=False):
    """Plot total demand over simulation period for every
    scenario for all regions
    """
    # Set figure size
    plt.figure(figsize=basic_plot_functions.cm2inch(14, 8))

    y_scenario = {}

    for scenario_name, scen_data in scenario_data.items():

        # Read out fueltype specific max h load
        data_years = {}
        for year, fueltype_reg_time in scen_data['ed_fueltype_regs_yh'].items(
        ):

            # Sum all regions and fueltypes
            tot_gwh_fueltype_y = np.sum(fueltype_reg_time)

            # Convert to TWh
            tot_twh_fueltype_y = conversions.gwh_to_twh(tot_gwh_fueltype_y)

            data_years[year] = tot_twh_fueltype_y

        y_scenario[scenario_name] = data_years

    # -----------------
    # Axis
    # -----------------
    base_yr, year_interval = 2015, 5
    first_scen = list(y_scenario.keys())[0]
    end_yr = list(y_scenario[first_scen].keys())

    major_ticks = np.arange(base_yr, end_yr[-1] + year_interval, year_interval)

    plt.xticks(major_ticks, major_ticks)

    # ----------
    # Plot lines
    # ----------
    # color_list_selection = plotting_styles.color_list_selection()
    color_list_selection = plotting_styles.color_list_scenarios()

    for scenario_name, fuel_fueltype_yrs in y_scenario.items():

        plt.plot(
            list(fuel_fueltype_yrs.keys()),  # years
            list(fuel_fueltype_yrs.values()),  # yearly data per fueltype
            color=str(color_list_selection.pop()),
            label=scenario_name)

    # ----
    # Axis
    # ----
    plt.ylim(ymin=0)

    # ------------
    # Plot legend
    # ------------
    plt.legend(ncol=1,
               loc=3,
               prop={
                   'family': 'arial',
                   'size': 10
               },
               frameon=False)

    # ---------
    # Labels
    # ---------
    plt.ylabel("TWh")
    plt.xlabel("year")
    plt.title("tot y ED all fueltypes")

    # Tight layout
    plt.tight_layout()
    plt.margins(x=0)

    plt.savefig(fig_name)

    if plotshow:
        plt.show()

    plt.close()
Exemplo n.º 30
0
def fueltypes_over_time(scenario_result_container,
                        sim_yrs,
                        fig_name,
                        fueltypes,
                        result_path,
                        plot_points=False,
                        unit='TWh',
                        crit_smooth_line=True,
                        seperate_legend=False):
    """Plot fueltypes over time
    """
    statistics_to_print = []

    fig = plt.figure(figsize=basic_plot_functions.cm2inch(10,
                                                          10))  #width, height
    ax = fig.add_subplot(1, 1, 1)

    colors = {
        # Low elec
        'electricity': '#3e3838',
        'gas': '#ae7c7c',
        'hydrogen': '#6cbbb3',
    }

    line_styles_default = plotting_styles.linestyles()

    linestyles = {
        'h_max': line_styles_default[0],
        'h_min': line_styles_default[6],
        'l_min': line_styles_default[8],
        'l_max': line_styles_default[9],
    }

    for cnt_scenario, i in enumerate(scenario_result_container):
        scenario_name = i['scenario_name']

        for cnt_linestyle, fueltype_str in enumerate(fueltypes):
            national_sum = i['national_{}'.format(fueltype_str)]

            if unit == 'TWh':
                unit_factor = conversions.gwh_to_twh(1)
            elif unit == 'GWh':
                unit_factor = 1
            else:
                raise Exception("Wrong unit")

            national_sum = national_sum * unit_factor

            # Calculate quantiles
            quantile_95 = 0.95
            quantile_05 = 0.05

            try:
                color = colors[fueltype_str]
            except KeyError:
                #color = list(colors.values())[cnt_linestyle]
                raise Exception("Wrong color")

            try:
                linestyle = linestyles[scenario_name]
            except KeyError:
                linestyle = list(linestyles.values())[cnt_scenario]
            try:
                marker = marker_styles[scenario_name]
            except KeyError:
                marker = list(marker_styles.values())[cnt_scenario]

            # Calculate average across all weather scenarios
            mean_national_sum = national_sum.mean(axis=0)
            mean_national_sum_sim_yrs = copy.copy(mean_national_sum)

            statistics_to_print.append(
                "{} fueltype_str: {} mean_national_sum_sim_yrs: {}".format(
                    scenario_name, fueltype_str, mean_national_sum_sim_yrs))

            # Standard deviation over all realisations
            df_q_05 = national_sum.quantile(quantile_05)
            df_q_95 = national_sum.quantile(quantile_95)

            statistics_to_print.append(
                "{} fueltype_str: {} df_q_05: {}".format(
                    scenario_name, fueltype_str, df_q_05))
            statistics_to_print.append(
                "{} fueltype_str: {} df_q_95: {}".format(
                    scenario_name, fueltype_str, df_q_95))
            # --------------------
            # Try to smooth lines
            # --------------------
            sim_yrs_smoothed = sim_yrs
            if crit_smooth_line:
                try:
                    sim_yrs_smoothed, mean_national_sum_smoothed = basic_plot_functions.smooth_data(
                        sim_yrs, mean_national_sum, num=500)
                    _, df_q_05_smoothed = basic_plot_functions.smooth_data(
                        sim_yrs, df_q_05, num=500)
                    _, df_q_95_smoothed = basic_plot_functions.smooth_data(
                        sim_yrs, df_q_95, num=500)

                    mean_national_sum = pd.Series(mean_national_sum_smoothed,
                                                  sim_yrs_smoothed)
                    df_q_05 = pd.Series(df_q_05_smoothed, sim_yrs_smoothed)
                    df_q_95 = pd.Series(df_q_95_smoothed, sim_yrs_smoothed)
                except:
                    print("did not owrk {} {}".format(fueltype_str,
                                                      scenario_name))
                    pass

            # ------------------------
            # Plot lines
            # ------------------------
            plt.plot(mean_national_sum,
                     label="{} {}".format(fueltype_str, scenario_name),
                     linestyle=linestyle,
                     color=color,
                     zorder=1,
                     clip_on=True)

            # ------------------------
            # Plot markers
            # ------------------------
            if plot_points:
                plt.scatter(sim_yrs,
                            mean_national_sum_sim_yrs,
                            marker=marker,
                            edgecolor='black',
                            linewidth=0.5,
                            c=color,
                            zorder=2,
                            s=15,
                            clip_on=False)  #do not clip points on axis

            # Plottin qunatilse and average scenario
            df_q_05.plot.line(color=color,
                              linestyle='--',
                              linewidth=0.1,
                              label='_nolegend_')  #, label="0.05")
            df_q_95.plot.line(color=color,
                              linestyle='--',
                              linewidth=0.1,
                              label='_nolegend_')  #, label="0.05")

            plt.fill_between(
                sim_yrs_smoothed,
                list(df_q_95),  #y1
                list(df_q_05),  #y2
                alpha=0.25,
                facecolor=color,
            )

    plt.xlim(2015, 2050)
    plt.ylim(0)

    ax = plt.gca()

    # Major ticks every 20, minor ticks every 5
    major_ticks = [200, 400, 600]  #np.arange(0, 600, 200)
    minor_ticks = [100, 200, 300, 400, 500, 600]  #np.arange(0, 600, 100)

    ax.set_yticks(major_ticks)
    ax.set_yticks(minor_ticks, minor=True)

    # And a corresponding grid
    ax.grid(which='both',
            color='black',
            linewidth=0.5,
            axis='y',
            linestyle=line_styles_default[3])  #[6])

    # Or if you want different settings for the grids:
    ax.grid(which='minor', axis='y', alpha=0.4)
    ax.grid(which='major', axis='y', alpha=0.8)

    # Achsen
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['top'].set_visible(False)

    # Ticks
    plt.tick_params(
        axis='y',  # changes apply to the x-axis
        which='both',  # both major and minor ticks are affected
        bottom=False,  # ticks along the bottom edge are off
        top=False,  # ticks along the top edge are off
        left=False,
        right=False,
        labelbottom=False,
        labeltop=False,
        labelleft=True,
        labelright=False)  # labels along the bottom edge are off

    # --------
    # Legend
    # --------
    legend = plt.legend(
        #title="tt",
        ncol=2,
        prop={'size': 6},
        loc='upper center',
        bbox_to_anchor=(0.5, -0.1),
        frameon=False)
    legend.get_title().set_fontsize(8)

    if seperate_legend:
        basic_plot_functions.export_legend(
            legend, os.path.join(result_path,
                                 "{}__legend.pdf".format(fig_name)))
        legend.remove()

    # --------
    # Labeling
    # --------
    plt.ylabel("national fuel over time [in {}]".format(unit))
    #plt.xlabel("year")
    #plt.title("Title")

    plt.tight_layout()
    #plt.show()
    plt.savefig(os.path.join(result_path, fig_name))
    plt.close()

    write_data.write_list_to_txt(
        os.path.join(result_path, fig_name).replace(".pdf", ".txt"),
        statistics_to_print)