Plotting Tools#

The pyathena.plt_tools module provides matplotlib utilities for visualizing simulation data.

Colormaps#

pyathena.plt_tools.cmap_shift.cmap_shift(cmap, start=0.0, midpoint=0.5, stop=1.0, name='shiftedcmap')[source]#

Function to offset the center of a colormap. Useful for data with a negative min and positive max and you want the middle of the colormap’s dynamic range to be at zero.

Parameters
  • cmap (matplotlib colormap) – Colormap to be modified

  • start (float) – Offset from lowest point in the colormap’s range. Should be between 0.0 and midpoint. Default value is 0.0 (no lower ofset).

  • midpoint (float) – New center of the colormap. Should be between 0.0 and 1.0. In general, this should be 1 - vmax/(vmax + abs(vmin)). For example, if the data ranges from -15.0 to +5.0 and you want the center of the colormap at 0.0, midpoint should be set to 1 - 5/(5 + 15)) or 0.75. Default value is 0.5 (no shift).

  • stop (float) – Offset from highets point in the colormap’s range. Should be between midpoint and 1.0. Default value is 1.0 (no upper ofset).

Returns

cmap_new – New colormap

Return type

matplotlib colormap

pyathena.plt_tools.cmap.cmap_apply_alpha(name='Blues')[source]#

Annotations#

class pyathena.plt_tools.line_annotation.LineAnnotation(text, line, x, xytext=(0, 5), textcoords='offset points', **kwargs)[source]#

A sloped annotation to line at position x with text Optionally an arrow pointing from the text to the graph at x can be drawn. Usage —– fig, ax = plt.subplots() x = np.linspace(0, 2*pi) line, = ax.plot(x, sin(x)) ax.add_artist(LineAnnotation(“text”, line, 1.5))

Annotate the point at x of the graph line with text text.

By default, the text is displayed with the same rotation as the slope of the graph at a relative position xytext above it (perpendicularly above).

An arrow pointing from the text to the annotated point xy can be added by defining arrowprops.

Parameters
  • text (str) – The text of the annotation.

  • line (Line2D) – Matplotlib line object to annotate

  • x (float) – The point x to annotate. y is calculated from the points on the line.

  • xytext ((float, float), default: (0, 5)) – The position (x, y) relative to the point x on the line to place the text at. The coordinate system is determined by textcoords.

  • **kwargs – Additional keyword arguments are passed on to Annotation.

See also

None, None

get_rotation()[source]#

Determines angle of the slope of the neighbours in display coordinate system

set(*, agg_filter=<UNSET>, alpha=<UNSET>, animated=<UNSET>, anncoords=<UNSET>, annotation_clip=<UNSET>, antialiased=<UNSET>, backgroundcolor=<UNSET>, bbox=<UNSET>, clip_box=<UNSET>, clip_on=<UNSET>, clip_path=<UNSET>, color=<UNSET>, fontfamily=<UNSET>, fontproperties=<UNSET>, fontsize=<UNSET>, fontstretch=<UNSET>, fontstyle=<UNSET>, fontvariant=<UNSET>, fontweight=<UNSET>, gid=<UNSET>, horizontalalignment=<UNSET>, in_layout=<UNSET>, label=<UNSET>, linespacing=<UNSET>, math_fontfamily=<UNSET>, mouseover=<UNSET>, multialignment=<UNSET>, parse_math=<UNSET>, path_effects=<UNSET>, picker=<UNSET>, position=<UNSET>, rasterized=<UNSET>, rotation=<UNSET>, rotation_mode=<UNSET>, sketch_params=<UNSET>, snap=<UNSET>, text=<UNSET>, transform=<UNSET>, transform_rotates_text=<UNSET>, url=<UNSET>, usetex=<UNSET>, verticalalignment=<UNSET>, visible=<UNSET>, wrap=<UNSET>, x=<UNSET>, y=<UNSET>, zorder=<UNSET>)#

Set multiple properties at once.

Supported properties are

Properties:

agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array and two offsets from the bottom left corner of the image alpha: float or None animated: bool anncoords: unknown annotation_clip: bool or None antialiased: bool backgroundcolor: :mpltype:`color` bbox: dict with properties for .patches.FancyBboxPatch clip_box: ~matplotlib.transforms.BboxBase or None clip_on: bool clip_path: Patch or (Path, Transform) or None color or c: :mpltype:`color` figure: ~matplotlib.figure.Figure or ~matplotlib.figure.SubFigure fontfamily or family or fontname: {FONTNAME, ‘serif’, ‘sans-serif’, ‘cursive’, ‘fantasy’, ‘monospace’} fontproperties or font or font_properties: .font_manager.FontProperties or str or pathlib.Path fontsize or size: float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’} fontstretch or stretch: {a numeric value in range 0-1000, ‘ultra-condensed’, ‘extra-condensed’, ‘condensed’, ‘semi-condensed’, ‘normal’, ‘semi-expanded’, ‘expanded’, ‘extra-expanded’, ‘ultra-expanded’} fontstyle or style: {‘normal’, ‘italic’, ‘oblique’} fontvariant or variant: {‘normal’, ‘small-caps’} fontweight or weight: {a numeric value in range 0-1000, ‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’} gid: str horizontalalignment or ha: {‘left’, ‘center’, ‘right’} in_layout: bool label: object linespacing: float (multiple of font size) math_fontfamily: str mouseover: bool multialignment or ma: {‘left’, ‘right’, ‘center’} parse_math: bool path_effects: list of .AbstractPathEffect picker: None or bool or float or callable position: (float, float) rasterized: bool rotation: float or {‘vertical’, ‘horizontal’} rotation_mode: {None, ‘default’, ‘anchor’} sketch_params: (scale: float, length: float, randomness: float) snap: bool or None text: object transform: ~matplotlib.transforms.Transform transform_rotates_text: bool url: str usetex: bool, default: :rc:`text.usetex` verticalalignment or va: {‘baseline’, ‘bottom’, ‘center’, ‘center_baseline’, ‘top’} visible: bool wrap: bool x: float y: float zorder: float

update_positions(renderer)[source]#

Updates relative position of annotation text .. note:: Called during annotation draw call

Plots#

pyathena.plt_tools.plt_joint_pdf.plt_joint_pdf(x, y, hexbin_args={}, bins=30, weights=None, gs=None)[source]#

Plot a 2D joint PDF with marginalized 1D histograms.

Creates a figure with a central hexbin 2D histogram and marginal histograms along the x and y axes.

Parameters
  • x (array-like) – Data for the x-axis.

  • y (array-like) – Data for the y-axis.

  • hexbin_args (dict, optional) – Arguments passed to matplotlib.axes.Axes.hexbin(). Must include an extent key ([xmin, xmax, ymin, ymax] in log10 for log scales) for the marginal histograms to align correctly. Default sets mincnt=1, xscale='log', yscale='log'.

  • bins (int, optional) – Number of bins for the marginal histograms. Default is 30.

  • weights (array-like, optional) – Weights applied to both the hexbin and marginal histograms. Default is None.

  • gs (matplotlib.gridspec.GridSpec, optional) – Existing GridSpec to draw into. If None, a new figure is created.

Returns

  • ax (matplotlib.axes.Axes) – Central 2D hexbin axes.

  • axx (matplotlib.axes.Axes) – Top marginal histogram axes (x-axis projection).

  • axy (matplotlib.axes.Axes) – Right marginal histogram axes (y-axis projection).

pyathena.plt_tools.plt_starpar.scatter_sp(sp, ax, dim, cmap=<matplotlib.colors.LinearSegmentedColormap object>, norm_factor=4.0, kind='prj', dist_max=50.0, marker='o', edgecolors=None, linewidths=None, alpha=1.0, kpc=False, runaway=False, agemax=20.0, agemax_sn=40.0, plt_old=False, u=None)[source]#

Function to scatter plot star particles. (From pyathena classic)

Parameters
  • sp (DataFrame) – Star particle data

  • ax (Axes) – matplotlib axes

  • dim ('x' or 'y' or 'z' (or 0, 1, 2)) – Line-of-sight direction

  • norm_factor (float) – Symbol size normalization (bigger for smaller norm_factor)

  • kind ('prj' or 'slc') – Slice or projection.

  • cmap (matplotlib colormap) –

  • dist_max (float) – maximum perpendicular distance from the slice plane (slice only)

pyathena.plt_tools.plt_starpar.legend_sp(ax, norm_factor, mass=[100.0, 1000.0], location='top', fontsize='medium', facecolors='k', linewidths=1.0, bbox_to_anchor=None, ext=None)[source]#

Add legend for sink particle mass.

Parameters
  • ax (matplotlib axes) –

  • norm_factor (float) – Normalization factor for symbol size.

  • mass (sequence of float) – Sink particle masses in solar mass.

  • location (str) – “top” or “right”

pyathena.plt_tools.plt_starpar.colorbar_sp(fig, agemax, cmap=<matplotlib.colors.LinearSegmentedColormap object>, orientation='horizontal', tickloc='top', bbox=[0.125, 0.9, 0.1, 0.015])[source]#

Style#

pyathena.plt_tools.set_plt.set_plt_default()[source]#

Apply default matplotlib rcParams for pyathena plots.

Sets figure size (8x6), DPI 200, font size 15, thick tick marks, and constrained layout.

pyathena.plt_tools.set_plt.set_plt_fancy()[source]#

Apply a publication-quality matplotlib style.

Sets ticks inward on all sides, white background, thick lines, and a custom 10-color cycle.

pyathena.plt_tools.set_plt.toggle_xticks(axes, visible=False)[source]#

Toggle x-axis tick label visibility on one or more axes.

Parameters
  • axes (matplotlib.axes.Axes or array-like of Axes) – Axes to modify.

  • visible (bool, optional) – Whether to show tick labels. Default is False (hidden).

pyathena.plt_tools.set_plt.toggle_yticks(axes, visible=False)[source]#

Toggle y-axis tick label visibility on one or more axes.

Parameters
  • axes (matplotlib.axes.Axes or array-like of Axes) – Axes to modify.

  • visible (bool, optional) – Whether to show tick labels. Default is False (hidden).

pyathena.plt_tools.utils.texteffect(fontsize=12, linewidth=3, foreground='w')[source]#
pyathena.plt_tools.make_spines_invisible.make_patch_spines_invisible(ax)[source]#

Movies#

pyathena.plt_tools.make_movie.make_movie(fname_glob, fname_out, fps_in=15, fps_out=15)[source]#

Create an mp4 movie from image files matching a glob pattern.

Requires ffmpeg to be installed and available on PATH. Output is encoded as H.264 with yuv420p pixel format for broad compatibility.

Parameters
  • fname_glob (str) – Glob pattern matching the input image files (e.g. 'frame.????.png').

  • fname_out (str) – Path of the output .mp4 file. Overwritten if it already exists.

  • fps_in (int, optional) – Frame rate of the input image sequence. Default is 15.

  • fps_out (int, optional) – Frame rate of the output video. Default is 15.

Returns

True if ffmpeg completed successfully, False otherwise.

Return type

bool

Examples

>>> make_movie('a.????.png', 'a.mp4', fps_in=1, fps_out=24)
pyathena.plt_tools.make_movie.display_movie(filename)[source]#

Display an mp4 video inline in a Jupyter notebook.

Parameters

filename (str) – Path to the .mp4 file.

Returns

An HTML video element for inline playback.

Return type

IPython.display.HTML