5

I was trying to make a "matplotlib cake". ;)

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

which results in the following output: enter image description here

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...

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:

enter image description here

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

enter image description here

Why is that, and how can I fix it ?

EDIT: As suggested by @Bazingaa, I tried:

1000:

enter image description here

1000 -> zoom:

enter image description here

2000:

enter image description here

2000 -> zoom:

enter image description here

3000:

enter image description here

3000 -> zoom:

enter image description here

and...

10000

enter image description here

10000 -> zoom:

enter image description here

  • I'm gonna make a guess that this has to do with the dimensions of the graph. Maybe try increasing the dimensions of the graph. – scales Sep 26 '18 at 12:54
  • @scales I tryied your idea, by adding: fig, ax = plt.subplots(figsize=(5,2), dpi=200, facecolor='w', edgecolor='k') but it did not change the result... –  Sep 26 '18 at 13:06
  • Can you try using 1000, 2000, 3000,4000 as well just to see if there is some crossover point? – Sheldore Sep 26 '18 at 13:23
  • 3
    I think there are some problems with constant dpi in pdf export which might lead to positions being wrong by as much as 1/72 inch. Feel free to open an issue at [the issue tracker](https://github.com/matplotlib/matplotlib/issues). – ImportanceOfBeingErnest Sep 26 '18 at 13:54
  • @Bazingaa Please have a look at my edit. It appears that the error becomes worse with increasing length. –  Sep 26 '18 at 13:59
  • Glad to see the output. Now you have a clear correlation to post in your issue that the overlap gets worse with the increasing length along x-axis. – Sheldore Sep 26 '18 at 14:38
  • @ImportanceOfBeingErnest Okay, I will report it. Do you see any way to fix this for now, any quick-fix or work-around ? –  Sep 26 '18 at 15:32
  • No, sorry. This really goes deep into the heart of rendering the pdf. (svg shows the same issue btw.) – ImportanceOfBeingErnest Sep 26 '18 at 19:18

1 Answers1

0

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

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

This fixed the problem! :)

The reason:

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