Coverage for gpkit/interactive/plot_sweep.py: 7%

67 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 16:49 -0500

1"Implements plot_sweep1d function" 

2import matplotlib.pyplot as plt 

3from ..exceptions import InvalidGPConstraint 

4 

5 

6def assign_axes(var, posys, axes): 

7 "Assigns axes to posys, creating and formatting if necessary" 

8 if not hasattr(posys, "__iter__"): 

9 posys = [posys] 

10 N = len(posys) 

11 if axes is None: 

12 _, axes = plt.subplots(N, 1, sharex="col", figsize=(4.5, 3+1.5*N)) 

13 if N == 1: 

14 axes = [axes] 

15 format_and_label_axes(var, posys, axes) 

16 elif N == 1 and not hasattr(axes, "__len__"): 

17 axes = [axes] 

18 return posys, axes 

19 

20 

21def format_and_label_axes(var, posys, axes, ylabel=True): 

22 "Formats and labels axes" 

23 for posy, ax in zip(posys, axes): 

24 if ylabel: 

25 if hasattr(posy, "key"): 

26 ylabel = (posy.key.descr.get("label", posy.key.name) 

27 + " [%s]" % posy.key.unitstr(dimless="-")) 

28 else: 

29 ylabel = str(posy) 

30 ax.set_ylabel(ylabel) 

31 ax.grid(color="0.6") 

32 # ax.set_frame_on(False) 

33 for item in [ax.xaxis.label, ax.yaxis.label]: 

34 item.set_fontsize(12) 

35 for item in ax.get_xticklabels() + ax.get_yticklabels(): 

36 item.set_fontsize(9) 

37 ax.tick_params(length=0) 

38 ax.spines['left'].set_visible(False) 

39 ax.spines['top'].set_visible(False) 

40 for i in ax.spines.values(): 

41 i.set_linewidth(0.6) 

42 i.set_color("0.6") 

43 i.set_linestyle("dotted") 

44 xlabel = (var.key.descr.get("label", var.key.name) 

45 + " [%s]" % var.key.unitstr(dimless="-")) 

46 ax.set_xlabel(xlabel) # pylint: disable=undefined-loop-variable 

47 plt.locator_params(nbins=4) 

48 plt.subplots_adjust(wspace=0.15) 

49 

50 

51# pylint: disable=too-many-locals,too-many-branches,too-many-statements 

52def plot_1dsweepgrid(model, sweeps, posys, origsol=None, tol=0.01, **solveargs): 

53 """Creates and plots a sweep from an existing model 

54 

55 Example usage: 

56 f, _ = plot_sweep_1d(m, {'x': np.linspace(1, 2, 5)}, 'y') 

57 f.savefig('mysweep.png') 

58 """ 

59 origsubs = {swept: model.substitutions[swept] for swept in sweeps 

60 if swept in model.substitutions} 

61 if origsubs and not origsol: 

62 try: 

63 origsol = model.solve(**solveargs) 

64 except InvalidGPConstraint: 

65 origsol = model.localsolve(**solveargs) 

66 if not hasattr(posys, "__iter__"): 

67 posys = [posys] 

68 

69 N, S = len(posys), len(sweeps) 

70 f, axes = plt.subplots(N, S, sharex='col', sharey='row', 

71 figsize=(4+2*S, 4+2*N)) 

72 plt.subplots_adjust(hspace=0.15) 

73 

74 for i, (swept, swept_over) in enumerate(sweeps.items()): 

75 if isinstance(swept_over, tuple) and len(swept_over) == 2: 

76 sol = model.autosweep({swept: swept_over}, tol=tol, **solveargs) 

77 else: 

78 sol = model.sweep({swept: swept_over}, **solveargs) 

79 

80 if len(sweeps) == 1: 

81 if len(posys) == 1: 

82 subaxes = [axes] 

83 else: 

84 subaxes = axes 

85 elif len(posys) == 1: 

86 subaxes = [axes[i]] 

87 else: 

88 subaxes = axes[:, i] 

89 

90 sol.plot(posys, subaxes) 

91 if origsubs: 

92 for posy, ax in zip(posys, subaxes): 

93 ax.plot(origsubs[swept], origsol(posy), "ko", markersize=4) 

94 format_and_label_axes(swept, posys, subaxes, ylabel=(i == 0)) 

95 model.substitutions.update(origsubs) 

96 

97 return f, axes