简体   繁体   中英

Matplotlib savefig pdf doesn't display some polygons

I'm making a relatively simple plot that seemed to work fine. I have a number of line segments, and each defines an area. I'm using polygons for the filled areas using plot.Polygon and ax.add_patch .

When I save to .png, everything works fine. So, I turn to pdf for the final image. I manually set things like fontsize , figsize and dpi , so there should be no problem.

Sadly, 2 out of the four Polygons in the current picture are not displayed in the pdf. Which is strange; the previous two images (other datasets) did act correctly. I've played around a bit with zorder and alpha on the Polygons but to no avail.

My google search did not turn up anything; it mentioned the dpi being different if you haven't set it manually, but this is no problem. Beyond that, I could find no clear reasons.

My sincere thanks, Daimonie

Okay, so I've found the problem.

Let's first denote the method I use to add the polygons, just for clarity. The points $(x,y)$ that define the vertices are called vertices . The polygon is made as:

first_polygon = plt.Polygon(vertices, zorder=.1, facecolor=colours[colour_index], alpha=fill_opacity)

Next, I add it to the figure:

ax.add_patch(first_polygon)

So why didn't it display some polygons? I cannot be sure of the reason, but the issue resolved when I redefined the polygons. The two polygons that didn't display were bound by three lines. The first polygon was bound by y0, y1, y2 and the 'frame' of the figure. Because I was still scaling the figure, I had put the point that bound it by the 'frame' as $(100, -100)$. When I changed that to $(min_x, min_y)$ it suddenly displayed properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM