JuliaPy / PythonPlot.jl

Plotting for Julia based on matplotlib.pyplot
MIT License
74 stars 8 forks source link

`bbox_inches="tight"` hardcoded #31

Open tfiers opened 1 year ago

tfiers commented 1 year ago

All figures are displayed with matplotlib's auto-selection of the crop:

https://github.com/JuliaPy/PythonPlot.jl/blob/fe588f4e3b084b9d0ada8641939c6331def77036/src/PythonPlot.jl#L86

This is a great default. But sometimes you want to have more control over the bbox (especially when working with extreme aspect ratios, when "tight" produces totally different figsizes than what you requested).

Also, if in your rcparams, savefig.bbox is set to "standard" (the default) and not "tight", then your saved figures will differ from the displayed figures, which is confusing.

So, it would be nice if this hardcoded setting was somehow configureable in PythonPlot.

tfiers commented 1 year ago

This is what I currently use to override PythonPlot's display (ignoring other formats besides PNG, and foregoing the _showable(m, f) check):


using PythonPlot: Figure, matplotlib as mpl

function set_bbox(bbox = "standard")
    if bbox ∈ ("standard", "tight")  # The dict-like `mpl.rcParams` object errors if not
        mpl.rcParams["savefig.bbox"] = bbox
    end
    # From PythonPlot.jl:
    @eval Main function Base.show(io::IO, m::MIME"image/png", f::Figure)
        if $(bbox == "standard")  # Can't pass that here.
            f.canvas.print_figure(io, format="png")
        else
            f.canvas.print_figure(io, format="png", bbox_inches=$bbox)
        end
    end
end