Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee2691c

Browse files
authored
Merge pull request #16392 from jklymak/fix-properly-fallback-noscale
FIX colorbars for Norms that do not have a scale.
2 parents 4312b06 + 0e24001 commit ee2691c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/matplotlib/colorbar.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ def __init__(self, ax, cmap=None,
451451
self.locator = None
452452
self.formatter = None
453453
self._manual_tick_data_values = None
454+
self.__scale = None # linear, log10 for now. Hopefully more?
454455

455456
if ticklocation == 'auto':
456457
ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
@@ -584,8 +585,8 @@ def _use_auto_colorbar_locator(self):
584585
one. (check is used twice so factored out here...)
585586
"""
586587
contouring = self.boundaries is not None and self.spacing == 'uniform'
587-
return (type(self.norm) in [colors.Normalize, colors.LogNorm]
588-
and not contouring)
588+
return (type(self.norm) in [colors.Normalize, colors.LogNorm] and
589+
not contouring)
589590

590591
def _reset_locator_formatter_scale(self):
591592
"""
@@ -601,9 +602,14 @@ def _reset_locator_formatter_scale(self):
601602
self.ax.set_xscale('log')
602603
self.ax.set_yscale('log')
603604
self.minorticks_on()
605+
self.__scale = 'log'
604606
else:
605607
self.ax.set_xscale('linear')
606608
self.ax.set_yscale('linear')
609+
if type(self.norm) is colors.Normalize:
610+
self.__scale = 'linear'
611+
else:
612+
self.__scale = 'manual'
607613

608614
def update_ticks(self):
609615
"""
@@ -1118,13 +1124,13 @@ def _mesh(self):
11181124
else:
11191125
y = self._proportional_y()
11201126
xmid = np.array([0.5])
1121-
try:
1127+
if self.__scale != 'manual':
11221128
y = norm.inverse(y)
11231129
x = norm.inverse(x)
11241130
xmid = norm.inverse(xmid)
1125-
except ValueError:
1126-
# occurs for norms that don't have an inverse, in
1127-
# which case manually scale:
1131+
else:
1132+
# if a norm doesn't have a named scale, or
1133+
# we are not using a norm
11281134
dv = self.vmax - self.vmin
11291135
x = x * dv + self.vmin
11301136
y = y * dv + self.vmin

lib/matplotlib/tests/test_colorbar.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ def test_colorbar_get_ticks():
378378
data = np.arange(1200).reshape(30, 40)
379379
levels = [0, 200, 400, 600, 800, 1000, 1200]
380380

381-
plt.subplot()
382381
plt.contourf(data, levels=levels)
383382

384383
# testing getter for user set ticks

0 commit comments

Comments
 (0)