Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Plotting methods"""
2import matplotlib.pyplot as plt
3import numpy as np
4from .plot_sweep import assign_axes
5from .. import GPCOLORS
8def compare(models, sweeps, posys, tol=0.001):
9 """Compares the values of posys over a sweep of several models.
11 If posys is of the same length as models, this will plot different
12 variables from different models.
14 Currently only supports a single sweepvar.
16 Example Usage:
17 compare([aec, fbc], {"R": (160, 300)},
18 ["cost", ("W_{\\rm batt}", "W_{\\rm fuel}")], tol=0.001)
19 """
20 sols = [m.autosweep(sweeps, tol, verbosity=0) for m in models]
21 posys, axes = assign_axes(sols[0].bst.sweptvar, posys, None)
22 for posy, ax in zip(posys, axes):
23 for i, sol in enumerate(sols):
24 if hasattr(posy, "__len__") and len(posy) == len(sols):
25 p = posy[i]
26 else:
27 p = posy
28 color = GPCOLORS[i % len(GPCOLORS)]
29 if sol._is_cost(p): # pylint: disable=protected-access
30 ax.fill_between(sol.sampled_at,
31 sol.cost_lb(), sol.cost_ub(),
32 facecolor=color, edgecolor=color,
33 linewidth=0.75)
34 else:
35 ax.plot(sol.sampled_at, sol(p), color=color)
38def plot_convergence(model):
39 """Plots the convergence of a signomial programming model
41 Arguments
42 ---------
43 model: Model
44 Signomial programming model that has already been solved
46 Returns
47 -------
48 matplotlib.pyplot Figure
49 Plot of cost as functions of SP iteration #
50 """
51 fig, ax = plt.subplots()
53 it = np.array([])
54 cost = np.array([])
55 for n in range(len(model.program.gps)):
56 try:
57 cost = np.append(cost, model.program.gps[n].result['cost'])
58 it = np.append(it, n+1)
59 except TypeError:
60 pass
61 ax.plot(it, cost, '-o')
62 ax.set_xlabel('Iteration')
63 ax.set_ylabel('Cost')
64 ax.set_xticks(range(1, len(model.program.gps)+1))
65 return fig, ax