Skip to content

[Bug]: Getting TypeError when creating filled colorbar for contour() plot #21932

Open
@twmacro

Description

@twmacro

Bug summary

When trying to create a filled colorbar for a contour plot, I get this:

TypeError: Dimensions of C (13, 1) are incompatible with X (2) and/or Y (13); see help(pcolormesh)

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

x = np.arange(10.0)
mat = np.hypot(x, x[:, None])
plt.xlim(-1, 10)
plt.ylim(-1, 10)

plt.figure(1, clear=True)
cntr = plt.contour(x, x, mat, levels=np.arange(13) + 0.5, cmap=cm.PRGn)

# for a solid colorbar:
cbar = plt.colorbar()
cbar.filled = True
cbar.draw_all()

Actual outcome

Python 3.8.12 (default, Oct 12 2021, 13:49:34)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.29.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: run tt

TypeError Traceback (most recent call last)
~/code/pyyeti/tt.py in
14 cbar = plt.colorbar()
15 cbar.filled = True
---> 16 cbar.draw_all()

~/anaconda3/lib/python3.8/site-packages/matplotlib/colorbar.py in draw_all(self)
597 if self._extend_upper():
598 ind = ind[:-1]
--> 599 self._add_solids(X, Y, self._values[ind, np.newaxis])
600
601 def _add_solids(self, X, Y, C):

~/anaconda3/lib/python3.8/site-packages/matplotlib/colorbar.py in _add_solids(self, X, Y, C)
613 self._add_solids_patches(X, Y, C, mappable)
614 else:
--> 615 self.solids = self.ax.pcolormesh(
616 X, Y, C, cmap=self.cmap, norm=self.norm, alpha=self.alpha,
617 edgecolors='none', shading='flat')

~/anaconda3/lib/python3.8/site-packages/matplotlib/init.py in inner(ax, data, *args, **kwargs)
1410 def inner(ax, *args, data=None, **kwargs):
1411 if data is None:
-> 1412 return func(ax, *map(sanitize_sequence, args), **kwargs)
1413
1414 bound = new_sig.bind(ax, *args, **kwargs)

~/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_axes.py in pcolormesh(self, alpha, norm, cmap, vmin, vmax, shading, antialiased, *args, **kwargs)
6014 kwargs.setdefault('edgecolors', 'none')
6015
-> 6016 X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
6017 shading=shading, kwargs=kwargs)
6018 coords = np.stack([X, Y], axis=-1)

~/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_axes.py in _pcolorargs(self, funcname, shading, *args, **kwargs)
5527 if shading == 'flat':
5528 if (Nx, Ny) != (ncols + 1, nrows + 1):
-> 5529 raise TypeError('Dimensions of C %s are incompatible with'
5530 ' X (%d) and/or Y (%d); see help(%s)' % (
5531 C.shape, Nx, Ny, funcname))

TypeError: Dimensions of C (13, 1) are incompatible with X (2) and/or Y (13); see help(pcolormesh)

In [2]:

Expected outcome

No errors and have a properly filled colorbar.

Additional information

This method worked before v3.5.0.

Operating system

Ubuntu: 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Matplotlib Version

3.5.0

Matplotlib Backend

Qt5Agg

Python version

Python 3.8.12

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions