简体   繁体   English

Matplotlib savefig pdf不显示某些多边形

[英]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 . 我正在使用plot.Polygonax.add_patch将多边形用于填充区域。

When I save to .png, everything works fine. 当我保存为.png时,一切正常。 So, I turn to pdf for the final image. 因此,我将pdf转换为最终图像。 I manually set things like fontsize , figsize and dpi , so there should be no problem. 我手动设置诸如fontsizefigsizedpi类的东西,因此应该没有问题。

Sadly, 2 out of the four Polygons in the current picture are not displayed in the pdf. 不幸的是,当前图片中四个Polygons中的两个没有显示在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. 我在“ Polygonszorderalpha ,但无济于事。

My google search did not turn up anything; 我的Google搜索未显示任何内容; it mentioned the dpi being different if you haven't set it manually, but this is no problem. 它提到如果您没有手动设置dpi ,则dpi会有所不同,但这没问题。 Beyond that, I could find no clear reasons. 除此之外,我找不到明确的原因。

My sincere thanks, Daimonie 衷心感谢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 . 定义顶点的点$(x,y)$称为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. 第一个多边形的边界是y0, y1, y2和图形的“框架”。 Because I was still scaling the figure, I had put the point that bound it by the 'frame' as $(100, -100)$. 因为我仍在缩放图形,所以我将受“框架”约束的点定为$(100,-100)$。 When I changed that to $(min_x, min_y)$ it suddenly displayed properly. 当我将其更改为$(min_x,min_y)$时,它突然正确显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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