diff --git a/mlair/plotting/postprocessing_plotting.py b/mlair/plotting/postprocessing_plotting.py index 6ceb7b30b8f37b5a0d06efb0f201979040ffdb5d..22e55220c6aa54f8352435cc5f5ddaf4f072f0b7 100644 --- a/mlair/plotting/postprocessing_plotting.py +++ b/mlair/plotting/postprocessing_plotting.py @@ -1439,6 +1439,18 @@ class PlotSeasonalMSEStack(AbstractPlotClass): sampling_letter = {"hourly": "H", "daily": "d"}.get(sampling[pos], "") return sampling, sampling_letter + @staticmethod + def _set_bar_label(ax): + opts = {} + sum = {} + for c in ax.containers: + labels = [v for v in c.datavalues] + opts[c] = labels + sum = {i: sum.get(i, 0) + l for (i, l) in enumerate(labels)} + for c, labels in opts.items(): + _l = [f"{round(100 * labels[i] / sum[i])}%" for i in range(len(labels))] + ax.bar_label(c, labels=_l, label_type='center') + def _plot(self, dim, split_ahead=True, sampling="daily", orientation="vertical"): _, sampling_letter = self._get_target_sampling(sampling, 1) if split_ahead is True: @@ -1449,36 +1461,40 @@ class PlotSeasonalMSEStack(AbstractPlotClass): data.to_pandas().T.plot.bar(ax=ax, stacked=True, cmap="Dark2", legend=False) ax.xaxis.label.set_visible(False) ax.set_ylabel(f"{self.error_measure} (in {self.error_unit})") + self._set_bar_label(ax) else: m = data.to_pandas().T.shape[0] fig, ax = plt.subplots(1, 1, figsize=(6, m)) data.to_pandas().T.plot.barh(ax=ax, stacked=True, cmap="Dark2", legend=False) ax.yaxis.label.set_visible(False) ax.set_xlabel(f"{self.error_measure} (in {self.error_unit})") + self._set_bar_label(ax) fig.legend(*ax.get_legend_handles_labels(), loc="upper center", ncol=4) fig.tight_layout(rect=[0, 0, 1, 0.9]) else: self.plot_name = self.plot_name_orig + "_" + orientation data = self._data n = len(data.coords[dim]) + m = data.max(self.season_dim).shape if orientation == "vertical": - fig, ax = plt.subplots(1, n, sharey=True) + fig, ax = plt.subplots(1, n, sharey=True, figsize=(np.prod(m) / 0.8, 5)) for i, sel in enumerate(data.coords[dim].values): data.sel({dim: sel}).to_pandas().T.plot.bar(ax=ax[i], stacked=True, cmap="Dark2", legend=False) label = str(sel) + sampling_letter ax[i].set_title(label) ax[i].xaxis.label.set_visible(False) + self._set_bar_label(ax[i]) ax[0].set_ylabel(f"{self.error_measure} (in {self.error_unit})") fig.legend(*ax[0].get_legend_handles_labels(), loc="upper center", ncol=4) fig.tight_layout(rect=[0, 0, 1, 0.9]) else: - m = data.max(self.season_dim).shape fig, ax = plt.subplots(n, 1, sharex=True, figsize=(6, np.prod(m) * 0.6)) for i, sel in enumerate(data.coords[dim].values): data.sel({dim: sel}).to_pandas().T.plot.barh(ax=ax[i], stacked=True, cmap="Dark2", legend=False) label = str(sel) + sampling_letter ax[i].set_title(label) ax[i].yaxis.label.set_visible(False) + self._set_bar_label(ax[i]) ax[-1].set_xlabel(f"{self.error_measure} (in {self.error_unit})") fig.legend(*ax[0].get_legend_handles_labels(), loc="upper center", ncol=4) fig.tight_layout(rect=[0, 0, 1, 0.95])