Skip to content

[Bug]: PolyCollection shows white space between polygons #27703

Closed as not planned

Description

@veenstrajelmer

Bug summary

When plotting unstructured meshes with matplotlib PolyCollection, we would like to avoid the edges/lines to be vislible. This can be done with edgecolor="face" but for fine meshes (many small polygons) the width of the edges can become significant and the image is incorrect. This is documented in Deltares/xugrid#203. A better solution would be to use edgecolor="none" or linewidths=0. However, in this case there is still white space showing up between the polygons, even though they should align perfectly.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
plt.close("all")
from matplotlib.collections import PolyCollection

vertices = np.array(
    [[[ 3.925     , 52.0125    ],
        [ 3.925     , 52.01458333],
        [ 3.9234375 , 52.0125    ],
        [ 3.925     , 52.0125    ],
        [ 3.925     , 52.0125    ]],

       [[ 3.925     , 52.01041667],
        [ 3.925     , 52.01145833],
        [ 3.9234375 , 52.01145833],
        [ 3.9234375 , 52.01041667],
        [ 3.925     , 52.01041667]],

       [[ 3.925     , 52.0125    ],
        [ 3.9234375 , 52.0125    ],
        [ 3.9234375 , 52.01145833],
        [ 3.925     , 52.01145833],
        [ 3.925     , 52.0125    ]],

       [[ 3.921875  , 52.0125    ],
        [ 3.9234375 , 52.0125    ],
        [ 3.921875  , 52.01458333],
        [ 3.921875  , 52.0125    ],
        [ 3.921875  , 52.0125    ]],

       [[ 3.925     , 52.01041667],
        [ 3.9234375 , 52.01041667],
        [ 3.9234375 , 52.009375  ],
        [ 3.925     , 52.009375  ],
        [ 3.925     , 52.01041667]],

       [[ 3.925     , 52.00833333],
        [ 3.925     , 52.009375  ],
        [ 3.9234375 , 52.009375  ],
        [ 3.9234375 , 52.00833333],
        [ 3.925     , 52.00833333]],

       [[ 3.925     , 52.01458333],
        [ 3.921875  , 52.01458333],
        [ 3.9234375 , 52.0125    ],
        [ 3.925     , 52.01458333],
        [ 3.925     , 52.01458333]],

       [[ 3.921875  , 52.01041667],
        [ 3.9234375 , 52.01041667],
        [ 3.9234375 , 52.01145833],
        [ 3.921875  , 52.01145833],
        [ 3.921875  , 52.01041667]],

       [[ 3.921875  , 52.0125    ],
        [ 3.921875  , 52.01145833],
        [ 3.9234375 , 52.01145833],
        [ 3.9234375 , 52.0125    ],
        [ 3.921875  , 52.0125    ]],

       [[ 3.921875  , 52.0125    ],
        [ 3.921875  , 52.01458333],
        [ 3.9203125 , 52.0125    ],
        [ 3.921875  , 52.0125    ],
        [ 3.921875  , 52.0125    ]],

       [[ 3.921875  , 52.01041667],
        [ 3.921875  , 52.009375  ],
        [ 3.9234375 , 52.009375  ],
        [ 3.9234375 , 52.01041667],
        [ 3.921875  , 52.01041667]],

       [[ 3.921875  , 52.00833333],
        [ 3.9234375 , 52.00833333],
        [ 3.9234375 , 52.009375  ],
        [ 3.921875  , 52.009375  ],
        [ 3.921875  , 52.00833333]],

       [[ 3.925     , 52.01666667],
        [ 3.921875  , 52.01666667],
        [ 3.921875  , 52.01458333],
        [ 3.925     , 52.01458333],
        [ 3.925     , 52.01666667]],

       [[ 3.921875  , 52.01041667],
        [ 3.921875  , 52.01145833],
        [ 3.9203125 , 52.01145833],
        [ 3.9203125 , 52.01041667],
        [ 3.921875  , 52.01041667]],

       [[ 3.921875  , 52.0125    ],
        [ 3.9203125 , 52.0125    ],
        [ 3.9203125 , 52.01145833],
        [ 3.921875  , 52.01145833],
        [ 3.921875  , 52.0125    ]],

       [[ 3.91875   , 52.01458333],
        [ 3.9203125 , 52.0125    ],
        [ 3.921875  , 52.01458333],
        [ 3.91875   , 52.01458333],
        [ 3.91875   , 52.01458333]],

       [[ 3.921875  , 52.01041667],
        [ 3.9203125 , 52.01041667],
        [ 3.9203125 , 52.009375  ],
        [ 3.921875  , 52.009375  ],
        [ 3.921875  , 52.01041667]],

       [[ 3.921875  , 52.00833333],
        [ 3.921875  , 52.009375  ],
        [ 3.9203125 , 52.009375  ],
        [ 3.9203125 , 52.00833333],
        [ 3.921875  , 52.00833333]],

       [[ 3.91875   , 52.01666667],
        [ 3.91875   , 52.01458333],
        [ 3.921875  , 52.01458333],
        [ 3.921875  , 52.01666667],
        [ 3.91875   , 52.01666667]],

       [[ 3.91875   , 52.01041667],
        [ 3.9203125 , 52.01041667],
        [ 3.9203125 , 52.01145833],
        [ 3.91875   , 52.01145833],
        [ 3.91875   , 52.01041667]],

       [[ 3.91875   , 52.0125    ],
        [ 3.91875   , 52.01145833],
        [ 3.9203125 , 52.01145833],
        [ 3.9203125 , 52.0125    ],
        [ 3.91875   , 52.0125    ]],

       [[ 3.91875   , 52.0125    ],
        [ 3.9203125 , 52.0125    ],
        [ 3.91875   , 52.01458333],
        [ 3.91875   , 52.0125    ],
        [ 3.91875   , 52.0125    ]],

       [[ 3.91875   , 52.01041667],
        [ 3.91875   , 52.009375  ],
        [ 3.9203125 , 52.009375  ],
        [ 3.9203125 , 52.01041667],
        [ 3.91875   , 52.01041667]],

       [[ 3.91875   , 52.00833333],
        [ 3.9203125 , 52.00833333],
        [ 3.9203125 , 52.009375  ],
        [ 3.91875   , 52.009375  ],
        [ 3.91875   , 52.00833333]],

       [[ 3.91875   , 52.01666667],
        [ 3.915625  , 52.01666667],
        [ 3.915625  , 52.01458333],
        [ 3.91875   , 52.01458333],
        [ 3.91875   , 52.01666667]],

       [[ 3.91875   , 52.01041667],
        [ 3.91875   , 52.01145833],
        [ 3.915625  , 52.01041667],
        [ 3.91875   , 52.01041667],
        [ 3.91875   , 52.01041667]],

       [[ 3.91875   , 52.0125    ],
        [ 3.915625  , 52.0125    ],
        [ 3.91875   , 52.01145833],
        [ 3.91875   , 52.0125    ],
        [ 3.91875   , 52.0125    ]],

       [[ 3.91875   , 52.0125    ],
        [ 3.91875   , 52.01458333],
        [ 3.915625  , 52.01458333],
        [ 3.915625  , 52.0125    ],
        [ 3.91875   , 52.0125    ]],

       [[ 3.91875   , 52.01041667],
        [ 3.915625  , 52.01041667],
        [ 3.91875   , 52.009375  ],
        [ 3.91875   , 52.01041667],
        [ 3.91875   , 52.01041667]],

       [[ 3.91875   , 52.00833333],
        [ 3.91875   , 52.009375  ],
        [ 3.915625  , 52.00833333],
        [ 3.91875   , 52.00833333],
        [ 3.91875   , 52.00833333]],

       [[ 3.9125    , 52.01666667],
        [ 3.9125    , 52.01458333],
        [ 3.915625  , 52.01458333],
        [ 3.915625  , 52.01666667],
        [ 3.9125    , 52.01666667]],

       [[ 3.915625  , 52.0125    ],
        [ 3.915625  , 52.01041667],
        [ 3.91875   , 52.01145833],
        [ 3.915625  , 52.0125    ],
        [ 3.915625  , 52.0125    ]],

       [[ 3.9125    , 52.0125    ],
        [ 3.915625  , 52.0125    ],
        [ 3.915625  , 52.01458333],
        [ 3.9125    , 52.01458333],
        [ 3.9125    , 52.0125    ]],

       [[ 3.915625  , 52.00833333],
        [ 3.91875   , 52.009375  ],
        [ 3.915625  , 52.01041667],
        [ 3.915625  , 52.00833333],
        [ 3.915625  , 52.00833333]],

       [[ 3.9125    , 52.0125    ],
        [ 3.9125    , 52.01041667],
        [ 3.915625  , 52.01041667],
        [ 3.915625  , 52.0125    ],
        [ 3.9125    , 52.0125    ]],

       [[ 3.9125    , 52.00833333],
        [ 3.915625  , 52.00833333],
        [ 3.915625  , 52.01041667],
        [ 3.9125    , 52.01041667],
        [ 3.9125    , 52.00833333]]])

kwargs = dict(edgecolors="none")

fig, ax = plt.subplots()
collection = PolyCollection(vertices, **kwargs)
collection.set_array(np.ones(len(vertices)))
primitive = ax.add_collection(collection)

xmin = vertices[:, :, 0].min()
ymin = vertices[:, :, 1].min()
xmax = vertices[:, :, 0].max()
ymax = vertices[:, :, 1].max()
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)

Actual outcome

image

Expected outcome

image

This second figure is created with edgecolors="face". However, keep in mind that the original xugrid issue linked in the summary also shows that in this specific application this is not desireable. I would expect that this figure without lines would also be the result of edgecolors="none" since all polygons are exactly next to each other.

Additional information

No response

Operating system

Windows 10

Matplotlib Version

3.8.2

Matplotlib Backend

Qt5Agg

Python version

3.11.6

Jupyter version

No response

Installation

pip

Activity

  1. timhoffm commented on Jan 26, 2024

    @timhoffm
    Member

    Thanks for the report. This is a fundamental rendering limitation. See #9574 (comment) for a good explanation. Unfortunately there's nothing we can realistically do about it.

    Variants of this have been discussed multiple times, e.g. #10115, #1188, #5694.

  2. veenstrajelmer commented on Jan 26, 2024

    @veenstrajelmer
    Author

    Thanks for the quick reply and the links, good to know. I searched for open issues but not for closed ones, although I am not sure if I would have found all of this. Either way, we will see how to work around this in that case.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions