Skip to content

Commit

Permalink
improving plots and saving legends externally
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Dec 18, 2018
1 parent fe07c03 commit 1d8adc7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 71 deletions.
30 changes: 12 additions & 18 deletions energy_demand/plotting/basic_plot_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
from energy_demand.technologies import diffusion_technologies
#matplotlib.use('Agg') # Used to make it work in linux

def export_legend(legend, filename="legend.png"):
"""Export legend as seperate file
"""
fig = legend.figure
fig.canvas.draw()
bbox = legend.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
fig.savefig(filename, dpi="figure", bbox_inches=bbox)

def cm2inch(*tupl):
"""Convert input cm to inches (width, hight)
"""
Expand Down Expand Up @@ -86,9 +94,6 @@ def smooth_data(
max_x_val,
num=num,
endpoint=True)

#y_smooth = f2(x_smooth)

else:
# Smooth x data
x_smooth = np.linspace(
Expand All @@ -104,26 +109,15 @@ def smooth_data(
# smooth
y_smooth = f2(x_smooth)

# Prevent smoothing to go into negative values
# and replace negative values with zero
#y_smooth[y_smooth < 0] = 0
# Prevent smoothing to go into negative values and replace negative values with zero

# Get position of first 0
# Get position of last negative entry
try:
cnt = 0
for i in y_smooth:
print(i)
if i == 0:
tt = cnt
cnt += 1
print(sum(y_smooth))
print(tt)
pos_zero = int(np.argwhere(y_smooth == 0)[-1])
pos_zero = int(np.argwhere(y_smooth < 0)[-1])
y_smooth[:pos_zero] = 0
print("Position " + str(pos_zero))
print(sum(y_smooth))
except IndexError:
pass

return x_smooth, y_smooth

def smooth_line(
Expand Down
114 changes: 61 additions & 53 deletions energy_demand/plotting/fig_3_plot_over_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import os
import pandas as pd
import matplotlib.pyplot as plt
import copy
import pylab

from energy_demand.plotting import basic_plot_functions
from energy_demand.basic import conversions
Expand Down Expand Up @@ -47,7 +49,7 @@ def scenario_over_time(
"""Plot peak over time
"""
fig = plt.figure(
figsize=basic_plot_functions.cm2inch(9, 8)) #width, height
figsize=basic_plot_functions.cm2inch(10, 10)) #width, height

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

Expand All @@ -71,24 +73,12 @@ def scenario_over_time(

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

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

# ------------------------
# Plot calculated data points
# ------------------------
if plot_points:

plt.scatter(
sim_yrs,
mean_national_peak,
c=color,
marker=marker,
s=15,
clip_on=False) #do not clip points on axis

# --------------------
# Try to smooth lines
# --------------------
Expand All @@ -105,11 +95,30 @@ def scenario_over_time(
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")
Expand Down Expand Up @@ -153,20 +162,16 @@ def scenario_over_time(
legend = plt.legend(
#title="tt",
ncol=2,
prop={'size': 8},
prop={'size': 10},
loc='upper center',
bbox_to_anchor=(0.5, -0.1),
frameon=False)
legend.get_title().set_fontsize(8)

# Put a legend to the right of the current axis
'''ax.legend(
ncol=1,
prop={'size': 8},
loc='center left',
bbox_to_anchor=(1, 0.5),
frameon=False)
#legend.get_title().set_fontsize(8)'''
basic_plot_functions.export_legend(
legend,
os.path.join(result_path, "legend_{}.pdf".format(fig_name)))
legend.remove()

# --------
# Labeling
Expand All @@ -176,7 +181,7 @@ def scenario_over_time(
#plt.title("Title")

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

Expand All @@ -193,7 +198,7 @@ def fueltypes_over_time(
"""Plot fueltypes over time
"""
fig = plt.figure(
figsize=basic_plot_functions.cm2inch(9, 8)) #width, height
figsize=basic_plot_functions.cm2inch(10, 10)) #width, height
ax = fig.add_subplot(1, 1, 1)

colors = {
Expand Down Expand Up @@ -247,6 +252,7 @@ def fueltypes_over_time(

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

if fueltype_str == 'hydrogen':
print("..")
Expand All @@ -255,43 +261,49 @@ def fueltypes_over_time(
df_q_05 = national_sum.quantile(quantile_05)
df_q_95 = national_sum.quantile(quantile_95)

# ------------------------
# Plot calculated data points
# ------------------------
if plot_points:

plt.scatter(
sim_yrs,
mean_national_sum,
marker=marker,
c=color,
s=15,
clip_on=False) #do not clip points on axis

# --------------------
# 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=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)
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")
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")
Expand Down Expand Up @@ -358,15 +370,11 @@ def fueltypes_over_time(
bbox_to_anchor=(0.5, -0.1),
frameon=False)
legend.get_title().set_fontsize(8)

# Put a legend to the right of the current axis
'''ax.legend(
ncol=1,
prop={'size': 8},
loc='center left',
bbox_to_anchor=(1, 0.5),
frameon=False)
#legend.get_title().set_fontsize(8)'''

basic_plot_functions.export_legend(
legend,
os.path.join(result_path, "legend_{}.pdf".format(fig_name)))
legend.remove()

# --------
# Labeling
Expand All @@ -376,6 +384,6 @@ def fueltypes_over_time(
#plt.title("Title")

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

0 comments on commit 1d8adc7

Please sign in to comment.