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

Skip to content

Commit 95497a6

Browse files
authored
Merge pull request #10705 from JelsB/bug-fix-for-issue-7942
FIX: enable extend kwargs with log scale colorbar
2 parents 60e682a + 694b3bf commit 95497a6

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

examples/images_contours_and_fields/contourf_log.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@
4242
# lev_exp = np.arange(np.floor(np.log10(z.min())-1),
4343
# np.ceil(np.log10(z.max())+1))
4444
# levs = np.power(10, lev_exp)
45-
# cs = P.contourf(X, Y, z, levs, norm=colors.LogNorm())
46-
47-
# The 'extend' kwarg does not work yet with a log scale.
45+
# cs = ax.contourf(X, Y, z, levs, norm=colors.LogNorm())
4846

4947
cbar = fig.colorbar(cs)
5048

lib/matplotlib/contour.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,9 +823,6 @@ def __init__(self, ax, *args, **kwargs):
823823
self.logscale = True
824824
if norm is None:
825825
norm = colors.LogNorm()
826-
if self.extend is not 'neither':
827-
raise ValueError('extend kwarg does not work yet with log '
828-
' scale')
829826
else:
830827
self.logscale = False
831828

@@ -1206,7 +1203,10 @@ def _process_levels(self):
12061203
# ...except that extended layers must be outside the
12071204
# normed range:
12081205
if self.extend in ('both', 'min'):
1209-
self.layers[0] = -1e150
1206+
if self.logscale:
1207+
self.layers[0] = 1e-150
1208+
else:
1209+
self.layers[0] = -1e150
12101210
if self.extend in ('both', 'max'):
12111211
self.layers[-1] = 1e150
12121212

lib/matplotlib/tests/test_contour.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from matplotlib.testing.decorators import image_comparison
77
from matplotlib import pyplot as plt
88
from numpy.testing import assert_array_almost_equal
9+
from matplotlib.colors import LogNorm
910
import pytest
1011
import warnings
1112

@@ -367,3 +368,36 @@ def test_circular_contour_warning():
367368
cs = plt.contour(x, y, r)
368369
plt.clabel(cs)
369370
assert len(record) == 0
371+
372+
373+
@image_comparison(baseline_images=['contour_log_extension'],
374+
extensions=['png'], remove_text=True, style='mpl20')
375+
def test_contourf_log_extension():
376+
# Test that contourf with lognorm is extended correctly
377+
fig = plt.figure(figsize=(10, 5))
378+
fig.subplots_adjust(left=0.05, right=0.95)
379+
ax1 = fig.add_subplot(131)
380+
ax2 = fig.add_subplot(132)
381+
ax3 = fig.add_subplot(133)
382+
383+
# make data set with large range e.g. between 1e-8 and 1e10
384+
data_exp = np.linspace(-8, 10, 1200)
385+
data = np.power(10, data_exp).reshape(30, 40)
386+
# make manual levels e.g. between 1e-4 and 1e-6
387+
levels_exp = np.arange(-4., 7.)
388+
levels = np.power(10., levels_exp)
389+
390+
# original data
391+
c1 = ax1.contourf(data,
392+
norm=LogNorm(vmin=data.min(), vmax=data.max()))
393+
# just show data in levels
394+
c2 = ax2.contourf(data, levels=levels,
395+
norm=LogNorm(vmin=levels.min(), vmax=levels.max()),
396+
extend='neither')
397+
# extend data from levels
398+
c3 = ax3.contourf(data, levels=levels,
399+
norm=LogNorm(vmin=levels.min(), vmax=levels.max()),
400+
extend='both')
401+
plt.colorbar(c1, ax=ax1)
402+
plt.colorbar(c2, ax=ax2)
403+
plt.colorbar(c3, ax=ax3)

0 commit comments

Comments
 (0)