Skip to content

Commit

Permalink
Revert namespace renaming of PlotStyle parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
conradhaupt committed Oct 3, 2022
1 parent 7c3ca90 commit 8ed2c9f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ def _default_options(cls):
default_options.plotter.set_options(
subplots=(3, 1),
style=PlotStyle(
figsize=(8, 10),
legend_loc="lower right",
text_box_rel_pos=(0.28, -0.10),
{
"figsize": (8, 10),
"legend_loc": "lower right",
"textbox_rel_pos": (0.28, -0.10),
}
),
)
default_options.plotter.set_figure_options(
Expand Down
20 changes: 10 additions & 10 deletions qiskit_experiments/visualization/drawers/mpl_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def initialize_canvas(self):
if isinstance(label, list):
# Y label can be given as a list for each sub axis
label = label[i]
sub_ax.set_ylabel(label, fontsize=self.style["axis.label_size"])
sub_ax.set_ylabel(label, fontsize=self.style["axis_label_size"])
if i != n_rows - 1:
# remove x axis except for most-bottom plot
sub_ax.set_xticklabels([])
Expand All @@ -103,18 +103,18 @@ def initialize_canvas(self):
if isinstance(label, list):
# X label can be given as a list for each sub axis
label = label[j]
sub_ax.set_xlabel(label, fontsize=self.style["axis.label_size"])
sub_ax.set_xlabel(label, fontsize=self.style["axis_label_size"])
if j == 0 or i == n_rows - 1:
# Set label size for outer axes where labels are drawn
sub_ax.tick_params(labelsize=self.style["tick.label_size"])
sub_ax.tick_params(labelsize=self.style["tick_label_size"])
sub_ax.grid()

# Remove original axis frames
axis.axis("off")
else:
axis.set_xlabel(self.figure_options.xlabel, fontsize=self.style["axis.label_size"])
axis.set_ylabel(self.figure_options.ylabel, fontsize=self.style["axis.label_size"])
axis.tick_params(labelsize=self.style["tick.label_size"])
axis.set_xlabel(self.figure_options.xlabel, fontsize=self.style["axis_label_size"])
axis.set_ylabel(self.figure_options.ylabel, fontsize=self.style["axis_label_size"])
axis.tick_params(labelsize=self.style["tick_label_size"])
axis.grid()

self._axis = axis
Expand All @@ -130,7 +130,7 @@ def format_canvas(self):
for sub_ax in all_axes:
_, labels = sub_ax.get_legend_handles_labels()
if len(labels) > 1:
sub_ax.legend(loc=self.style["legend.loc"])
sub_ax.legend(loc=self.style["legend_loc"])

# Format x and y axis
for ax_type in ("x", "y"):
Expand Down Expand Up @@ -216,7 +216,7 @@ def format_canvas(self):
if self.figure_options.figure_title is not None:
self._axis.set_title(
label=self.figure_options.figure_title,
fontsize=self.style["axis.label_size"],
fontsize=self.style["axis_label_size"],
)

def _get_axis(self, index: Optional[int] = None) -> Axes:
Expand Down Expand Up @@ -427,14 +427,14 @@ def draw_text_box(
bbox_props.update(**options)

if rel_pos is None:
rel_pos = self.style["textbox.rel_pos"]
rel_pos = self.style["textbox_rel_pos"]

text_box_handler = self._axis.text(
*rel_pos,
s=description,
ha="center",
va="top",
size=self.style["textbox.text_size"],
size=self.style["textbox_text_size"],
transform=self._axis.transAxes,
zorder=1000, # Very large zorder to draw over other graphics.
)
Expand Down
32 changes: 16 additions & 16 deletions qiskit_experiments/visualization/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ class PlotStyle(dict):
This style class is used by :class:`BasePlotter` and :class:`BaseDrawer`. The default style for
Qiskit Experiments is defined in :meth:`default_style`. Style parameters are stored as dictionary
entries, grouped by graphics or figure component. For example, style parameters relating to textboxes
have the prefix ``textbox.``. For default style parameter names and their values, see the
have the prefix ``textbox_``. For default style parameter names and their values, see the
:meth:`default_style` method.
Example:
.. code-block:: python
# Create custom style
custom_style = PlotStyle(
{
"legend.loc": "upper right",
"textbox.rel_pos": (1, 1),
"textbox.text_size": 14,
"legend_loc": "upper right",
"textbox_rel_pos": (1, 1),
"textbox_text_size": 14,
}
)
# Create full style, using PEP448 to combine with default style.
full_style = PlotStyle.merge(PlotStyle.default_style(), custom_style)
# Query style parameters
full_style["legend.loc"] # Returns "upper right"
full_style["axis.label_size"] # Returns the value provided in ``PlotStyle.default_style()``
full_style["legend_loc"] # Returns "upper right"
full_style["axis_label_size"] # Returns the value provided in ``PlotStyle.default_style()``
"""

@classmethod
Expand All @@ -48,12 +48,12 @@ def default_style(cls) -> "PlotStyle":
Style Parameters:
figsize (Tuple[int,int]): The size of the figure ``(width, height)``, in inches.
legend.loc (str): The location of the legend.
tick.label_size (int): The font size for tick labels.
axis.label_size (int): The font size for axis labels.
textbox.rel_pos (Tuple[float,float]): The relative position ``(horizontal, vertical)`` of
legend_loc (str): The location of the legend.
tick_label_size (int): The font size for tick labels.
axis_label_size (int): The font size for axis labels.
textbox_rel_pos (Tuple[float,float]): The relative position ``(horizontal, vertical)`` of
textboxes, as a percentage of the canvas dimensions.
textbox.text_size (int): The font size for textboxes.
textbox_text_size (int): The font size for textboxes.
Returns:
PlotStyle: The default plot style used by Qiskit Experiments.
Expand All @@ -62,15 +62,15 @@ def default_style(cls) -> "PlotStyle":
# size of figure (width, height)
"figsize": (8, 5), # Tuple[int, int]
# legend location (vertical, horizontal)
"legend.loc": "center right", # str
"legend_loc": "center right", # str
# size of tick label
"tick.label_size": 14, # int
"tick_label_size": 14, # int
# size of axis label
"axis.label_size": 16, # int
"axis_label_size": 16, # int
# relative position of a textbox
"textbox.rel_pos": (0.6, 0.95), # Tuple[float, float]
"textbox_rel_pos": (0.6, 0.95), # Tuple[float, float]
# size of textbox text
"textbox.text_size": 14, # int
"textbox_text_size": 14, # int
}
return cls(**style)

Expand Down
10 changes: 5 additions & 5 deletions test/visualization/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def test_default_only_contains_expected_fields(self):
default = PlotStyle.default_style()
expected_not_none_fields = [
"figsize",
"legend.loc",
"tick.label_size",
"axis.label_size",
"textbox.rel_pos",
"textbox.text_size",
"legend_loc",
"tick_label_size",
"axis_label_size",
"textbox_rel_pos",
"textbox_text_size",
]
for field in expected_not_none_fields:
self.assertIsNotNone(default.get(field, None))
Expand Down

0 comments on commit 8ed2c9f

Please sign in to comment.