windrose icon indicating copy to clipboard operation
windrose copied to clipboard

Update legend fontsize

Open Enzoupi opened this issue 6 years ago • 2 comments

Bug report ?

I was trying to set the fontsize of the labels of the legend. Increasing the fontsize through: ax.set_legend(prop={'size':20}, labelspacing=0.5) did increase the size of the coloured patches BUT the fontsize did remain the same. Looking at the source code, I had to do something like: plt.setp(legend.get_texts(), fontsize=15) to actually change the label fontsize.

I believe it is not a good idea ti have it as an hard-coded value.

Otherwise thanks a lot for the API which is very convenient !

https://github.com/python-windrose/windrose/blob/a06c57de02d7067c4eab9cf68d4435dbf7fb3fa6/windrose/windrose.py#L250

Enzoupi avatar Mar 06 '19 16:03 Enzoupi

Hi @Enzoupi

Thanks according https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html fontsize seems to be keyword argument of pyplot.legend

https://github.com/python-windrose/windrose/blob/a06c57de02d7067c4eab9cf68d4435dbf7fb3fa6/windrose/windrose.py#L246

Maybe https://github.com/python-windrose/windrose/blob/a06c57de02d7067c4eab9cf68d4435dbf7fb3fa6/windrose/windrose.py#L246-L251

could be changed to (untested)

def set_legend(self, **pyplot_arguments): 
    if "borderaxespad" not in pyplot_arguments: 
        pyplot_arguments["borderaxespad"] = -0.10
    if "fontsize" not in pyplot_arguments: 
        pyplot_arguments["fontsize"] = 8
    legend = self.legend(**pyplot_arguments) 
    plt.setp(legend.get_texts(), fontsize=pyplot_arguments["fontsize"]) 
    return legend 

and call set_legend passing fontsize=8

Feel free to try and submit pull-request.

Kind regards

s-celles avatar Mar 06 '19 16:03 s-celles

Hi!

First, thank you for creating this small, but useful package for python.

I found that using ax.set_legend() doesn't allow to change the font size of the numbers within the legend. Indeed, you can add the argument "fontsize=10", for instance, but this will only change the size of the colored rectangles within the legend, but not the font size. I've tried also adding "prop={'size': 6}" as an argument with the same result.

Actually, instead of using ax.set_legend(), I have used ax.legend() and it works well as it should.

I just commented on this issue to let you know and suggest a change in your examples. Please, use ax.legend() instead of ax.set_legend(). So, new users will not lose time trying to edit the legend unsuccessfully.

Kind regards.

rabaneda avatar Apr 12 '20 19:04 rabaneda

Closing this as answered. Please re-open if you disagree.

ocefpaf avatar Sep 21 '22 17:09 ocefpaf