1

I am trying to take full control of the legend in the Windrose; the legend is controlled by the bins, which can be called using the ax.info['bins']. However, I want to change the name of the bin groups.

Bin group names

Looking online, the only solution I found was to edit the .py file of the windrose, edit it and then reimport it into the python script.

Python Windrose legend bracket format and loc

I was looking for an easier method, as I plan to have different bins for various plots within the script. So far i have tried label and set_label which does not seem to work.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Aly Abdelaziz
  • 292
  • 4
  • 24

1 Answers1

1

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_

more info: Github issue solution

J.C Guzman
  • 1,192
  • 3
  • 16
  • 40