pyloggrid.Libs.plotLib

Utilities for fast plotting

Module Contents

Functions

initFormat(→ None)

Sets up pyplot formatting for scientific publications.

pltshowm(→ None)

Display the pyplot figure maximized.

labels(→ None)

Add labels and title to an axis.

scatter(→ Any)

Plot a scatter plot using plot() instead of scatter().

plot2axes(→ tuple[matplotlib.figure.Figure, Any, Any])

Create a plot with left and right axis.

plot2axesX(→ tuple[matplotlib.figure.Figure, Any, Any])

plot with bottom and top axis.

_save_animation_from_slider(→ None)

Save a matplotlib graph with a slider as an animation.

dec_enable_slider_save(→ Callable)

Decorator to save an animation from a slider graph.

interactive_spectrum(...)

An interactive spectrum + arbitrary time-dependant quantities.

interactive_3D_logplot_by_z(...)

An interactive 3D logplot, sliced by Z if given.

interactive_grid_imshow(...)

Imshow with slider, with axes formatted by the grid.

interactive_grid_3Dslice(...)

Interactive 3D slice of a grid field.

interactive_3D_logplot_positive(, threshold_fact, Any])

3D logplot of scalar field on kx,ky,kz>0.

rand_cmap(→ matplotlib.colors.LinearSegmentedColormap)

Creates a random colormap to be used together with matplotlib. Useful for segmentation tasks

initFormat(size: float = 1, txtsize: float = None, rcParams: dict = None) None

Sets up pyplot formatting for scientific publications.

Parameters:
  • size – the figure’s size relative to the default one

  • txtsize – scaling for the figure’s fonts, if not set defaults to size

  • rcParams – parameters to append to matplotlib.rcParams

pltshowm(full: bool = True, save: str = None, legend: bool = True, compact: bool = True, tight: bool = False) None

Display the pyplot figure maximized.

Parameters:
  • legend – if False, do not add a legend

  • save – if specified, save path for the figure

  • full – if False, doe not go fullscreen

  • compact – if True, use plt.tight_layout

  • tight – if True, autoscale with tight boundaries

labels(xlabel: str = '', ylabel: str = '', title: str = '', ax: Any = None) None

Add labels and title to an axis.

Parameters:
  • xlabel

  • ylabel

  • title

  • ax – if specified, the axis on which to add the labels; defaults to the current axis

scatter(ax=None, *args, **kwargs) Any

Plot a scatter plot using plot() instead of scatter().

(not handled the same way, e.g. zindex)

Parameters:

ax – the axis to use, defaults to

plot2axes() tuple[Figure, Any, Any]

Create a plot with left and right axis.

Axes are set to Any since the real class is hidden in mpl.axes._subplots.

Returns:

figure, ax1, ax2

plot2axesX() tuple[Figure, Any, Any]

plot with bottom and top axis.

Axes are set to Any since the real class is hidden in mpl.axes._subplots.

_save_animation_from_slider(slider: Slider, save: str, temp_dir: str, N: int = 100) None

Save a matplotlib graph with a slider as an animation.

Parameters:
  • slider – the slider object

  • save – the output file name

  • temp_dir – where to store the intermediate images

  • N – number of frames

dec_enable_slider_save(f: Callable) Callable

Decorator to save an animation from a slider graph.

This adds two arguments to the decorated function, save and temp_path, which if supplied are forwarded to _save_animation_from_slider().

interactive_spectrum(ts: pyloggrid.Libs.singlethread_numpy.np.ndarray, ks: pyloggrid.Libs.singlethread_numpy.np.ndarray, spectra: dict[str, pyloggrid.Libs.singlethread_numpy.np.ndarray], quantities: dict[str, pyloggrid.Libs.singlethread_numpy.np.ndarray]) tuple[Slider, Any, Any]

An interactive spectrum + arbitrary time-dependant quantities.

Warning

As for all interactive functions, the returned val must be assigned, ex via _ = my_interactive_function(...)

LHS: spectrum vs k RHS: quantity vs t current time is changed via a slider

Parameters:
  • ts – time array

  • ks – ks array for each time

  • spectra – dict label->array

  • quantities – dict label->quantity

interactive_3D_logplot_by_z(X: pyloggrid.Libs.singlethread_numpy.np.ndarray, Y: pyloggrid.Libs.singlethread_numpy.np.ndarray, V: pyloggrid.Libs.singlethread_numpy.np.ndarray, Z: pyloggrid.Libs.singlethread_numpy.np.ndarray = None) tuple[Slider, Any, Any]

An interactive 3D logplot, sliced by Z if given.

Warning

As for all interactive functions, the returned val must be assigned, ex via _ = my_interactive_function(...)

You can either provide 3D arrays (X, Y, V, Z[:,:,0]) or 2D arrays (X, Y, V).

Parameters:
  • X

  • Y

  • V – values

  • Z – optional 1D Z axis

interactive_grid_imshow(grid: Grid, update_data: Callable[[Any, float], pyloggrid.Libs.singlethread_numpy.np.ndarray], slider_params: tuple[float | None, float | None, float | None, str | None, str | None] = (0, 1, None, '', None)) tuple[Slider, Any]

Imshow with slider, with axes formatted by the grid.

Warning

As for all interactive functions, the returned val must be assigned, ex via _ = my_interactive_function(...)

Parameters:
  • grid

  • update_data – a function that takes the plotting ax and the slider’s value, and returns an array to plot of size (N, N)

  • slider_params – (vmin, vmax, vstep, title, vformat), all optional

interactive_grid_3Dslice(grid: Grid, update_data: Callable[[Any, float], pyloggrid.Libs.singlethread_numpy.np.ndarray], slider_params: tuple[float | None, float | None, float | None, str | None, str | None] = (0, 1, None, '', None)) tuple[Slider, Any]

Interactive 3D slice of a grid field.

Warning

As for all interactive functions, the returned val must be assigned, ex via _ = my_interactive_function(...)

Parameters:
  • grid

  • update_data – a function that takes the plotting ax and the slider’s value, and returns a 3D array to plot

  • slider_params – (vmin, vmax, vstep, title, vformat), all optional

interactive_3D_logplot_positive(update_data: Callable[[Any, float], pyloggrid.Libs.singlethread_numpy.np.ndarray], slider_params: tuple[float | None, float | None, float | None, str | None, str | None] = (0, 1, None, '', None), threshold_fact: float = 5) tuple[Slider, Any]

3D logplot of scalar field on kx,ky,kz>0.

Warning

As for all interactive functions, the returned val must be assigned, ex via _ = my_interactive_function(...)

Parameters:
  • update_data – a function that takes the plotting ax and the slider’s value, and returns a 3D array to plot

  • slider_params – (vmin, vmax, vstep, title, vformat), all optional

  • threshold_fact – which points to hide, relative to max(data)

rand_cmap(nlabels: int, type_: Literal[bright, soft] = 'bright', first_color_black: bool = True, last_color_black: bool = False) LinearSegmentedColormap

Creates a random colormap to be used together with matplotlib. Useful for segmentation tasks

Parameters:
  • nlabels – Number of labels (size of colormap)

  • type – ‘bright’ for strong colors, ‘soft’ for pastel colors

  • first_color_black – Option to use first color as black

  • last_color_black – Option to use last color as black

Returns:

colormap for matplotlib