Skip to content

Commit

Permalink
Refactor visualization classes to have clearer function names and doc…
Browse files Browse the repository at this point in the history
…strings
  • Loading branch information
conradhaupt committed Oct 3, 2022
1 parent fdf1c23 commit 84ef573
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 13 additions & 2 deletions qiskit_experiments/visualization/drawers/mpl_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

"""Curve drawer for matplotlib backend."""

from typing import Dict, Optional, Sequence, Tuple
from typing import Any, Dict, Optional, Sequence, Tuple

import numpy as np
from matplotlib.axes import Axes
Expand Down Expand Up @@ -42,9 +42,20 @@ class PrefixFormatter(Formatter):
"""

def __init__(self, factor: float):
"""Create a PrefixFormatter instance.
Args:
factor: factor by which to scale tick values.
"""
self.factor = factor

def __call__(self, x, pos=None):
def __call__(self, x: Any, pos: int = None):
"""Returns the formatted string for tick position ``pos`` and value ``x``.
Args:
x: the tick value to format.
pos: the tick label position.
"""
return self.fix_minus("{:.3g}".format(x * self.factor))

def __init__(self):
Expand Down
10 changes: 5 additions & 5 deletions qiskit_experiments/visualization/plotters/base_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,17 @@ def _plot_figure(self):
"""

def figure(self) -> Any:
"""Generates and returns a figure for the already provided series and figure data.
"""Generates and returns a figure for the already provided series and supplementary data.
:meth:`figure` calls :meth:`_plot_figure`, which is overridden by sub-classes. Before and after
calling :meth:`_plot_figure`; :func:`_initialize_drawer`, :func:`initialize_canvas` and
calling :meth:`_plot_figure`; :func:`_configure_drawer`, :func:`initialize_canvas` and
:func:`format_canvas` are called on the drawer respectively.
Returns:
Any: A figure generated by :attr:`drawer`, of the same type as ``drawer.figure``.
"""
# Initialize drawer, to copy axis, subplots, style, and figure-options across.
self._initialize_drawer()
self._configure_drawer()

# Initialize canvas, which creates subplots, assigns axis labels, etc.
self.drawer.initialize_canvas()
Expand Down Expand Up @@ -452,7 +452,7 @@ def set_figure_options(self, **fields):
self._figure_options.update_options(**fields)
self._set_figure_options = self._set_figure_options.union(fields)

def _initialize_drawer(self):
def _configure_drawer(self):
"""Configures :attr:`drawer` before plotting.
The following actions are taken:
Expand All @@ -463,7 +463,7 @@ def _initialize_drawer(self):
These steps are different as all figure-options could be passed to :attr:`drawer`, if the drawer
already has a figure-option with the same name. ``axis``, ``subplots``, and ``style`` are the
only plotter options (from :attr:`options`) passed to :attr:`drawer` in
:meth:`_initialize_drawer`. This is done as these options make more sense as an option for a
:meth:`_configure_drawer`. This is done as these options make more sense as an option for a
plotter, given the interface of :class:`BasePlotter`.
"""
## Axis, subplots, and style
Expand Down

0 comments on commit 84ef573

Please sign in to comment.