We Are Going To Discuss About **How to remove gaps between adjacent matplotlib patches**. So lets Start this Python Article.

## How to remove gaps between adjacent matplotlib patches

**How to solve How to remove gaps between adjacent matplotlib patches**While setting the

`color`

of the whole collection does remove the gap as @rvd suggested, this limits us to 1 color for all polygons:`col = PatchCollection(patches, color='C0') # limit 1 color per collection`

A more robust solution is to set the`color`

of each polygon and enable`match_original`

in the collection:: If`match_original`

`True`

, use the colors and linewidths of the original patches.

This allows us to have distinct colors per patch instead of 1 global color per collection:`import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mpatches from matplotlib.collections import PatchCollection # use numpy arrays to simplify indexing nodes = np.array([[0.1, 0.1], [0.25, 0.1], [0.75, 0.9], [0.1, 0.9], [0.9, 0.1], [0.9, 0.9]]) elems = np.array([[0, 1, 2, 3], [1, 4, 5, 2]]) colors = np.array(['C0', 'C4']) # color per elem # set color per polygon patches = [mpatches.Polygon(nodes[e], color=c) for e, c in zip(elems, colors)] # ------- # retain original polygon colors collection = PatchCollection(patches, match_original=True) # ------------------- fig, ax = plt.subplots() ax.add_collection(collection) plt.show()`

Of course if you only want one color, you can remove the`zip`

and just set a single color (as long as`match_original`

is enabled in the collection):`patches = [mpatches.Polygon(nodes[e], color='C0') for e in elems] collection = PatchCollection(patches, match_original=True)`

**Update:**As I can't reproduce the artifacts using the sample data, try using these extra options on your end:`rasterized=True`

and`linewidth=0`

in the collection`dpi=600`

(or higher) when saving the figure`import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mpatches from matplotlib.collections import PatchCollection # use numpy arrays to simplify indexing nodes = np.array([[0.1, 0.1], [0.25, 0.1], [0.75, 0.9], [0.1, 0.9], [0.9, 0.1], [0.9, 0.9]]) elems = np.array([[0, 1, 2, 3], [1, 4, 5, 2]]) colors = np.array(['C0', 'C4']) # color per elem # set color per polygon patches = [mpatches.Polygon(nodes[e], color=c) for e, c in zip(elems, colors)] # ------- # retain original polygon colors collection = PatchCollection(patches, snap=True, match_original=True, rasterized=True, linewidth=0) # ------------------- --------------- ----------- fig, ax = plt.subplots() ax.add_collection(collection) fig.savefig('fe.png', dpi=600) # -------`

**How to remove gaps between adjacent matplotlib patches**While setting the

`color`

of the whole collection does remove the gap as @rvd suggested, this limits us to 1 color for all polygons:`col = PatchCollection(patches, color='C0') # limit 1 color per collection`

A more robust solution is to set the`color`

of each polygon and enable`match_original`

in the collection:: If`match_original`

`True`

, use the colors and linewidths of the original patches.

This allows us to have distinct colors per patch instead of 1 global color per collection:`import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mpatches from matplotlib.collections import PatchCollection # use numpy arrays to simplify indexing nodes = np.array([[0.1, 0.1], [0.25, 0.1], [0.75, 0.9], [0.1, 0.9], [0.9, 0.1], [0.9, 0.9]]) elems = np.array([[0, 1, 2, 3], [1, 4, 5, 2]]) colors = np.array(['C0', 'C4']) # color per elem # set color per polygon patches = [mpatches.Polygon(nodes[e], color=c) for e, c in zip(elems, colors)] # ------- # retain original polygon colors collection = PatchCollection(patches, match_original=True) # ------------------- fig, ax = plt.subplots() ax.add_collection(collection) plt.show()`

Of course if you only want one color, you can remove the`zip`

and just set a single color (as long as`match_original`

is enabled in the collection):`patches = [mpatches.Polygon(nodes[e], color='C0') for e in elems] collection = PatchCollection(patches, match_original=True)`

**Update:**As I can't reproduce the artifacts using the sample data, try using these extra options on your end:`rasterized=True`

and`linewidth=0`

in the collection`dpi=600`

(or higher) when saving the figure`import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mpatches from matplotlib.collections import PatchCollection # use numpy arrays to simplify indexing nodes = np.array([[0.1, 0.1], [0.25, 0.1], [0.75, 0.9], [0.1, 0.9], [0.9, 0.1], [0.9, 0.9]]) elems = np.array([[0, 1, 2, 3], [1, 4, 5, 2]]) colors = np.array(['C0', 'C4']) # color per elem # set color per polygon patches = [mpatches.Polygon(nodes[e], color=c) for e, c in zip(elems, colors)] # ------- # retain original polygon colors collection = PatchCollection(patches, snap=True, match_original=True, rasterized=True, linewidth=0) # ------------------- --------------- ----------- fig, ax = plt.subplots() ax.add_collection(collection) fig.savefig('fe.png', dpi=600) # -------`

## Solution 1

While setting the `color`

of the whole collection does remove the gap as @rvd suggested, this limits us to 1 color for all polygons:

```
col = PatchCollection(patches, color='C0') # limit 1 color per collection
```

A more robust solution is to set the `color`

of each polygon and enable `match_original`

in the collection:

: If`match_original`

`True`

, use the colors and linewidths of the original patches.

This allows us to have distinct colors per patch instead of 1 global color per collection:

```
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
# use numpy arrays to simplify indexing
nodes = np.array([[0.1, 0.1], [0.25, 0.1], [0.75, 0.9], [0.1, 0.9], [0.9, 0.1], [0.9, 0.9]])
elems = np.array([[0, 1, 2, 3], [1, 4, 5, 2]])
colors = np.array(['C0', 'C4']) # color per elem
# set color per polygon
patches = [mpatches.Polygon(nodes[e], color=c) for e, c in zip(elems, colors)]
# -------
# retain original polygon colors
collection = PatchCollection(patches, match_original=True)
# -------------------
fig, ax = plt.subplots()
ax.add_collection(collection)
plt.show()
```

Of course if you only want one color, you can remove the `zip`

and just set a single color (as long as `match_original`

is enabled in the collection):

```
patches = [mpatches.Polygon(nodes[e], color='C0') for e in elems]
collection = PatchCollection(patches, match_original=True)
```

**Update:** As I can’t reproduce the artifacts using the sample data, try using these extra options on your end:

`rasterized=True`

and`linewidth=0`

in the collection`dpi=600`

(or higher) when saving the figure

```
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
# use numpy arrays to simplify indexing
nodes = np.array([[0.1, 0.1], [0.25, 0.1], [0.75, 0.9], [0.1, 0.9], [0.9, 0.1], [0.9, 0.9]])
elems = np.array([[0, 1, 2, 3], [1, 4, 5, 2]])
colors = np.array(['C0', 'C4']) # color per elem
# set color per polygon
patches = [mpatches.Polygon(nodes[e], color=c) for e, c in zip(elems, colors)]
# -------
# retain original polygon colors
collection = PatchCollection(patches, snap=True, match_original=True, rasterized=True, linewidth=0)
# ------------------- --------------- -----------
fig, ax = plt.subplots()
ax.add_collection(collection)
fig.savefig('fe.png', dpi=600)
# -------
```

Original Author tdy Of This Content

## Solution 2

You can fix this by using `PatchCollection`

colors and different PatchCollections.

PatchCollection -> no lines, even without `edgecolors`

and without `linewidth=0`

```
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
nodes = [[0.1, 0.1],
[0.25, 0.1],
[0.75, 0.9],
[0.1, 0.9],
[0.9, 0.1],
[0.9, 0.9]]
elems = [[0, 1, 2, 3],
[1, 4, 5, 2]]
collections = []
for elem in elems:
xy = np.array([nodes[i] for i in elem], dtype=float)
collections.append(PatchCollection([mpatches.Polygon(xy)], snap=True, color='black'))
fig, ax = plt.subplots(1, 1)
for col in collections:
ax.add_collection(col)
plt.show()
```

I have no idea why this works.

Edit: Applied tdy suggestions from the comments.

Original Author rvd Of This Content

## Conclusion

So This is all About **This Tutorial.** Hope This Tutorial Helped You. Thank You.