Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matplotlib plt.Rectangle Incoherent Results #12335

Closed
dorianherle opened this issue Sep 29, 2018 · 6 comments
Closed

Matplotlib plt.Rectangle Incoherent Results #12335

dorianherle opened this issue Sep 29, 2018 · 6 comments

Comments

@dorianherle
Copy link

dorianherle commented Sep 29, 2018

Bug report

Bug summary

Matplotlib plt.Rectangle does not produce coherent pdf results: Overlap where there should be none !
Maybe other shapes have the same issue.

Code for reproduction

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

Actual outcome

image

This seems okay, but when I increase the length of the top blue rectangle to 2000:

image

Zoom in:
image

The overlap gets worse with the increasing length along x-axis.
See: https://stackoverflow.com/questions/52518140/weird-behavior-of-matplotlib-plt-rectangle?noredirect=1#comment91989045_52518140

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 2.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.5
  • Jupyter version (if applicable): 1.0.0
conda default
@jklymak
Copy link
Member

jklymak commented Sep 29, 2018

Set the linewidth property of all your Rectangles to 0 (linewidth=0). I'm going to close, but feel free to comment if you think that doesn't fix your problem.

@jklymak jklymak closed this as completed Sep 29, 2018
@dorianherle
Copy link
Author

Yes ! This fixes the problem ! Thanks a lot !!
However, why would it not work "out of the box" ? I don't understand if I made something wrong, or if it is really a bug in matplotlib.

@jklymak
Copy link
Member

jklymak commented Sep 29, 2018

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

@dorianherle
Copy link
Author

I understand ! Thank you so much !! 👍

@tacaswell
Copy link
Member

I don't think you did anything wrong nor do I think this is a bug, just that the default behavior is not what you needed.

By default patches (of which Rectangle is a subclass) are drawn with a 1 pt think border which defaults to the same color as the patch with the edge of the patch going through the center of the border line. You could argue that the default line width of the border should be 0, but then if users want to change the color of the border would be surprised when it had no effect (with out also changing the line width). Making the default linewidth depend on if the border is the same color as the face would also be very confusing (as the on-screen shape would change based on the colors!).

Another tricky thing about this is that the position and shape of the rectangle is specified in data-units, but the thickness of the border is in absolute units (pts, which are 1/72 of an inch). If you zoomed in by changing the axis limits (rather than by zooming in on the pdf) you would have gotten different results.

@dorianherle
Copy link
Author

dorianherle commented Sep 29, 2018

@tacaswell Thanks a lot for your comment and your helpful explenation !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants