-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Added feature for specifying uniform handle sizes for scatter plost. #4257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
if (self._custom_handler_map is not None and | ||
PathCollection in self._custom_handler_map): | ||
warnings.warn("'scatter_uni_size' was set, but custom " + | ||
"handler was provided. The former will " + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please be explicit, I would say "'scatter_uni_size will be ignored'". (This is mostly because I personally always have to stop and think about which one is 'former' and which one is 'later').
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I this might be a reasonable place to raise an Exception as the user has passed in conflicting inputs. Not 100% sold on that though.
👍 modulo comment warning. attn @pelson Any comments on the changes to Could you please add an entry to https://github.com/matplotlib/matplotlib/tree/master/doc/users/whats_new ? |
@leeonadoh @ryanbelt Can you add an entry in whats_new? This helps to make sure that your new feature is discover-able and highlighted when we release the next feature version. |
Sorry for not giving an update. There's currently a better solution in the works, which will generalize this feature to all types of plots. We'll add the whats_new entry when we have that ready. |
Ah, great to hear! Please feel free to share early and often to get feed back. |
TODO: Modify axes.legend and axes.figlegend to use UniformLegend when uniform size is desired.
The updated solution is able to give all types of plots a uniform size when displayed on the legend. It works by creating a subclass of Currently, the code to use UniformLegend has not been written. The idea however, is similar to the old solution where we introduce a new parameter |
@@ -803,7 +803,7 @@ def get_title(self): | |||
return self._legend_title_box._text | |||
|
|||
def get_window_extent(self, *args, **kwargs): | |||
'return a extent of the legend' | |||
'return a extent of the the legend' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo (that was fixed and this adds back in?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops! Missed that when merging. Fixed in 3f0ae27
Modified `Axes.legend` and `Figure.legend` to support `uniform_size`. Added checks to throw exception on conflicting parameters. `uniform_size` specified with `markerscale` or `handler_map`.
TODO: Unit tests. |
If possible I would generally prefer tests checking marker sizes over image comparison tests. |
This seems like a lot of work for this feature, and I wonder if there is a neater approach. It seems that the key part of this is to Rather than implementing a very specific keyword, I'd be looking at simplifying the ability to change the legend class which is used from With the ability to switch the legend class, I'd then be looking for the best place to override so that I can get the behaviour I wanted. It isn't entirely clear where the handler responsibility lies within the Legend class, but I'd probably plump for It is worth noting, that there is a bit of a smell in the legend code at the moment - some of the artists from the handles are not referenced within legend itself, as there appears to have been an assumption of 1-to-1 mapping of handler to artists. https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/legend_handler.py#L205-L208 So with all of this, I'd be looking at (untested):
And I'd call it with:
Just an alternative thought to the problem. HTH |
Moved to 'proprosed next point release' meaning this is not a blocker for the next release, but can go in if it is ready. This needs a re-base and @pelson 's comments on a possible re-think have not been addressed. |
Closing as abandoned. Seems like it is also in #11127 |
#11127 is a bit different, it would create many legend entries for a single scatterplot. The idea here was to unify the size of different scatter plot's handles. Many options are shown in the stackoverflow question "setting a fixed size for points in legend". The easiest is just:
My faviourite, which I just added to that thread as well would be to use the update function of the Handler.
|
Fix for issue #3225
The user can choose to provide the
scatter_uni_size
keyword within the call to pyplot.legend, which will set the legend handles to be of the specified size:Switching
plt.legend(scatter_uni_size=30)
with the commented line, we see that a warning is thrown if the user provides their own custom handler, while specifyingscatter_uni_size
. In this case,scatter_uni_size
is ignored.In contrast with #4247, this fix works by defining a custom handler for
PathCollection
that sets the size to the one given. This fix does not modify the default mappings, as it can modify more than just the intended subplot, demonstrated by the following:The 2nd subplot will also recieve the changes done to the default mapping. However, by replacing
with
the 2nd subplot will remain unaffected.
Tests will be implemented soon.