Skip to content

Commit

Permalink
improved spider plot
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Mar 21, 2018
1 parent 3c8a114 commit 0f86889
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 79 deletions.
133 changes: 70 additions & 63 deletions energy_demand/plotting/plotting_multiple_scenarios.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""
This file containes functions to plot multiple scenarios in a folder
"""
import os
import operator
import numpy as np
import matplotlib.pyplot as plt
from energy_demand.plotting import plotting_styles
from energy_demand.plotting import plotting_program
from energy_demand.basic import conversions
from energy_demand.plotting import plotting_results
from energy_demand.basic import lookup_tables
from energy_demand.technologies import tech_related

def plot_reg_y_over_time(
scenario_data,
Expand Down Expand Up @@ -211,69 +214,73 @@ def plot_radar_plots_average_peak_day(
scenario_data,
year_to_plot,
fig_name,
plotshow):
"""Compare averaged dh profile overall regions for peak day
"""

# Scenarios
all_scenarios = list(scenario_data.keys())
first_scenario = str(all_scenarios[:1][0])

# ----------------
# Create base year peak load profile
# -----------------
all_regs_fueltypes_yh_by = {}
for fueltype, fuels_regs in enumerate(scenario_data[first_scenario]['results_every_year'][2015]):
for region_array_nr, fuel_reg in enumerate(fuels_regs):
try:
all_regs_fueltypes_yh_by[fueltype] += fuel_reg
except KeyError:
all_regs_fueltypes_yh_by[fueltype] = fuel_reg

try:
all_fuels += fuel_reg
except:
all_fuels = fuel_reg

# Future year
all_regs_fueltypes_yh_ey = {}
for fueltype, fuels_regs in enumerate(scenario_data[first_scenario]['results_every_year'][year_to_plot]):
for region_array_nr, fuel_reg in enumerate(fuels_regs):
try:
all_regs_fueltypes_yh_ey[fueltype] += fuel_reg
except KeyError:
all_regs_fueltypes_yh_ey[fueltype] = fuel_reg

# -----------
# get peak day across all fueltypes
# -----------
for fueltype in all_regs_fueltypes_yh_by.keys():
all_regs_fueltypes_yh_by[fueltype].reshape((365, 24))
for fueltype in all_regs_fueltypes_yh_ey.keys():
all_regs_fueltypes_yh_ey[fueltype].reshape((365, 24))

all_fuels_d = all_fuels.np.reshape((365, 24))

peak_day_nr = np.argmax(all_fuels_d, axis=0)

print("peak_day_nr" + str(peak_day_nr))

#fueltype_to_plot = 'electricity']
for fueltype, fuels_regs in all_regs_fueltypes_yh_by.items():

name_spider_plot = "spider_{}".format(fueltype)
# ----------------------------------
# Plot dh for peak day for base year
# ----------------------------------
individ_radars_to_plot_dh = [
list(all_regs_fueltypes_yh_by[fueltype][peak_day_nr]),
list(all_regs_fueltypes_yh_ey[peak_day_nr])]

plotting_results.plot_radar_plot_multiple_lines(
individ_radars_to_plot_dh,
name_spider_plot,
plot_steps=20,
plotshow=False)
plotshow
):
"""Compare averaged dh profile overall regions for peak day
for future year and base year
"""
lookups = lookup_tables.basic_lookups()

# Scenarios
all_scenarios = list(scenario_data.keys())
first_scenario = str(all_scenarios[:1][0])

# ----------------
# Create base year peak load profile
# -----------------
all_regs_fueltypes_yh_by = {}
for fueltype, fuels_regs in enumerate(scenario_data[first_scenario]['results_every_year'][2015]):
for region_array_nr, fuel_reg in enumerate(fuels_regs):
try:
all_regs_fueltypes_yh_by[fueltype] += fuel_reg
except KeyError:
all_regs_fueltypes_yh_by[fueltype] = fuel_reg

try:
all_fuels_by += fuel_reg
except:
all_fuels_by = fuel_reg

# ------------------------
# Future year load profile
# ------------------------
all_regs_fueltypes_yh_ey = {}
for fueltype, fuels_regs in enumerate(scenario_data[first_scenario]['results_every_year'][year_to_plot]):
for region_array_nr, fuel_reg in enumerate(fuels_regs):
try:
all_regs_fueltypes_yh_ey[fueltype] += fuel_reg
except KeyError:
all_regs_fueltypes_yh_ey[fueltype] = fuel_reg

# -----------
# get peak day across all fueltypes
# -----------
for fueltype in all_regs_fueltypes_yh_by.keys():
all_regs_fueltypes_yh_by[fueltype] = all_regs_fueltypes_yh_by[fueltype].reshape((365, 24))
for fueltype in all_regs_fueltypes_yh_ey.keys():
all_regs_fueltypes_yh_ey[fueltype] = all_regs_fueltypes_yh_ey[fueltype].reshape((365, 24))

all_fuels_by_d = all_fuels_by.reshape((365, 24))

peak_day_nr = np.argmax(np.sum(all_fuels_by_d, axis=1))

for fueltype in all_regs_fueltypes_yh_by.keys():

fueltype_str = tech_related.get_fueltype_str(lookups['fueltypes'], fueltype)
name_spider_plot = os.path.join(fig_name, "spider_{}_.pdf".format(fueltype_str))

# ----------------------------------
# Plot dh for peak day for base year
# ----------------------------------
individ_radars_to_plot_dh = [
list(all_regs_fueltypes_yh_by[fueltype][peak_day_nr]),
list(all_regs_fueltypes_yh_ey[fueltype][peak_day_nr])]

plotting_results.plot_radar_plot_multiple_lines(
individ_radars_to_plot_dh,
name_spider_plot,
plot_steps=20,
plotshow=False)

def plot_LAD_comparison_scenarios(
scenario_data,
Expand Down
44 changes: 35 additions & 9 deletions energy_demand/plotting/plotting_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -1717,7 +1717,12 @@ def plot_radar_plot(dh_profile, fig_name, plot_steps=30, plotshow=False):
else:
plt.close()

def plot_radar_plot_multiple_lines(dh_profiles, fig_name, plot_steps=30, plotshow=False):
def plot_radar_plot_multiple_lines(
dh_profiles,
fig_name,
plot_steps=30,
plotshow=False
):
"""Plot daily load profile on a radar plot
Arguments
Expand All @@ -1730,12 +1735,14 @@ def plot_radar_plot_multiple_lines(dh_profiles, fig_name, plot_steps=30, plotsho
SOURCE: https://python-graph-gallery.com/390-basic-radar-chart/
"""

# Get maximum demand of first array
max_entry = np.array(dh_profiles[0]).max()
max_demand = round(max_entry, -1) + 10 # Round to nearest 10 plus add 10
max_demand = 120 #SCRAP
# 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

Expand All @@ -1747,8 +1754,14 @@ def plot_radar_plot_multiple_lines(dh_profiles, fig_name, plot_steps=30, plotsho
axis_plots_inner.append(plot_steps*i)
axis_plots_outer.append(str(plot_steps*i))

color_lines = ['grey', 'blue']
years = ['2015', '2050']

# Iterate lines
for dh_profile in dh_profiles:
for cnt, dh_profile in enumerate(dh_profiles):

color_line = color_lines[cnt]
year_line = years[cnt]

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

Expand Down Expand Up @@ -1811,14 +1824,27 @@ def plot_radar_plot_multiple_lines(dh_profiles, fig_name, plot_steps=30, plotsho
angles,
values,
linestyle='--',
linewidth=0.5)
linewidth=0.5,
label="{}".format(year_line))

ax.fill(
angles,
values,
'blue', #b
color_line, #b
alpha=0.1)

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


plt.savefig(fig_name)

if plotshow:
Expand Down
24 changes: 17 additions & 7 deletions energy_demand/processing_multiple_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from energy_demand.basic import date_prop
from energy_demand.plotting import plotting_multiple_scenarios
from energy_demand import processing
from energy_demand.basic import basic_functions

def process_result_multi_scen(path_to_scenarios, path_shapefile_input):
"""Iterate the folders with scenario
Expand Down Expand Up @@ -33,6 +34,10 @@ def process_scenarios(path_to_scenarios, year_to_model=2015):
path_to_scenarios : str
Path to folders with stored results
"""
# Delete folder results if existing
path_result_folder = os.path.join(path_to_scenarios, "results_multiple_scenarios")
basic_functions.delete_folder(path_result_folder)

seasons = date_prop.get_season(
year_to_model=year_to_model)

Expand Down Expand Up @@ -62,46 +67,51 @@ def process_scenarios(path_to_scenarios, year_to_model=2015):
seasons=seasons,
model_yeardays_daytype=model_yeardays_daytype)

# -----------------------
# Generate result folder
# -----------------------
basic_functions.create_folder(path_result_folder)

# ------------
# Create plots
# ------------
# Plot total demand for every year in line plot
plotting_multiple_scenarios.plot_tot_y_over_time(
scenario_data,
fig_name=os.path.join(path_to_scenarios, "tot_y_multiple.pdf"),
fig_name=os.path.join(path_result_folder, "tot_y_multiple.pdf"),
plotshow=True)

# Plot for all regions demand for every year in line plot
plotting_multiple_scenarios.plot_reg_y_over_time(
scenario_data,
fig_name=os.path.join(path_to_scenarios, "reg_y_multiple.pdf"),
fig_name=os.path.join(path_result_folder, "reg_y_multiple.pdf"),
plotshow=True)

# Plot comparison of total demand for a year for all LADs (scatter plot)
plotting_multiple_scenarios.plot_LAD_comparison_scenarios(
scenario_data,
year_to_plot=2050,
fig_name=os.path.join(path_to_scenarios, "LAD_multiple.pdf"),
fig_name=os.path.join(path_result_folder, "LAD_multiple.pdf"),
plotshow=False)

# Plot different profiels in radar plot
plotting_multiple_scenarios.plot_radar_plots_average_peak_day(
scenario_data,
year_to_plot=2050,
fig_name=os.path.join(path_to_scenarios, "radar_average.pdf"),
fig_name=os.path.join(path_result_folder),
plotshow=False)
return

# ------------------------------------
# Execute rusult processing for every scenario
# ------------------------------------^
#'''
'''
process_result_multi_scen(
os.path.abspath("C:/Users/cenv0553/nismod/data_energy_demand/_MULT2"),
os.path.abspath('C:/Users/cenv0553/nismod/data_energy_demand/_raw_data/C_LAD_geography/same_as_pop_scenario/lad_2016_uk_simplified.shp'))
#'''
'''

# ------------------------------------
# Generate plots across all scenarios
# ------------------------------------
#process_scenarios(os.path.abspath("C:/Users/cenv0553/nismod/data_energy_demand/_MULT2"))
process_scenarios(os.path.abspath("C:/Users/cenv0553/nismod/data_energy_demand/_MULT2"))

0 comments on commit 0f86889

Please sign in to comment.