Skip to content

Commit

Permalink
Merge pull request #3565 from neutrinoceros/hotfix_3564
Browse files Browse the repository at this point in the history
BUG: fix for matplotlib 3.5 + symlog + linthresh value outside data range
  • Loading branch information
cphyc authored and neutrinoceros committed Nov 7, 2021
1 parent 0cec44b commit 6c1bfa1
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions yt/visualization/base_plot_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from io import BytesIO

import matplotlib
Expand All @@ -12,7 +13,7 @@
mylog,
)

from ._commons import get_canvas, validate_image_name
from ._commons import MPL_VERSION, get_canvas, validate_image_name

BACKEND_SPECS = {
"GTK": ["backend_gtk", "FigureCanvasGTK", "FigureManagerGTK"],
Expand Down Expand Up @@ -206,17 +207,28 @@ def _init_image(self, data, cbnorm, cblinthresh, cmap, extent, aspect):
vmin=float(self.zmin) if self.zmin is not None else None,
vmax=float(self.zmax) if self.zmax is not None else None,
)
zmin = float(self.zmin) if self.zmin is not None else np.nanmin(data)
zmax = float(self.zmax) if self.zmax is not None else np.nanmax(data)

if cbnorm == "symlog":
# if cblinthresh is not specified, try to come up with a reasonable default
min_abs_val = np.min(np.abs((zmin, zmax)))
if cblinthresh is None:
cblinthresh = np.nanmin(np.absolute(data)[data != 0])
elif zmin * zmax > 0 and cblinthresh < min_abs_val:
warnings.warn(
f"Cannot set a symlog norm with linear threshold {cblinthresh} "
f"lower than the minimal absolute data value {min_abs_val} . "
"Switching to log norm."
)
cbnorm = "log10"

if cbnorm == "log10":
cbnorm_cls = matplotlib.colors.LogNorm
elif cbnorm == "linear":
cbnorm_cls = matplotlib.colors.Normalize
elif cbnorm == "symlog":
# if cblinthresh is not specified, try to come up with a reasonable default
if cblinthresh is None:
cblinthresh = np.nanmin(np.absolute(data)[data != 0])

cbnorm_kwargs.update(dict(linthresh=cblinthresh))
MPL_VERSION = Version(matplotlib.__version__)
if MPL_VERSION >= Version("3.2.0"):
# note that this creates an inconsistency between mpl versions
# since the default value previous to mpl 3.4.0 is np.e
Expand Down Expand Up @@ -273,8 +285,6 @@ def _init_image(self, data, cbnorm, cblinthresh, cmap, extent, aspect):
if cbnorm == "symlog":
formatter = matplotlib.ticker.LogFormatterMathtext(linthresh=cblinthresh)
self.cb = self.figure.colorbar(self.image, self.cax, format=formatter)
zmin = float(self.zmin) if self.zmin is not None else np.nanmin(data)
zmax = float(self.zmax) if self.zmax is not None else np.nanmax(data)

if zmin >= 0.0:
yticks = [zmin] + list(
Expand Down

0 comments on commit 6c1bfa1

Please sign in to comment.