Skip to content

matplotlib.collections.QuadMesh.set_array() input arg format is weird and undocumented #15388

Closed

Description

@Tigraan

Bug report

Bug summary

Trying to give matplotlib.collections.QuadMesh.set_array() the same argument as was used by matplotlib.axes.Axes.pcolormesh() results in an error. Moreover, the argument format depends on the shading; an argument that is correct for shading='gouraud' will be accepted and produce spurious graphics for shading='flat'. In my opinion, the user should be able to use the same argument format for the update (via set_array or another method if that causes backwards compatibility issues) than used for creation (via pcolormesh), and not have to care about the shading when setting the data.

It was raised in a StackOverflow post from 2013 and the top answer (from 2015) gives workarounds, but I did not find anything in the issue tracker (here). It should at least be documented, the current doc only says "Set the image array from numpy array A".

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt


def f(X, Y, t=0):
    return np.sin(X+t*np.pi)*np.sin(Y)

xmin, xmax = 0.0, 4*np.pi
ymin, ymax = 0.0, 5*np.pi
Nx, Ny = 50, 40
x, y = np.linspace(xmin, xmax, num=Nx), np.linspace(ymin, ymax, num=Ny)
X, Y = np.meshgrid(x, y, indexing='ij')
zstart = f(X, Y)

run_number = np.random.rand()

# Intialize figure stuff
fig = plt.figure()
axs = [
    plt.subplot(221, title='control flat'),
    plt.subplot(222, title='test flat'),
    plt.subplot(223, title='control gouraud'),
    plt.subplot(224, title='test gouraud'),
    ]

def drawcmesh(X, Y, z, ax, shading):
    ax.set_xlim(left=xmin, right=xmax)
    ax.set_ylim(bottom=ymin, top=ymax)
    cmesh = ax.pcolormesh(X, Y, z,
                          shading=shading, vmin=-1.5, vmax=1.5, cmap='hot')
    return cmesh



zstart = f(X, Y)
perm_mesh = [
    drawcmesh(X, Y, zstart, axs[1], shading='flat'),
    drawcmesh(X, Y, zstart, axs[3], shading='gouraud')
    ]
drawcmesh(X, Y, zstart, axs[0], shading='flat')
drawcmesh(X, Y, zstart, axs[2], shading='gouraud')

def update(t):
    z = f(X, Y, t)

    for i in range(4):
        ax = axs[i]
        if i<2:
            shading='flat'
        else:
            shading='gouraud'

        if i%2 == 0:
            # Control case: redraw by hand
            axs[i].clear()
            drawcmesh(X, Y, z, axs[i], shading=shading)
            axs[i].set_title('control {}'.format(shading))

        if i==1:
            # Flat shading

            # # The following would copy the syntax of plt.pcolormesh() but it
            # # results in an error
            # perm_mesh[0].set_array(z)

            # The following does not error out but the plot is later incorrect
            perm_mesh[0].set_array(z.ravel())

            # # The following works
            # perm_mesh[0].set_array(z[:-1, :-1].ravel())

        if i==3:
            # Gouraud shading

            # # The following would copy the syntax of plt.pcolormesh() but it
            # # results in an error
            # perm_mesh[1].set_array(z)

            # The following works
            perm_mesh[1].set_array(z.ravel())

            # # The following results in an error at draw time:

            
            # perm_mesh[1].set_array(z[:-1, :-1].ravel())

# # Uncomment this to check that the initial drawing for the test case is correct
# plt.show(block=True)

plt.show(block=False)
for t in np.linspace(0.0, 4.0, num=50):
    update(t)
    fig.canvas.draw()
    fig.canvas.flush_events()

Results
Cf commented code under # Flat shading and # Gouraud shading in the source for the various possibilities.

Spurious graphics for flat shading:

test-shadings

Error given by gouraud shading when dimensions do not match:

<current file>, line 93, in <module>
    fig.canvas.draw()
File "<anaconda-site-packages>\matplotlib\backends\backend_agg.py", line 388, in draw
    self.figure.draw(self.renderer)
File "<anaconda-site-packages>\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
File "<anaconda-site-packages>\matplotlib\figure.py", line 1709, in draw
    renderer, self, artists, self.suppressComposite)
File "<anaconda-site-packages>\matplotlib\image.py", line 135, in _draw_list_compositing_images
    a.draw(renderer)
File "<anaconda-site-packages>\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
File "<anaconda-site-packages>\matplotlib\axes\_base.py", line 2647, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
File "<anaconda-site-packages>\matplotlib\image.py", line 135, in _draw_list_compositing_images
    a.draw(renderer)
File "<anaconda-site-packages>\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
File "<anaconda-site-packages>\matplotlib\collections.py", line 2037, in draw
    self._meshWidth, self._meshHeight, coordinates)
File "<anaconda-site-packages>\matplotlib\collections.py", line 1985, in convert_mesh_to_triangles
    c = self.get_facecolor().reshape((meshHeight + 1, meshWidth + 1, 4))
ValueError: cannot reshape array of size 7644 into shape (50,40,4)

Matplotlib version

  • Operating system: Win10
  • Matplotlib version: 3.1.1 (from conda)
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.7.4

Activity

  1. ImportanceOfBeingErnest commented on Oct 7, 2019

    @ImportanceOfBeingErnest
    Member

    In principle the user should use the same input format, so the following works (for X,Y,Z with the same shape)

    im1 = ax1.pcolormesh(X, Y, Z[:-1, :-1], shading="flat")
    im1.set_array(Z[:-1, :-1].flatten())
    
    im2 = ax2.pcolormesh(X, Y, Z, shading="gouraud")
    im2.set_array(Z.flatten())
    

    The confusing bit is that in addition, pcolormesh(X, Y, Z, shading="flat") works as well - and just silently cuts the last row/column out of the array.

    I see four action items here:

    Documentation of pcolormesh:
    So I think the first thing to do would be to state the allowed shapes in the pcolormesh docs

    If shading="flat":

    • X: array of shape (n,) or (m,n); Y: array of shape (m,) or (m,n)
      • C: array of shape (m-1, n-1): Default
      • C: array of shape (m,n): Convenience functionality; Last row and column will be cut

    If shading="gouraud":

    • X: array of shape (n,) or (m,n); Y: array of shape (m,) or (m,n)
      • C: array of shape (m,n): Default

    Better Error message for shading="gouraud":

    Currently pcolormesh(X, Y, Z[:-1, :-1], shading="gouraud") gives

    TypeError: Dimensions of C (10, 10) are incompatible with X (11) and/or Y (11)
    

    This is confusing, because those are really the default valid shapes for the default shading. So it should say that those dimensions are incompatible due to the shading option in use.

    set_array method

    For the QuadMesh.set_array method one may decide to allow for 2D input. It could even cut the data in case of "flat" shading. It could also directly error out on incompatible shapes.

    Documentation of set_array method

    Depending on the choices above this needs to be updated.

  2. added this to the v3.3.0 milestone on Oct 7, 2019
  3. jklymak commented on Oct 7, 2019

    @jklymak
    Member

    See also #9629 which I've kind of abandoned for backwards compatibility reasons, but I think is actually the right thing to do rather than silently drop data....

  4. tacaswell commented on Jun 2, 2020

    @tacaswell
    Member

    I think this is partially fixed by #16908 which lets you set 2D data (rather than requiring you to ravel it), but I think we still have shape miss-match issues based on the shading.

  5. modified the milestones: v3.3.0, v3.4.0 on Jun 2, 2020
  6. modified the milestones: v3.4.0, v3.5.0 on Jan 27, 2021
  7. modified the milestones: v3.5.0, v3.6.0 on Sep 25, 2021
  8. modified the milestones: v3.6.0, v3.6.1 on Sep 14, 2022
  9. modified the milestones: v3.6.1, v3.6.2 on Oct 6, 2022
  10. modified the milestones: v3.6.2, v3.6.3 on Oct 27, 2022
  11. modified the milestones: v3.6.3, v3.7.0 on Jan 11, 2023
  12. QuLogic commented on Jan 24, 2023

    @QuLogic
    Member

    As of #24829 and #24356, the input to QuadMesh.set_array is required to match coordinate shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …Documentation

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions