windrose icon indicating copy to clipboard operation
windrose copied to clipboard

Change Label Legend

Open alicarlos opened this issue 7 years ago • 2 comments

Be able to change the labels on the bins

onspw

alicarlos avatar Oct 18 '18 15:10 alicarlos

https://github.com/python-windrose/windrose/blob/master/windrose/windrose.py#L222-L248

s-celles avatar Oct 18 '18 15:10 s-celles

By adding a labels argument in ax.set_legend and changing the script to accept the argument. I also added a small validation that displays error in case the labels do not match the handles.

ax.set_legend(labels = ['T', 'S', 'M'], title="Failure Mode", loc="upper left")

        def get_labels(labels, decimal_places=1):
            _decimal_places = str(decimal_places)

            fmt = (
                "[%." + _decimal_places + "f " +
                ": %0." + _decimal_places + "f"
            )

            if labels is None:
                labels = np.copy(self._info['bins'])
                if locale.getlocale()[0] in ['fr_FR']:
                    fmt += '['
                else:
                    fmt += ')'
                labels = [fmt % (labels[i], labels[i + 1])
                    for i in range(len(labels) - 1)]
            else:
                if len(labels) != len(self._info['bins']) -1 :
                    print("ERROR")
                labels = labels
            
            return labels


        kwargs.pop('handles', None)

        decimal_places = kwargs.pop('decimal_places', 1)
        labels = kwargs.pop('labels', None)

        handles = get_handles()
        labels = get_labels(labels, decimal_places)
        self.legend_ = mpl.legend.Legend(self, handles, labels, loc, **kwargs)
        return self.legend_

alicarlos avatar Oct 22 '18 00:10 alicarlos

Should be fixed by https://github.com/python-windrose/windrose/pull/146 and we can also override that using mpl like in https://github.com/python-windrose/windrose/pull/120#issuecomment-1253717780.

ocefpaf avatar Sep 21 '22 17:09 ocefpaf