9

When plotting small patch objects in matplotlib, artifacts are introduced due to the display resolution. Using anti-aliasing does not solve the problem.

Is there a solution to this problem?

import matplotlib.pyplot as plt
import matplotlib.patches as patches

ax = plt.axes()

for x in range(-10,11):
    for y in range(-10,11):
        rect = patches.Rectangle((x, y), width=0.1, height=0.1, color='k',aa=True)
        ax.add_patch(rect)

plt.xlim([-30, 30])
plt.ylim([-30, 30])
plt.show()

output

3
  • 2
    I've had this problem as well. This doesn't help your interactive session, but for what it's worth if you save the image in a vectorized format i.e. plt.save("a.pdf") the resulting pdf does not suffer from the same artifacts. Commented Dec 11, 2012 at 18:39
  • Setting the ec='none' might help a little: rect = patches.Rectangle((x, y), width=1, height=1, color='k',ec='none') Commented Dec 11, 2012 at 19:25
  • I could not reproduce this problem. Saved as png and as jpg with matplotlib 1.1.1 on OS X, and the result was fine. Commented Dec 12, 2012 at 0:09

1 Answer 1

6

Thanks for putting together a simple example of the problem - it really makes investigating this much easier!

Is there a solution to this problem?

Yes, it turns out there is! My initial guess, by just looking at the image you attached, was that there is some strange clipping/snapping going on. After ruling out the antialiasing possibility (by flicking the switch that you provided) my only other avenue of testing was to set the "snap" keyword to false (for the very limited docs on the snap method see http://matplotlib.org/api/artist_api.html#matplotlib.artist.Artist.set_snap).

Setting the snap does the trick and you end up with the expected results:

import matplotlib.pyplot as plt
import matplotlib.patches as patches

ax = plt.axes()

for x in range(-10,11):
    for y in range(-10,11):
        rect = patches.Rectangle((x, y), width=0.1, height=0.1, 
                                 color='k', snap=False)
        ax.add_patch(rect)

plt.xlim([-30, 30])
plt.ylim([-30, 30])
plt.show()

A visual comparison (probably best opening the image in a new window as your browser will probably scale the image and introduce further visual effects):

comparison of the snap property

I'm not particularly knowledgeable about the snap property in mpl and whether this is really desirable behaviour, so I will post a question on the mpl-devel mailing list to open up a conversation about this question. Hopefully this answer will help you in the meantime.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow why is snap on by default? This was so difficult to figure out why I was seeing moire effects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.