Skip to content

Commit

Permalink
improve plot functions in routeparams (e.g. add mean for power vs. dist)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdemmich committed Jan 30, 2024
1 parent c88d1e8 commit 2744197
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
33 changes: 30 additions & 3 deletions WeatherRoutingTool/routeparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,27 +283,54 @@ def get_power_type(self, power_type):
if power_type == 'fuel':
return {"value": self.get_fuel_per_dist(), "label": "Treibstoffverbrauch", "unit": u.kg}

def plot_power_vs_dist(self, color, label, power_type):
def plot_power_vs_dist(self, color, label, power_type, ax):
power = self.get_power_type(power_type)
dist = self.dists_per_step

hist_values = graphics.get_hist_values_from_widths(dist, power["value"], power_type)

# only for power: also plot bin showing weighted mean. This does not make sense for fuel.
if power_type == 'power':
plt.ylabel(power["label"] + ' (kW)')
weighted_mean = power["value"] * dist
weighted_mean = weighted_mean.sum() / dist.sum()

# append empty bin
hist_values["bin_centres"] = np.append(hist_values["bin_centres"], 3750 * u.km)
hist_values["bin_contents"] = np.append(hist_values["bin_contents"], -1 * weighted_mean)
hist_values["bin_widths"] = np.append(hist_values["bin_widths"], 250 * u.km)

# append mean bin
hist_values["bin_centres"] = np.append(hist_values["bin_centres"], 4000 * u.km)
hist_values["bin_contents"] = np.append(hist_values["bin_contents"], weighted_mean)
hist_values["bin_widths"] = np.append(hist_values["bin_widths"], 150 * u.km)

# plt.ylabel(power["label"] + ' (kW)')
plt.ylabel(power["label"])
plt.bar(
hist_values["bin_centres"].to(u.km).value,
hist_values["bin_contents"].to(u.kiloWatt).value,
hist_values["bin_widths"].to(u.km).value,
fill=False, color=color, edgecolor=color, label=label)
ax.set_ylim(0, 6000)

# customise labels
labels = ax.get_xticks().tolist()
for i in range(0, len(labels)):
labels[i] = int(labels[i])
labels[-2] = 'Mittelwert'
ax.set_xticklabels(labels)

ax.set_xlim(-100, 4499)
else:
plt.ylabel(power["label"] + ' (t/km)')
# plt.ylabel(power["label"] + ' (t/km)')
plt.ylabel(power["label"])
plt.bar(
hist_values["bin_centres"].to(u.km).value,
hist_values["bin_contents"].to(u.tonne/u.kilometer).value,
hist_values["bin_widths"].to(u.km).value,
fill=False, color=color, edgecolor=color, label=label
)

plt.xlabel('Weglänge (km)')
plt.xticks()

Expand Down
4 changes: 2 additions & 2 deletions WeatherRoutingTool/utils/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def plot_legend(fig):


def get_colour(i):
colours = ['darkred', 'gold', 'seagreen', 'peachpuff', 'darkviolet', 'crimson', 'olive',
'skyblue', 'moccasin', 'plum', 'firebrick']
colours = ['darkred', 'mediumblue', 'olive', 'peachpuff', 'seagreen', 'gold', 'darkviolet', 'crimson',
'moccasin', 'plum', 'firebrick', 'skyblue']
if (i > 10):
i = i-10
print('Currently only 11 different colours available. Will use one that has already been used before: '
Expand Down
30 changes: 18 additions & 12 deletions compare_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
from WeatherRoutingTool.weather_factory import WeatherFactory


def plot_power_vs_dist(rp_list, rp_str_list, power_type='fuel'):
def plot_power_vs_dist(rp_list, rp_str_list, scenario_str, power_type='fuel'):
fig, ax = plt.subplots(figsize=(12, 8), dpi=96)
for irp in range(0, len(rp_list)):
rp_list[irp].plot_power_vs_dist(graphics.get_colour(irp), rp_str_list[irp], power_type)
rp_list[irp].plot_power_vs_dist(graphics.get_colour(irp), rp_str_list[irp], power_type, ax)

ax.legend(loc='lower left')
# ax.set_ylim(0, 0.016)
ax.legend(loc='upper left', frameon=False)
# ax.tick_params(top=True, right=True)
ax.tick_params(labelleft=False, left=False, top=True) # hide y labels
ax.text(0.95, 0.96, scenario_str, verticalalignment='top', horizontalalignment='right',
transform=ax.transAxes)
plt.savefig(figurefile + '/' + power_type + '_vs_dist.png')


Expand All @@ -26,7 +29,7 @@ def plot_acc_power_vs_dist(rp_list, rp_str_list, power_type='fuel'):
for irp in range(0, len(rp_list)):
rp_list[irp].plot_acc_power_vs_dist(graphics.get_colour(irp), rp_str_list[irp], power_type)

ax.legend(loc='upper left')
ax.legend(loc='upper center')
# ax.set_ylim(0, 0.016)
plt.savefig(figurefile + '/' + power_type + 'acc_vs_dist.png')

Expand All @@ -50,7 +53,8 @@ def plot_power_vs_dist_ratios(rp_list, rp_str_list, power_type='fuel'):
rp_list[irp].plot_power_vs_dist_ratios(rp_list[0], graphics.get_colour(irp),
rp_str_list[irp] + '/' + rp_str_list[0], power_type)

ax.legend(loc='lower left')
ax.legend(loc='upper center', frameon=False)
ax.tick_params(top=True, right=True)
# ax.set_title('Windige Wetterlage (Windgeschwindigkeit: ' + windspeed + ' m/s)')
plt.savefig(figurefile + '/' + power_type + '_vs_dist_ratios' + '.png')

Expand All @@ -76,17 +80,19 @@ def plot_power_vs_dist_ratios(rp_list, rp_str_list, power_type='fuel'):
# rp_read3 = RouteParams.from_file(filename3)
# rp_read4 = RouteParams.from_file(filename4)

rp_1_str = 'Original CBT'
rp_2_str = '80% Zusatzwiderstand Seegang'
rp_1_str = 'Originalroute'
rp_2_str = 'Isofuel-Routenfindung'
rp_3_str = '120% Zusatzwiderstand Seegang'
rp_4_str = 'original'

scenario_str = 'Mittelmeer-Szenario'

rp_list = [rp_read1, rp_read2]
rp_str_list = [rp_1_str, rp_2_str]

do_plot_weather = False
do_plot_route = True
do_plot_power_vs_dist = False
do_plot_power_vs_dist = True
do_plot_fuel_vs_dist = False
do_plot_acc_fuel_vs_dist = False

Expand All @@ -96,7 +102,7 @@ def plot_power_vs_dist_ratios(rp_list, rp_str_list, power_type='fuel'):
do_plot_fuel_vs_lat = False

do_plot_power_vs_dist_showing_weather = False
do_plot_power_vs_dist_ratios = False
do_plot_power_vs_dist_ratios = True
do_plot_fuel_vs_dist_ratios = False
do_write_fuel = True

Expand Down Expand Up @@ -141,10 +147,10 @@ def plot_power_vs_dist_ratios(rp_list, rp_str_list, power_type='fuel'):
##
# plotting vs. distance
if do_plot_power_vs_dist:
plot_power_vs_dist(rp_list, rp_str_list, 'power')
plot_power_vs_dist(rp_list, rp_str_list, scenario_str, 'power')

if do_plot_fuel_vs_dist:
plot_power_vs_dist(rp_list, rp_str_list, 'fuel')
plot_power_vs_dist(rp_list, rp_str_list, scenario_str, 'fuel')

##
# plotting accumulated vs. distance
Expand Down
4 changes: 3 additions & 1 deletion tests/test_graphics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime

import numpy as np
import matplotlib.pyplot as plt
from astropy import units as u

import WeatherRoutingTool.utils.graphics as graphics
Expand Down Expand Up @@ -35,7 +36,8 @@ def test_plot_power_vs_dist():
dists_per_step=dist_per_step * u.meter,
starttime_per_step=time_per_step,
ship_params_per_step=sp, gcr=dummy_list)
route.plot_power_vs_dist("orange", "Route X", "fuel")
fig, ax = plt.subplots(figsize=(12, 8), dpi=96)
route.plot_power_vs_dist("orange", "Route X", "fuel", ax)


def test_get_accumulated_dist():
Expand Down

0 comments on commit 2744197

Please sign in to comment.