Skip to content

Commit

Permalink
Backport PR yt-project#4475: ENH: avoid implicitly dropping default p…
Browse files Browse the repository at this point in the history
…arameters in plot annotation methods
  • Loading branch information
matthewturk authored and meeseeksmachine committed Jun 9, 2023
1 parent cd52a5a commit ddc1529
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 91 deletions.
4 changes: 0 additions & 4 deletions yt/visualization/_mpl_imports.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# these imports are very expensive so we delay them to until they are requested

import matplotlib
from matplotlib.backend_bases import FigureCanvasBase
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.backends.backend_pdf import FigureCanvasPdf
from matplotlib.backends.backend_ps import FigureCanvasPS
from matplotlib.backends.backend_svg import FigureCanvasSVG

matplotlib.rc("contour", negative_linestyle="solid")
167 changes: 80 additions & 87 deletions yt/visualization/plot_modifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,6 @@ def __init__(
text_args: Optional[Dict[str, Any]] = None,
ncont: Optional[int] = None, # deprecated
) -> None:
def_plot_args = {"colors": "k", "linestyles": "solid"}
def_text_args = {"colors": "w"}
if ncont is not None:
issue_deprecation_warning(
"The `ncont` keyword argument is deprecated, use `levels` instead.",
Expand All @@ -914,13 +912,16 @@ def __init__(
self.factor = _validate_factor_tuple(factor)
self.clim = clim
self.take_log = take_log
if plot_args is None:
plot_args = def_plot_args
self.plot_args = plot_args
self.plot_args = {
"colors": "black",
"linestyles": "solid",
**(plot_args or {}),
}
self.label = label
if text_args is None:
text_args = def_text_args
self.text_args = text_args
self.text_args = {
"colors": "white",
**(text_args or {}),
}
self.data_source = data_source

def __call__(self, plot) -> None:
Expand Down Expand Up @@ -1392,8 +1393,6 @@ def __init__(
plot_args: Optional[Dict[str, Any]] = None,
**kwargs,
):
def_plot_args = {"color": "white", "linewidth": 2}

self.p1 = p1
self.p2 = p2
if plot_args is not None:
Expand All @@ -1402,9 +1401,13 @@ def __init__(
"You can now pass arbitrary keyword arguments instead of a dictionary.",
since="4.1.0",
)
plot_args.update(kwargs)

self.plot_args = {**def_plot_args, **kwargs}
self.plot_args = {
"color": "white",
"linewidth": 2,
**(plot_args or {}),
**kwargs,
}
self.coord_system = coord_system
self.transform = None

Expand Down Expand Up @@ -1669,7 +1672,6 @@ def __init__(
plot_args: Optional[Dict[str, Any]] = None, # deprecated
**kwargs,
):
def_plot_args = {"color": "white"}
self.pos = pos
self.length = length
self.width = width
Expand All @@ -1685,10 +1687,11 @@ def __init__(
"You can now pass arbitrary keyword arguments instead of a dictionary.",
since="4.1.0",
)
plot_args = {**def_plot_args, **plot_args, **kwargs}
else:
plot_args = def_plot_args
self.plot_args = plot_args
self.plot_args = {
"color": "white",
**(plot_args or {}),
**kwargs,
}

def __call__(self, plot):
x, y = self._sanitize_coord_system(
Expand Down Expand Up @@ -1812,7 +1815,6 @@ class MarkerAnnotateCallback(PlotCallback):
def __init__(
self, pos, marker="x", *, coord_system="data", plot_args=None, **kwargs
):
def_plot_args = {"color": "w", "s": 50}
self.pos = pos
self.marker = marker
if plot_args is not None:
Expand All @@ -1821,21 +1823,21 @@ def __init__(
"You can now pass arbitrary keyword arguments instead of a dictionary.",
since="4.1.0",
)
plot_args = {**def_plot_args, **plot_args, **kwargs}
else:
plot_args = {**def_plot_args, **kwargs}
self.plot_args = plot_args
self.plot_args = {
"color": "white",
"s": 50,
"transform": None,
**(plot_args or {}),
**kwargs,
}
self.coord_system = coord_system
self.transform = None

def __call__(self, plot):
x, y = self._sanitize_coord_system(
plot, self.pos, coord_system=self.coord_system
)
x, y = self._sanitize_xy_order(plot, x, y)
plot._axes.scatter(
x, y, marker=self.marker, transform=self.transform, **self.plot_args
)
plot._axes.scatter(x, y, marker=self.marker, **self.plot_args)
self._set_plot_limits(plot)


Expand Down Expand Up @@ -1901,19 +1903,18 @@ def __init__(
circle_args=None,
text_args=None,
):
def_text_args = {"color": "white"}
def_circle_args = {"color": "white"}
self.center = center
self.radius = radius
if circle_args is None:
circle_args = def_circle_args
if "fill" not in circle_args:
circle_args["fill"] = False
self.circle_args = circle_args
self.circle_args = {
"color": "white",
"fill": False,
**(circle_args or {}),
}
self.text = text
if text_args is None:
text_args = def_text_args
self.text_args = text_args
self.text_args = {
"color": "white",
**(text_args or {}),
}
self.coord_system = coord_system
self.transform = None

Expand Down Expand Up @@ -2041,12 +2042,12 @@ def __init__(
text_args=None,
inset_box_args=None,
):
def_text_args = {"color": "white"}
self.pos = pos
self.text = text
if text_args is None:
text_args = def_text_args
self.text_args = text_args
self.text_args = {
"color": "white",
**(text_args or {}),
}
self.inset_box_args = inset_box_args
self.coord_system = coord_system
self.transform = None
Expand Down Expand Up @@ -2481,19 +2482,6 @@ def __init__(
text_args=None,
inset_box_args=None,
):
def_text_args = {
"color": "white",
"horizontalalignment": "center",
"verticalalignment": "top",
}
def_inset_box_args = {
"boxstyle": "square,pad=0.3",
"facecolor": "black",
"linewidth": 3,
"edgecolor": "white",
"alpha": 0.5,
}

# Set position based on corner argument.
self.pos = (x_pos, y_pos)
self.corner = corner
Expand All @@ -2504,15 +2492,23 @@ def __init__(
self.time_unit = time_unit
self.coord_system = coord_system
self.time_offset = time_offset
if text_args is None:
text_args = def_text_args
self.text_args = text_args
if inset_box_args is None:
inset_box_args = def_inset_box_args
self.inset_box_args = inset_box_args
self.text_args = {
"color": "white",
"horizontalalignment": "center",
"verticalalignment": "top",
**(text_args or {}),
}

# if inset box is not desired, set inset_box_args to {}
if not draw_inset_box:
if draw_inset_box:
self.inset_box_args = {
"boxstyle": "square,pad=0.3",
"facecolor": "black",
"linewidth": 3,
"edgecolor": "white",
"alpha": 0.5,
**(inset_box_args or {}),
}
else:
self.inset_box_args = None

def __call__(self, plot):
Expand Down Expand Up @@ -2720,16 +2716,6 @@ def __init__(
inset_box_args=None,
scale_text_format="{scale} {units}",
):
def_size_bar_args = {"pad": 0.05, "sep": 5, "borderpad": 1, "color": "w"}

def_inset_box_args = {
"facecolor": "black",
"linewidth": 3,
"edgecolor": "white",
"alpha": 0.5,
"boxstyle": "square",
}

# Set position based on corner argument.
self.corner = corner
self.coeff = coeff
Expand All @@ -2739,18 +2725,24 @@ def __init__(
self.min_frac = min_frac
self.coord_system = coord_system
self.scale_text_format = scale_text_format
if size_bar_args is None:
self.size_bar_args = def_size_bar_args
else:
self.size_bar_args = size_bar_args
if inset_box_args is None:
self.inset_box_args = def_inset_box_args
else:
self.inset_box_args = inset_box_args

self.size_bar_args = {
"pad": 0.05,
"sep": 5,
"borderpad": 1,
"color": "white",
**(size_bar_args or {}),
}
self.inset_box_args = {
"facecolor": "black",
"linewidth": 3,
"edgecolor": "white",
"alpha": 0.5,
"boxstyle": "square",
**(inset_box_args or {}),
}
self.text_args = text_args or {}
self.draw_inset_box = draw_inset_box
if text_args is None:
text_args = {}
self.text_args = text_args

def __call__(self, plot):
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
Expand Down Expand Up @@ -2904,7 +2896,6 @@ class RayCallback(PlotCallback):
_supported_geometries = ("cartesian", "spectral_cube", "force")

def __init__(self, ray, *, arrow=False, plot_args=None, **kwargs):
def_plot_args = {"color": "white", "linewidth": 2}
self.ray = ray
self.arrow = arrow
if plot_args is not None:
Expand All @@ -2913,10 +2904,12 @@ def __init__(self, ray, *, arrow=False, plot_args=None, **kwargs):
"You can now pass arbitrary keyword arguments instead of a dictionary.",
since="4.1.0",
)
plot_args = {**def_plot_args, **plot_args, **kwargs}
else:
plot_args = {**def_plot_args, **kwargs}
self.plot_args = plot_args
self.plot_args = {
"color": "white",
"linewidth": 2,
**(plot_args or {}),
**kwargs,
}

def _process_ray(self):
"""
Expand Down

0 comments on commit ddc1529

Please sign in to comment.