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
Remove spacing between polygons #2823
Comments
In your code, you set edgecolor to be 'none'. That means that the edge Lastly, which version of matplotlib are you using? Cheers! On Thu, Feb 20, 2014 at 4:03 AM, Maik Riechert notifications@github.comwrote:
|
Changing the dpi seems to keep the edge size always at 1px with Agg. I use matplotlib 1.1.1rc, the latest version in the Ubuntu 12.04 repos. Also, my real data is RGB colors and the coordinates are not in any Cheers On 20/02/14 15:45, Benjamin Root wrote:
|
I just tested with 1.3.1 and the problem doesn't appear anymore with agg and pdf. With svg I can still see a slight spacing (although it seems smaller than before). So somewhere between 1.1.1rc and 1.3.1 there must have been a fix. It's a shame that the Ubuntu repos lag behind so much. |
It seems I cheered too early. When using basemap, the issue persists: I'm not sure yet if this is really related to basemap or rather the non-rectangular polygon shape (due to map projection). Wouldn't life be a lot easier if matplotlib would accept polygoncollections defined by vertices and indices? Then it is absolutely clear for the renderer which polygon edges are identical in case two polygons share an edge. |
@neothemachine Did you pin this down with basemap? |
Punting this to 1.5 as it sounds like a) you have a usable work-around and b) fixing it is probably non-trivial. |
This sounds like a perennial problem for which no one has found a solution, because the problem is in the renderers; is there any point in leaving this as an open issue? |
@tacaswell Regarding the work-around, it comes with a severe loss in accuracy as I just make the polygons bigger (at the bottom and right), you can see it clearly at the bottom of the plots. You can close it if you want, I just don't want it to get lost as I think this issue is quite important. |
I think this should be left open (or at a minimum replaced) as a feature request for a way to pass in an arbitrary mesh + colors. That is why I punted to 1.5. For you expanding workaround, why not push all of the edges out equally? Find the center of mass of the polygon and then move each point out by some delta along the line from the center to the point
which should help reduce the distortion. |
Well, yes, but I couldn't find a fast way to do that using numpy. I |
Trying to solve this by pushing the edges out, so there is some overlap, will cause new artifacts if alpha is not 1. |
I just tried the attached code on master and did not see the issue that was reported. I therefore think this is now resolved. @tacaswell wrote:
This hasn't been done AFAIK, but should definitely be a separate issue if it does actually need to be tracked (I'm sceptical). Finally, I think the original question was perhaps a little contrived? If one really wanted to use a bunch of polygons to represent an image you might consider using a pcolormesh. An example of a curved pcolormesh is here (I'm showing it without data to highlight that there are no unsightly borders):
Yet another alternative is to use a library that does the image transformation for you - cartopy has an example of doing this at https://scitools.org.uk/cartopy/docs/latest/gallery/geostationary.html#sphx-glr-gallery-geostationary-py. Whilst I have the power to close the issue, I'm resisting given the suggestion from @tacaswell. I'll leave it to him to make the final call. |
PolyCollections are basically bound to always have this problem; drawing a quadmesh is indeed the way to go. On the other hand, perhaps this suggests that what's needed is a way for PolyCollections to convert themselves to QuadMeshes (triangulating ("quadrangulating"?) larger polygons on-demand -- that's the "interesting" part) for drawing? |
any updates on this? |
There is an experimental backend that might address this problem that you
could try out: https://github.com/matplotlib/mplcairo
…On Fri, Oct 1, 2021 at 3:10 PM Raphael Quast ***@***.***> wrote:
any updates on this?
I'm experiencing similar problems when plotting a collection of ~100K
polygons.
Using coll.set_edgecolors(coll.get_facecolors()) and a linewidth
according to the zoom seems to solve the problem on my side but I'd very
much prefer a general solution that does not need manual tweaking of
linewidths...
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2823 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACHF6E4OPJWKWLCMJLVZ53UEYBRNANCNFSM4AMNX35Q>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
hey @WeatherGod, thanks for the suggestion! For now, I found that converting the |
As @anntzer points out above, this is extremely hard to get rid of in general, and is a pretty big problem in graphics: if you are going to antialias a polygon, you need to know what its background is. Matplotlib's backends have no sophisticated way to know this, so they antialias to the background color of the axes. Its fun to think about fixing this somehow, but it would have to be pretty low level in the rendering, so I think you would need to write your own backend. |
@jklymak The only thing I don't really understand is how To illustrate what I mean I've create a minimal example (code is in "Details"): The left rows are plotted using a import numpy as np
from matplotlib.collections import PolyCollection
from matplotlib.tri import TriMesh, Triangulation
import matplotlib.pyplot as plt
def tri_rectangles(verts, z=None):
# convert rectangles to 2 triangles by splitting it in the diagonal
x = np.vstack([verts[:, 2][:, 0],
verts[:, 3][:, 0],
verts[:, 1][:, 0]]).T.ravel()
y = np.vstack([verts[:, 2][:, 1],
verts[:, 3][:, 1],
verts[:, 1][:, 1]]).T.ravel()
x2 = np.vstack([verts[:, 3][:, 0],
verts[:, 0][:, 0],
verts[:, 1][:, 0]]).T.ravel()
y2 = np.vstack([verts[:, 3][:, 1],
verts[:, 0][:, 1],
verts[:, 1][:, 1]]).T.ravel()
x = np.append(x, x2)
y = np.append(y, y2)
tri = Triangulation(
x, y, triangles=np.array(range(len(x))).reshape((len(x) // 3, 3))
)
if z is not None:
z = np.tile(np.repeat(z, 3), 2)
return tri, z
verts = np.array([[[ 9.63981913, 44.62741306],
[ 9.6274869 , 44.62568775],
[ 9.62980296, 44.61689049],
[ 9.64213333, 44.61861546]],
[[ 9.65215211, 44.62913692],
[ 9.63981913, 44.62741306],
[ 9.64213333, 44.61861546],
[ 9.65446446, 44.62033897]],
[[ 9.66448586, 44.63085932],
[ 9.65215211, 44.62913692],
[ 9.65446446, 44.62033897],
[ 9.66679635, 44.62206104]],
])
f, ax = plt.subplots(figsize=(12,5))
# --------------------------------- plot with a PolyCollection
coll = PolyCollection(verts)
coll.set_array(np.array([1,1,1]))
ax.add_collection(coll)
coll = PolyCollection(verts + .0125)
coll.set_array(np.array([1,1,1]))
coll.set_alpha(.5)
ax.add_collection(coll)
coll = PolyCollection(verts + .025)
coll.set_array(np.array([1,2,3]))
ax.add_collection(coll)
# --------------------------------- plot with a TriMesh
verts[:,:,0] += 0.045
tri, z_tri = tri_rectangles(verts, [1,1,1])
tri_coll = TriMesh(tri)
tri_coll.set_array(z_tri)
ax.add_collection(tri_coll)
tri, z_tri = tri_rectangles(verts + .0125, [1,1,1])
tri_coll = TriMesh(tri)
tri_coll.set_array(z_tri)
tri_coll.set_alpha(.5)
ax.add_collection(tri_coll)
tri, z_tri = tri_rectangles(verts + .025, [1,2,3])
tri_coll = TriMesh(tri)
tri_coll.set_array(z_tri)
ax.add_collection(tri_coll)
ax.set_xlim(9.623228860352405, 9.740110942312576)
ax.set_ylim(44.615001488088424, 44.6577688106626)
f.tight_layout() |
PolyCollection is just a bunch of polygons, usually not even adjacent to each other, hence they have no concept of neighbours. Quadmesh and Trimesh know they are part of one large object and hence the cells know about their neighbours and can do a better job of antialiasing. |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
Closing as a duplicate of #1188 |
I believe the spacing between the polygons shouldn't be there. Below is my code. Is this a bug or am I missing some option? It happens on all backends.
I know that I can enlarge the polygons slightly so that they overlap each other which then removes these spaces, but this approach is only suitable for very simple polygons like the rectangular ones above, and in my real use-case I have arbitrarily shaped polygons, so that's not really an option.
The text was updated successfully, but these errors were encountered: