windrose icon indicating copy to clipboard operation
windrose copied to clipboard

Custom labels for legend entries

Open jordij opened this issue 5 years ago • 2 comments

  • Allow custom labels for legend entries.
  • Added corresponding tests.
  • [Fixes #103]

jordij avatar Jul 23 '19 10:07 jordij

Cheers for the library - nice work.

jordij avatar Jul 23 '19 10:07 jordij

Hello! I wrote the code below to define labels for my wind rose legend:

def speed_labels(spd_bins, units): labels = [] for left, right in zip (spd_bins[:-1], spd_bins[1:]): if left == spd_bins[1:3]: labels.append('calm'.format(right)) elif np.isinf(right): labels.append('>{} {}'.format(left, units)) else: labels.append('{} - {} {}'.format(left, right, units)) print(labels) return list(labels)

spd_bins = [1, 8, 12, 15, 20, 25] #, np.inf] spd_labels = speed_labels(spd_bins, units='kts')+['>25 its']

Last summer I was able to get the desired labels to show in the legend just by calling: ax.set_legend(loc=(-0.12, 0.75), labels=spd_labels)

I just ran the code today and this doesn't work anymore I get the standard labels (with the brackets) instead...

Any idea why?

nalfahel avatar Mar 29 '20 18:03 nalfahel

@jordij thanks for the PR but this adds an extra complexity to the code that I'm not 100% sure we need. The same can be accomplished with one list comprehension when creating the legend:

leg = ax.set_legend()
labels = ["1st", "2nd", "3rd", "4th", "5th", "6th"]
[old.set_text(new) for (old, new) in zip(leg.texts, labels)];  # this 1-line extra in your code should do it.

Closing this b/c I believe anything we can "outsource" to matplotlib the better.

ocefpaf avatar Sep 21 '22 13:09 ocefpaf