Skip to content

Adjustments to contour.py so that levels are set accordingly to input vmin and vmax, if provided, so color bar works correctly. #2176

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -1493,9 +1493,26 @@ def _contour_args(self, args, kwargs):
else:
raise TypeError("Too many arguments to %s; see help(%s)" %
(fn, fn))

# Check for vmin and vmax values and max out z values accordingly
# want zmax and zmin to be able to be outside data range, according to input vmin/vmax
if 'vmax' in kwargs:
vmax = kwargs['vmax']
ind = z > vmax
z[ind] = vmax
self.zmax = kwargs['vmax']
else:
self.zmax = ma.maximum(z)
if 'vmin' in kwargs:
vmin = kwargs['vmin']
ind = z < vmin
z[ind] = vmin
self.zmin = kwargs['vmin']
else:
self.zmin = ma.minimum(z)

z = ma.masked_invalid(z, copy=False)
self.zmax = ma.maximum(z)
self.zmin = ma.minimum(z)

if self.logscale and self.zmin <= 0:
z = ma.masked_where(z <= 0, z)
warnings.warn('Log scale: values of z <= 0 have been masked')
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,30 @@ def test_given_colors_levels_and_extends():
plt.colorbar()


@image_comparison(baseline_images=['contour_vmax_over_colorbar_range'],
extensions=['png'])
def test_vmax_over_colorbar_range():
xvec = np.linspace(0, 10)
yvec = np.linspace(0, 20)
X, Y = np.meshgrid(xvec, yvec)
Z = X**2 + Y**2

plt.contourf(X, Y, Z, cmap='pink_r', vmax=700)
plt.colorbar()


@image_comparison(baseline_images=['contour_vmax_under_colorbar_range'],
extensions=['png'])
def test_vmax_under_colorbar_range():
xvec = np.linspace(0, 10)
yvec = np.linspace(0, 20)
X, Y = np.meshgrid(xvec, yvec)
Z = X**2 + Y**2

plt.contourf(X, Y, Z, cmap='pink_r', vmax=300)
plt.colorbar()


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)