-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ENH: Add func norm #18653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Add func norm #18653
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ Classes | |
PowerNorm | ||
SymLogNorm | ||
TwoSlopeNorm | ||
FuncNorm | ||
|
||
Functions | ||
--------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -537,6 +537,29 @@ def test_Normalize(): | |
assert 0 < norm(1 + 50 * eps) < 1 | ||
|
||
|
||
def test_FuncNorm(): | ||
def forward(x): | ||
return (x**2) | ||
def inverse(x): | ||
return np.sqrt(x) | ||
|
||
norm = mcolors.FuncNorm((forward, inverse), vmin=0, vmax=10) | ||
expected = np.array([0, 0.25, 1]) | ||
input = np.array([0, 5, 10]) | ||
assert_array_almost_equal(norm(input), expected) | ||
assert_array_almost_equal(norm.inverse(expected), input) | ||
|
||
def forward(x): | ||
return np.log10(x) | ||
def inverse(x): | ||
return 10**x | ||
norm = mcolors.FuncNorm((forward, inverse), vmin=0.1, vmax=10) | ||
lognorm = mcolors.LogNorm(vmin=0.1, vmax=10) | ||
assert_array_almost_equal(norm([0.2, 5, 10]), lognorm([0.2, 5, 10])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you not want to assert the inverse here as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I wasn't going to but it does drop our test coverage, so added... Interestingly the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably because diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py
index e417b8178d..b37ec947fa 100644
--- a/lib/matplotlib/colors.py
+++ b/lib/matplotlib/colors.py
@@ -1449,12 +1449,14 @@ def _make_norm_from_scale(scale_cls, base_norm_cls=None, *, init=None):
t_vmin, t_vmax = self._trf.transform([self.vmin, self.vmax])
if not np.isfinite([t_vmin, t_vmax]).all():
raise ValueError("Invalid vmin or vmax")
+ value, is_scalar = self.process_value(value)
rescaled = value * (t_vmax - t_vmin)
rescaled += t_vmin
- return (self._trf
- .inverted()
- .transform(rescaled)
- .reshape(np.shape(value)))
+ t_value = (self._trf
+ .inverted()
+ .transform(rescaled)
+ .reshape(np.shape(value)))
+ return t_value[0] if is_scalar else t_value
Norm.__name__ = base_norm_cls.__name__
Norm.__qualname__ = base_norm_cls.__qualname__ Maybe it also needs the masking? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks right. But maybe orthogonal to this PR. I opened an issue in #19239 just to keep them separate... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since that is now fixed, do you want to remove the |
||
assert_array_almost_equal(norm.inverse([0.2, 5, 10]), | ||
lognorm.inverse([0.2, 5, 10])) | ||
|
||
|
||
def test_TwoSlopeNorm_autoscale(): | ||
norm = mcolors.TwoSlopeNorm(vcenter=20) | ||
norm.autoscale([10, 20, 30, 40]) | ||
|
Uh oh!
There was an error while loading. Please reload this page.