Plots world state from an instance of World3 or any single sector.
(time, var_data, var_names, var_lims,
img_background=None,
title=None,
figsize=None,
dist_spines=0.09,
grid=False)
| 86 | |
| 87 | |
| 88 | def plot_world_variables(time, var_data, var_names, var_lims, |
| 89 | img_background=None, |
| 90 | title=None, |
| 91 | figsize=None, |
| 92 | dist_spines=0.09, |
| 93 | grid=False): |
| 94 | """ |
| 95 | Plots world state from an instance of World3 or any single sector. |
| 96 | |
| 97 | """ |
| 98 | prop_cycle = plt.rcParams['axes.prop_cycle'] |
| 99 | colors = prop_cycle.by_key()['color'] |
| 100 | |
| 101 | var_number = len(var_data) |
| 102 | |
| 103 | fig, host = plt.subplots(figsize=figsize) |
| 104 | axs = [host, ] |
| 105 | for i in range(var_number-1): |
| 106 | axs.append(host.twinx()) |
| 107 | |
| 108 | fig.subplots_adjust(left=dist_spines*2) |
| 109 | for i, ax in enumerate(axs[1:]): |
| 110 | ax.spines["left"].set_position(("axes", -(i + 1)*dist_spines)) |
| 111 | ax.spines["left"].set_visible(True) |
| 112 | ax.yaxis.set_label_position('left') |
| 113 | ax.yaxis.set_ticks_position('left') |
| 114 | |
| 115 | if img_background is not None: |
| 116 | im = imread(img_background) |
| 117 | axs[0].imshow(im, aspect="auto", |
| 118 | extent=[time[0], time[-1], |
| 119 | var_lims[0][0], var_lims[0][1]], cmap="gray") |
| 120 | |
| 121 | ps = [] |
| 122 | for ax, label, ydata, color in zip(axs, var_names, var_data, colors): |
| 123 | ps.append(ax.plot(time, ydata, label=label, color=color)[0]) |
| 124 | axs[0].grid(grid) |
| 125 | axs[0].set_xlim(time[0], time[-1]) |
| 126 | |
| 127 | for ax, lim in zip(axs, var_lims): |
| 128 | ax.set_ylim(lim[0], lim[1]) |
| 129 | |
| 130 | for ax_ in axs: |
| 131 | formatter_ = EngFormatter(places=0, sep="\N{THIN SPACE}") |
| 132 | ax_.tick_params(axis='y', rotation=90) |
| 133 | ax_.yaxis.set_major_locator(plt.MaxNLocator(5)) |
| 134 | ax_.yaxis.set_major_formatter(formatter_) |
| 135 | |
| 136 | tkw = dict(size=4, width=1.5) |
| 137 | axs[0].set_xlabel("time [years]") |
| 138 | axs[0].tick_params(axis='x', **tkw) |
| 139 | for i, (ax, p) in enumerate(zip(axs, ps)): |
| 140 | ax.set_ylabel(p.get_label(), rotation="horizontal") |
| 141 | ax.yaxis.label.set_color(p.get_color()) |
| 142 | ax.tick_params(axis='y', colors=p.get_color(), **tkw) |
| 143 | ax.yaxis.set_label_coords(-i*dist_spines, 1.01) |
| 144 | |
| 145 | if title is not None: |
no outgoing calls
no test coverage detected