Skip to content

[MNT]: Interaction between savefig args transparent and facecolor/edgecolor make it impossible to spell out the signature of savefig #24413

Open
@anntzer

Description

@anntzer

Summary

In #24405 I tried to replace the current pyplot.savefig() wrapper manual implementation, which just has an unhelpful signature of *args, **kwargs, by a standard autogenerated pyplot wrapper, which copies the signature of Figure.savefig, which should itself match the signature of FigureCanvasBase.print_figure -- except for the fact that savefig adds a "transparent" kwarg not present in print_figure (and for a discrepancy in the name of the first argument: fname vs filename).

Unfortunately, the "transparent" kwarg has some problematic semantics (when set to True -- nothing happens when it is set to False): it always makes axes transparent, but the figure is made transparent only if the "facecolor" and "edgecolor" kwargs are unset -- if "facecolor"/"edgecolor" are None, then they are considered as set to the corresponding rcParams values (savefig.facecolor/savefig.edgecolor). This is actually documented behavior ("If True, the Axes patches will all be transparent; the Figure patch will also be transparent unless facecolor and/or edgecolor are specified via kwargs.")

This means that the real signature of print_figure is not print_figure(..., transparent=None [-> rcparam], facecolor=None [-> rcparam], edgecolor=None [-> rcparam], ...) but that facecolor/edgecolor can indeed not appear explicitly in the signature, only implicitly via **kwargs (or they have to instead default to some special placeholder like _UNSET). This is not a desirable state, e.g. due to the difficulty in providing introspection for the signature of savefig.

Proposed fix

Mildly break the API and instead make transparent=True override anything set by the facecolor and edgecolor kwargs, which should be rarely(?) used anyways.


See also #9080 (comment).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions