Skip to content
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

added bbox_inches flag. Defaults to None #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions xmovie/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,14 @@ def combine_frames_into_movie(
# return fig


def save_single_frame(fig, frame, odir=None, frame_pattern="frame_%05d.png", dpi=100):
def save_single_frame(fig, frame, odir=None, frame_pattern="frame_%05d.png", dpi=100, bbox_inches=None):
"""Saves a single frame of data from an already-created figure and then closes the figure"""
fig.savefig(
os.path.join(odir, frame_pattern % (frame)),
dpi=dpi,
facecolor=fig.get_facecolor(),
transparent=True,
bbox_inches=bbox_inches,
)
# I am trying everything to *wipe* this figure, hoping that it could
# help with the dask glitches I experienced earlier.
Expand All @@ -243,6 +244,7 @@ def __init__(
pixelwidth=1920,
pixelheight=1080,
dpi=200,
bbox_inches=None,
Copy link
Owner

Choose a reason for hiding this comment

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

We might add other kwargs for .savefig() here (see comments below). If so should we have some dict that catches all save_kwargs=None (we can then set defaults we like in the init)? Otherwise we might be accumulating a bunch of input arguments and the signature of this might become unruly?

frame_pattern="frame_%05d.png",
fieldname=None,
input_check=True,
Expand All @@ -264,6 +266,9 @@ def __init__(
Movie size.
dpi : int
Movie resolution.
bbox_inches: str
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
bbox_inches: str
bbox_inches: str or matplotlib.transforms.Bbox

Same as bbox_inches flag in savefig from matplotlib.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Same as bbox_inches flag in savefig from matplotlib.
Passed to :meth:`~matplotlib.figure.Figure.savefig` when saving frames.

Bounding box in inches: only the given portion of the figure is saved. If 'tight', try to figure out the tight bbox of the figure.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Bounding box in inches: only the given portion of the figure is saved. If 'tight', try to figure out the tight bbox of the figure.
Bounding box in inches: only the given portion of the figure is saved.
If ``'tight'``, try to figure out the tight bbox of the figure.
Try this option to eliminate unnecessary amounts of edge whitespace in frames.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should pad_inches also be added?

frame_pattern : str
Filename pattern when saving frames.
fieldname
Expand All @@ -275,6 +280,7 @@ def __init__(
self.pixelwidth = pixelwidth
self.pixelheight = pixelheight
self.dpi = dpi
self.bbox_inches = bbox_inches
Copy link
Owner

Choose a reason for hiding this comment

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

Same here. I would prefer to carry self.save_kwargs here as a dict.

self.width = self.pixelwidth / self.dpi
self.height = self.pixelheight / self.dpi
self.frame_pattern = frame_pattern
Expand Down Expand Up @@ -372,7 +378,14 @@ def save_frames_serial(self, odir, progress=False):

for timestep in frame_range:
fig, ax, pp = self.render_single_frame(timestep)
save_single_frame(fig, timestep, odir=odir, frame_pattern=self.frame_pattern, dpi=self.dpi)
save_single_frame(
fig,
timestep,
odir=odir,
frame_pattern=self.frame_pattern,
dpi=self.dpi,
bbox_inches=self.bbox_inches,
)

def save_frames_parallel(self, odir, parallel_compute_kwargs=dict()):
"""
Expand Down Expand Up @@ -415,7 +428,14 @@ def _save_single_frame_parallel(xr_array, framedim):
) # get index of chunk in framedim

fig, ax, pp = self.render_single_frame(timestep)
save_single_frame(fig, timestep, odir=odir, frame_pattern=self.frame_pattern, dpi=self.dpi)
save_single_frame(
fig,
timestep,
odir=odir,
frame_pattern=self.frame_pattern,
dpi=self.dpi,
bbox_inches=self.bbox_inches,
)

return time_of_chunk

Expand Down