Skip to content

Commit 3f8e522

Browse files
authored
Merge pull request #196 from boutproject/logscale-minor-refactor
Move handling logscale argument to animate_pcolormesh, animate_poloidal
2 parents 787fefc + 780d29e commit 3f8e522

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

xbout/boutdataset.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
_normalise_time_coord,
2626
_parse_coord_option,
2727
)
28-
from .plotting.utils import _create_norm
2928
from .region import _from_region
3029
from .utils import _get_bounding_surfaces, _split_into_restarts
3130

@@ -1043,15 +1042,6 @@ def is_list(variable):
10431042
# set 'v' to use for the timeline below
10441043
v = v[0]
10451044
elif ndims == 3:
1046-
if this_vmin is None:
1047-
this_vmin = data.min().values
1048-
if this_vmax is None:
1049-
this_vmax = data.max().values
1050-
1051-
norm = _create_norm(
1052-
this_logscale, kwargs.get("norm", None), this_vmin, this_vmax
1053-
)
1054-
10551045
if this_poloidal_plot:
10561046
var_blocks = animate_poloidal(
10571047
data,
@@ -1062,7 +1052,7 @@ def is_list(variable):
10621052
axis_coords=this_axis_coords,
10631053
vmin=this_vmin,
10641054
vmax=this_vmax,
1065-
norm=norm,
1055+
logscale=this_logscale,
10661056
aspect=this_aspect,
10671057
extend=this_extend,
10681058
**kwargs,
@@ -1080,7 +1070,7 @@ def is_list(variable):
10801070
axis_coords=this_axis_coords,
10811071
vmin=this_vmin,
10821072
vmax=this_vmax,
1083-
norm=norm,
1073+
logscale=this_logscale,
10841074
aspect=this_aspect,
10851075
extend=this_extend,
10861076
**kwargs,

xbout/plotting/animate.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55

66
import animatplot as amp
77

8-
from .utils import _decompose_regions, _is_core_only, plot_separatrices, plot_targets
8+
from .utils import (
9+
_create_norm,
10+
_decompose_regions,
11+
_is_core_only,
12+
plot_separatrices,
13+
plot_targets,
14+
)
915
from matplotlib.animation import PillowWriter
1016

1117

@@ -80,6 +86,7 @@ def animate_poloidal(
8086
axis_coords=None,
8187
vmin=None,
8288
vmax=None,
89+
logscale=False,
8390
animate=True,
8491
save_as=None,
8592
fps=10,
@@ -125,6 +132,12 @@ def animate_poloidal(
125132
Minimum value for the color scale
126133
vmax : float, optional
127134
Maximum value for the color scale
135+
logscale : bool or float, optional
136+
If True, default to a logarithmic color scale instead of a linear one.
137+
If a non-bool type is passed it is treated as a float used to set the linear
138+
threshold of a symmetric logarithmic scale as
139+
linthresh=min(abs(vmin),abs(vmax))*logscale, defaults to 1e-5 if True is
140+
passed.
128141
animate : bool, optional
129142
If set to false, do not create the animation, just return the blocks
130143
save_as : True or str, optional
@@ -188,11 +201,7 @@ def animate_poloidal(
188201
extend = "neither"
189202

190203
# create colorbar
191-
norm = (
192-
kwargs["norm"]
193-
if "norm" in kwargs
194-
else matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
195-
)
204+
norm = _create_norm(logscale, kwargs.get("norm", None), vmin, vmax)
196205
sm = plt.cm.ScalarMappable(norm=norm, cmap=cmap)
197206
sm.set_array([])
198207
cmap = sm.get_cmap()
@@ -279,6 +288,7 @@ def animate_pcolormesh(
279288
vmin=None,
280289
vmax=None,
281290
vsymmetric=False,
291+
logscale=False,
282292
fps=10,
283293
save_as=None,
284294
ax=None,
@@ -325,6 +335,12 @@ def animate_pcolormesh(
325335
data across whole timeseries.
326336
vsymmetric : bool, optional
327337
If set to true, make the color-scale symmetric
338+
logscale : bool or float, optional
339+
If True, default to a logarithmic color scale instead of a linear one.
340+
If a non-bool type is passed it is treated as a float used to set the linear
341+
threshold of a symmetric logarithmic scale as
342+
linthresh=min(abs(vmin),abs(vmax))*logscale, defaults to 1e-5 if True is
343+
passed.
328344
fps : int, optional
329345
Frames per second of resulting gif
330346
save_as : True or str, optional
@@ -399,17 +415,15 @@ def animate_pcolormesh(
399415
# well with dask arrays!
400416
image_data = data.values
401417

402-
if "norm" not in kwargs:
403-
# If not specified, determine max and min values across entire data series
404-
if vmax is None:
405-
vmax = np.max(image_data)
406-
if vmin is None:
407-
vmin = np.min(image_data)
408-
if vsymmetric:
409-
vmax = max(np.abs(vmin), np.abs(vmax))
410-
vmin = -vmax
411-
kwargs["vmin"] = vmin
412-
kwargs["vmax"] = vmax
418+
# If not specified, determine max and min values across entire data series
419+
if vmax is None:
420+
vmax = np.max(image_data)
421+
if vmin is None:
422+
vmin = np.min(image_data)
423+
if vsymmetric:
424+
vmax = max(np.abs(vmin), np.abs(vmax))
425+
vmin = -vmax
426+
kwargs["norm"] = _create_norm(logscale, kwargs.get("norm", None), vmin, vmax)
413427

414428
if not ax:
415429
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)