Skip to content

Mnt/generalize plot varargs #28653

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

Merged
merged 3 commits into from
Oct 25, 2024

Conversation

tacaswell
Copy link
Member

PR summary

This extends the _plot_var_args helper class slightly so that it can be used in the data-prototype work. The key idea here is that in addition to being able to return instantiated Artists, it can also return the inferred x, y, and kwargs.

This is all private API.

PR checklist

@tacaswell tacaswell added this to the v3.10.0 milestone Aug 2, 2024
@@ -214,6 +214,7 @@ class _process_plot_var_args:
"""

def __init__(self, command='plot'):
_api.check_in_list(['plot', 'fill', 'mirror'], command=command)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command API was not great, not documented, but sort of understandable:

The command is the Axes method that calls the class ax.plot() / ax.fill(). The created artists are the return types of that function. Current nuissance: To be more clear the generator functions should be called _make_Line2D and _make_Polygon (modulo capitalization).

I don't understand what "mirror" is for.

command "plot" "fill" "mirror"
created artist Line2D Polygon ?
internal generator function _makeline _makefill _mirror

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wanted to be able to re-use all of the machinery for processing the vargs, but get the per-artist args/wkwargs out so the we can create new-style artist from them (which we can not inject into core Matplotlib yet).

I agree it is not great, but was going for the minimal change that would unblock the data prototype work without having to re-write this whole API!

Instead of "mirror" use "no_artist" instead?

Copy link
Member

@timhoffm timhoffm Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all internal, so renaming would be possible. However there's a slight complication that command is used in raise _api.kwarg_error(self.command, pos_only). With that we inject the usage context name into the class, so that when called from plot() it can issue an error containing "plot" and likewise for "fill". This means that _process_plot_var_args is not really context free and you'd have to hard-code in which places you want to use it to support a proper exception.

The "cheap" solution would be command="UNKNOWN" or similar for the new case, which would result in

UNKNOWN() got an unexpected keyword argument 'x'

That's sort of ok.

The IMHO slightly better solution would be to decouple the function from the caller, i.e. return a generic "unexpected keyword arguments 'x'" and - if desired - catch and ammend that in the caller. This would give the freedom to rename command - e.g. to artist with values Line2D, Polygon and None.

@timhoffm
Copy link
Member

@tacaswell I've been so bold to push a cleanup commit in the spirit of #28653 (comment)

Distinguish variants by desired output instead of calling command.
This gives a better terminology for the new coordinate output variant.

The command input was also used to create an error message containing the
calling function name. This has been replaced by introspection.

IMHO this makes the sematics much more clear. Please have a look if you agree. If you don't agree, feel free to modify or drop that commit.

@tacaswell tacaswell closed this Oct 24, 2024
@tacaswell tacaswell reopened this Oct 24, 2024
tacaswell and others added 3 commits October 24, 2024 15:46
Distinguish variants by desired output instead of calling command.
This gives a better terminology for the new coordinate output variant.

The command input was also used to create an error message containing the
calling function name. This has been replaced by introspection.
@tacaswell tacaswell force-pushed the mnt/generalize_plot_varargs branch from 603d823 to ca2ecb2 Compare October 24, 2024 19:47
axes._process_unit_info(kwargs=kwargs)

for pos_only in "xy":
if pos_only in kwargs:
raise _api.kwarg_error(self.command, pos_only)
raise _api.kwarg_error(inspect.stack()[1].function, pos_only)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit wary of stack walking, but this is only in an case were we are already going to errer out and raise it is OK.

@tacaswell
Copy link
Member Author

I am 👍🏻 on @timhoffm changes.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve. I suggest to Tom and me together as one reviewer, covering each others contribution. So if @ksunden agrees with the changes done after his approval, we can merge.

@ksunden ksunden merged commit db76fc5 into matplotlib:main Oct 25, 2024
43 checks passed
@tacaswell tacaswell deleted the mnt/generalize_plot_varargs branch October 26, 2024 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants