matplotlib plt.Rectangle的怪异行为 [英] Weird behavior of matplotlib plt.Rectangle

查看:144
本文介绍了matplotlib plt.Rectangle的怪异行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个 matplotlib蛋糕.;)

我有以下代码:它应该打印一个蓝色和红色的矩形,再除以绿色的涂层".

I have the following code: It should print a blue and an red rectangle, divided by a green "coating".

import matplotlib.pyplot as plt

def save_fig(layer):
    # Hide the right and top spines
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    # Sacale axis
    plt.axis('scaled')
    fig.savefig(layer+'.pdf', dpi=fig.dpi)
    fig.savefig(layer+'.jpeg', dpi=fig.dpi)

gap =10
fig, ax = plt.subplots()
rectangle_gap = plt.Rectangle((0-gap, 0), 500+2*gap, 100+gap, color ="green");
plt.gca().add_patch(rectangle_gap);
rectangle = plt.Rectangle((0, 0), 500, 100, color = "red");

plt.gca().add_patch(rectangle)
rectangle = plt.Rectangle((0, 100+gap), 500, 100, color = "blue");
plt.gca().add_patch(rectangle);


save_fig("test")

结果如下:

如您所见,输出正是我所期望的.伟大的 !不过,我玩的参数...

As you can see the output is exactly how I expect it. Great ! However, I played with the parameters...

如果我把顶部的蓝色矩形的宽度设置的很长,它会以某种方式进入绿色分隔涂层...

If I make the width of the top blue rectangle very long, it somehow enters the green dividing coating...

这是更改后的代码(唯一更改的是顶部矩形的宽度,从 500 到 5000):

Here is the changed code (the only thing that changed is the width of the top rectangle, from 500 to 5000):

gap =10
fig, ax = plt.subplots()
rectangle_gap = plt.Rectangle((0-gap, 0), 500+2*gap, 100+gap, color ="green");
plt.gca().add_patch(rectangle_gap);
rectangle = plt.Rectangle((0, 0), 500, 100, color = "red");

plt.gca().add_patch(rectangle)
rectangle = plt.Rectangle((0, 100+gap), 5000, 100, color = "blue");
plt.gca().add_patch(rectangle);

现在输出看起来像这样:

Now the output looks like this:

如果我打开pdf输出并放大,您会明白我的意思:

If I open the pdf output and zoom in, you can see what I mean:

为什么会这样,我该如何解决?

正如@Bazingaa 所建议的,我尝试过:

As suggested by @Bazingaa, I tried:

1000:

1000 -> 缩放:

1000 -> zoom:

2000:

2000->缩放:

3000:

3000 -> 缩放:

3000 -> zoom:

还有……

10000

10000->缩放:

10000 -> zoom:

推荐答案

回答:我在 github 上被告知要执行以下操作:

To answer: I was told on github to do the following:

将所有矩形的linewidth属性设置为0(linewidth = 0).

Set the linewidth property of all your Rectangles to 0 (linewidth=0).

这解决了问题!:)

原因:

该线宽 1 pt,与矩形边缘重叠 0.5 pt.你正在使地块变得如此小以至于可见.

The line is 1 pt wide and 0.5 pt overlaps your rectangle edge. You were making plots so small this was visible.

这篇关于matplotlib plt.Rectangle的怪异行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆