PlasmaCalcs.plotting.labels.XarrayText
- class PlasmaCalcs.plotting.labels.XarrayText(array, txt, t=None, *, base_text=None, **kw_super)
Bases:
MovieTextMoviePlotNode managing text associated with an xarray.DataArray.- txt: Text object
- e.g., the result of plt.text(…) or plt.title(…)
- array: xarray.DataArray, probably ndim=3.
- the full DataArray which will be plotted throughout the movie.internally, store xarray_fill_coords(array), so that coordless dims’ indices can be used.e.g. if “dim0” is a dimension with no coords, will use np.arange(dim0.size).
- t: None or str
- the array dimension which frames will index. E.g. ‘time’.None –> infer it via infer_movie_dim(array.dims, t)
- base_text: None, UNSET or str
- the base text string, to be formatted by array at each frame.UNSET –> will use array._title_for_slice() for array at each frame.str –> will be formatted by xarray_nondim_coords(array at frame).E.g., base_text=’fluid={{fluid}}, time={{t:.2e}} seconds’.if None, infer from txt.
— Examples —# simple example:import PlasmaCalcs as pcarray = … # some array having fluid & t coords…txt = plt.text(0, 7, ‘{fluid}, t={t:.2e}’) # text at x=0, y=7 in data coords.xtext = pc.XarrayText(array, txt, t=’t’)xtext.save(‘text_movie.mp4’)# slightly more complex: start like above, but attach to existing MoviePlotNode instead:xim = array.pc.image()xim.add_child(xtext, arr)xim.save(‘image_and_text_movie.mp4’) # movie of image & text, both updating in time!# one more notable option: instead of txt = plt.text(…),# could do txt = plt.title(…), plt.suptitle(…) or similar option.# also, if infer_movie_dim can figure out t, don’t need to provide it explicitly:xtext = pc.XarrayText(array, plt.title(‘{fluid}, t={t:.2e}’))# finally, note that this can be accessed from array, e.g.:xtext = array.pc.text(plt.title(‘{fluid}, t={t:.2e}’))To view or adjust plot settings in self, see self.plot_settings, or help(self.plot_settings).- __init__(array, txt, t=None, *, base_text=None, **kw_super)
Methods
__init__(array, txt[, t, base_text])add_child(child)display([show_depth, max_depth, shorthand])enumerate_flat(*[, include_self])flat(*[, include_self])flat_branches_until(branches_until, *[, ...])frame_to_text(frame)get_animator(*[, fps, blit, frames, plt_close])get_data_at_frame(frame)help()html([show_depth, max_depth, shorthand])init_plots(*[, plotted_ok])make_child(obj)make_children(objs)save(filename[, frames, fps, blit])set_parent(parent, *[, _internal])update_to_frame(frame)Attributes
DEFAULT_TREE_SHORTHANDDEFAULT_TREE_SHOW_DEPTHDEFAULT_TREE_SHOW_MAX_DEPTH- add_child(child)
- adds this child (a Tree) to self.children. Also, child.set_parent(self).returns the added child.
- property ani
- alias to get_animator
- property ax
- mpl.axes.Axes where this MovieText is plotted.
- property depth
- number of layers above self. (parent, parent’s parents, etc.)depth = 0 for the node with parent = None.
- display(show_depth=None, max_depth=None, *, shorthand=None)
- display self in html. Includes self.html() and DEFAULTS.TREE_CSS.
- show_depth: None or int
- max number of layers of tree to show by default (i.e. “not hidden” by default)None –> use self.DEFAULT_TREE_SHOW_DEPTH if defined else DEFAULTS.TREE_SHOW_DEPTH
- max_depth: None or int
- max number of layers of tree to render (even if all layers are “not hidden”).Anything deeper will not be converted to html string.None –> use self.DEFAULT_TREE_SHOW_MAX_DEPTH if defined else DEFAULTS.TREE_SHOW_MAX_DEPTH
- shorthand: None or bool
- whether to use shorthand for the “Tree([depth=N, height=N, size=N], obj=…)” part of the repr.True –> use shorthand; replace that^ with: “((N, N, N)) …”None –> use self.DEFAULT_TREE_SHORTHAND if defined else DEFAULTS.TREE_SHORTHAND
- element_cls
alias of
TextPlotElement
- enumerate_flat(*, include_self=False)
- returns a generator which iterates over all of self’s descendants, in depth-first order,yielding (index, node) pairs, such that self[index] == node.Note that index will be a tuple with length == node.depth.if include_self, yield self first, as: ((), self)).
- property fig
- figure where this MovieText is plotted.
- flat(*, include_self=False)
- returns a generator which iterates over all of self’s descendants, in depth-first order.if include_self, yield self first.
- flat_branches_until(branches_until, *, include_self=False)
- returns a generator which iterates over all of self’s descendants, in depth-first order,but stop looking at descendants on a branch as soon as branches_until(node).E.g. self.flat_branches_until(lambda node: node.obj==7) will be similar to flat,but won’t go to any descendants for any node with obj==7.if include self, yield self first, and check branches_until(self) before continuing.otherwise, never check branches_until(self).
- property frame
- the currently-plotted frame
- frame_to_text(frame)
- return the text string at this frame
- property frames
- the frames that could be in the movie.if set to None, will use self.get_nframes() instead.if set to a slice, will use range(self.get_nframes())[frames] instead.
- get_animator(*, fps=UNSET, blit=UNSET, frames=UNSET, plt_close=True, **kw_func_animation)
- returns FuncAnimation instance using self as func.Use kwarg defaults from self.plot_settings, for any kwargs not provided here.
- fps: UNSET, None, or number (default: UNSET)
- frames per second.UNSET –> use DEFAULTS.PLOT.FPS (default: 30).(Or use value from self.plot_settings, if provided.)None –> use matplotlib defaults.
- blit: UNSET, None, or bool (default: UNSET)
- whether to use blitting.UNSET –> use DEFAULTS.PLOT.BLIT (default: True).(Or use value from self.plot_settings, if provided.)if None, use matplotlib defaults.
- frames: UNSET, None, int, iterable, or slice (default: UNSET)
- passed to FuncAnimation. Tells number of frames or which frames to plot.If UNSET, use value from self.plot_settings if possible else getattr(self, ‘frames’, None).if slice, use range(self.get_nframes())[frames], crashing if self doesn’t have ‘get_nframes’.
- plt_close: bool
- whether to plt.close() before returning the result.This is useful in Jupyter, where commonly one cell might make a plot,then call get_animator() to display movie in-line, but not plt.close().In that case, plt_close=False would display animation & plot
[TODO] use init_func kwarg to avoid calling self twice for frame 0?
- get_data_at_frame(frame)
- returns {‘text’: text string for this frame}.
- get_nframes()
- return max of get_nframes_here() for self & all descendants of self.If any node throws PlottingNframesUnknownError, pretend they said nframes=0.If all nodes throw PlottingNframesUnknownError, raise the one from self.
- get_nframes_here()
- returns number of frames that could be in this movie, based on this node.
- property height
- number of layers below self. (children, children’s children, etc.)height = 0 for a node with no children.
- classmethod help()
- prints a helpful message with examples for how to use this cls
- html(show_depth=None, max_depth=None, *, shorthand=None)
- returns html for displaying self and all of self’s children.
- show_depth: None or int
- max number of layers of tree to show by default (i.e. “not hidden” by default)None –> use self.DEFAULT_TREE_SHOW_DEPTH if defined else DEFAULTS.TREE_SHOW_DEPTH
- max_depth: None or int
- max number of layers of tree to render (even if all layers are “not hidden”).Anything deeper will not be converted to html string.None –> use self.DEFAULT_TREE_SHOW_MAX_DEPTH if defined else DEFAULTS.TREE_SHOW_MAX_DEPTH
- shorthand: None or bool
- whether to use shorthand for the “Tree([depth=N, height=N, size=N], obj=…)” part of the repr.True –> use shorthand; replace that^ with: “((N, N, N)) …”None –> use self.DEFAULT_TREE_SHORTHAND if defined else DEFAULTS.TREE_SHORTHAND
- init_plot()
- plot for the first time. Save the TitlePlotElement at self.obj.
- init_plots(*, plotted_ok=True)
- init_plot for self & all descendants with non-None obj.
- plotted_ok: bool
- True –> skip node if node.plotted.False –> call init_plot on all nodes with non-None obj.
- make_child(obj)
- makes a child of self, with obj as its stored object, and returns the child.
- make_children(objs)
- make_child(obj) for obj in objs; returns the list of newly made children.
- property parent
- parent node of self. None if self is root.When set to a value, calls self.set_parent(value),which also updates tracking info appropriately, and updates parent’s children.
- property parent_ref
- stores parent value, but internally uses weakref to avoid circular references.Users should always use self.parent instead.
- property plotted
- whether this node’s element has actually been plotted yet.False before init_plot; True after. Always None if self.obj is None.
- property plotted_data
- the currently plotted data.
- save(filename, frames=UNSET, *, fps=UNSET, blit=UNSET, **kw)
- save the movie to filename.RECOMMENDED:first, self.save(…, frames=N), with small N (e.g. N=5), to test movie formatting.
- Troubleshooting: if movie getting cut off,
- try plt.subplots_adjust(bottom=0.2, left=0.2, right=0.8, top=0.8),or even more extreme values if necessary. (0 is bottom/left edge; 1 is top/right edge.)
- frames: UNSET, None, int, iterable, or slice (default: UNSET)
- passed to FuncAnimation. Tells number of frames or which frames to plot.If UNSET, use value from self.plot_settings if possible else getattr(self, ‘frames’, None).if slice, use range(self.get_nframes())[frames], crashing if self doesn’t have ‘get_nframes’.
- fps: UNSET, None, or number (default: UNSET)
- frames per second.UNSET –> use DEFAULTS.PLOT.FPS (default: 30).(Or use value from self.plot_settings, if provided.)None –> use matplotlib defaults.
- blit: UNSET, None, or bool (default: UNSET)
- whether to use blitting.UNSET –> use DEFAULTS.PLOT.BLIT (default: True).(Or use value from self.plot_settings, if provided.)if None, use matplotlib defaults.
additional kwargs passed to FuncAnimation() or FuncAnimation.save().returns abspath of the saved movie.
- set_parent(parent, *, _internal=False)
- sets self.parent = parent. Also, parent.add_child(self), unless _internal=True.Users should use self.parent = parent instead of calling set_parent directly.
- property size
- number of nodes in this tree. (here and below)size = 1 for a node with no children.
- property text
- the text string stored in self.obj; use self.update_to_frame to update it.
- update_to_frame(frame)
- update the plot for the given frame. set self.frame=frame.also calls update_to_frame for all children.return iterable of all updated artists.