Artifacts in matplotlib patch plotting

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()


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):

快照属性的比较

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.

链接地址: http://www.djcxy.com/p/11576.html

上一篇: 在MacOSX上设置SLIME

下一篇: matplotlib修补程序绘图中的工件