-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[MNT]: Remove the label API from Artist #29422
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
Comments
I can't say I really like that, e.g. I currently have code to serialize the data of (all subplots of) a figure into a dict of |
Not sure I understand what you do. Most of the labels are not set and there's no guarantee that they are unique. For example, each Tick has 6 labels associated, one for Are you sure you need labelling capability on all ( / most) of the artist subclasses (https://matplotlib.org/stable/api/artist_api.html#inheritance-diagrams)?
One can always monkey-patch such an information onto the artist if that's needed in some special cases. Making that a builtin public API is only necessary if we need a formalized way to access such information, i.e. when that information needs to be shared/accessed in our code and/or 3rd parties. Do you have such a use case? |
I only serialize
Not all, but I do use this for ImageBase at least.
Obviously I could just attach a custom attribute on the artist ( |
Well, we could introduce a simple In particular, I find it important to strictly separate such information-only data provided by the user and anything that has an effect in the library. Abusing the label parameter for "additional information" this is a recipe for confusion because depending on the Artist and the formatting (underscore prefix) you may influence the library behavior or not.
Widgets and animations are dedicated concepts in the library. If we need to store them on a figure or axes, that should get explicit attributes. I therefore claim this is a completely separate topic. |
Semi-OT: It may be reasonable to be able to name artists, i.e. make it possible to retrieve them back by a string. such that this would be equivalent
This would also help with FuncAnimations, which are currently quite cobbled together: typically, the plot is created globally, and relevant Artists are stored in global variables that are accessed from the function. This means, the FuncAnimation is not self-contained but the function depends on globally accessible Artists at run-time. Taking from https://matplotlib.org/stable/gallery/animation/simple_anim.html we could then evolve the API such that
could be replaced by
where we pass the figure into the function and from that retrieve the relevant artists. This would eliminate the global state. @anntzer I believe this kind of naming would cover your use case as well? |
This is indeed more or less what I do by hand right now in other places (unrelated to the serialization problem): directly copy-pasting from my internal codebase, I have artists = {}
def register(art):
assert art.get_label()
artists[art.get_label()] = art
return art
# then use as
register(ax.plot(...)[0])
register(ax.imshow(...))
# etc.
# and access via the artists dict. |
What about an explicit LegendKeyArtist ? (DataArtist is too semantically overloaded for me). OT but looking at @anntzer's answer, thinking it'd be nice to be able to register artists with the legend in a way that isn't either on creation (via label kwarg) or all at once (by passing in a list of handles) but possibly on the fly. Particularly if you want multiple legends or to modify a third party legend.
Seems to me like a clean solution for separating out legend label from id label? Also "fig.get_artist("myline")" would be great for my use case of putting stuff in functions but then wanting to tweak the artist just a drop when I'm putting it somewhere else. Currently I'm just stashing stuff in a dict. |
I've moved the Artist name/id discussion to #29429.
Maybe. But it could be that this is more specific than we want. I feel a distinction between "data" (everything that is related to the data coordinates) and "decoration" (title, legend, spines, ticks, etc.) could be helpful. - That's not exactly the same as "can have a legend entry" ("decoration" can never have legend entries, but there can be "data" Artists like images that do not fit in a legend either) - but I'd be ok with that level of imprecision. It's not the end of the world if images would continue to have an (unused) label property.
Agreed, but that's orthogonal. You basically have to add a method |
So far as I remember, some of the motivation for the data work @ksunden is doing is so you can feed data to labels and ticks. Like the tick location/labeling: the x axis loc/lab are built around the x data values and the y axis loc/lab re built around the y data. Yes there's level of indirection b/c it's fed from the axis but I dunno if we make that distinction anywherem And I think the most basic artists like patch and Line2D gets reused when making both "plot type (visual idiom)" visual elements and "labeling/annotation" visual elements But also like you point out, you're making the distinction based on coordinate space - but like axline is mixed coordinate and annotation is whatever coordinate you choose. And if this abstraction will include artists that don't participate in the legend then it seems to not solve the original request of removing label from artists that don't participate in legend. |
We're in a bad situation as There are different ways to improve:
Overall, I'm not clear what the best solution is. |
A more radical change here is to add an API to
and relax label to "some descriptive text". Objects used in a context where they should not have a legend entry (either as decoration or because it makes no sense to be a legend entry for the legend in the legend) could raise or return a "Nope!" value of some sort. This would let us move a whole bunch of the complexity (the multi-layered legend factory registry) to the artist objects and allow for more natural tuning of them at the artist level. |
Maybe I'm not fully understanding this. |
Summary
Artist provides the label API intended to for legend labels:
However, only a small subset of Artists are "data" Artists and can be reasonably used in a legend. From the subclasses of Artist:
In some of these not-suitable classes label is repurposed for other use cases, e.g. axis labels, figure labels being displayed as window title. That's quite confusing.
Proposed fix
Create a Protocol or Mixin or DataArtist subclass for legend labels and use that in Line2D, Collection, Patch. Deprecate and remove
get/set_label
from all other Artists; possibly taking special measures where label is currently repurposed.The text was updated successfully, but these errors were encountered: