Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clipping in Matplotlib. Why doesn't this work?

I'm attempting to clip shapes like circles and ellipses using clipping in Matplotlib, but there must be something I am missing.. Why doesn't this clip the circle in half?:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.transforms import Bbox

clip_box = Bbox(((-2,-2),(2,0)))
circle = Circle((0,0),1,clip_box=clip_box,clip_on=True)

plt.axes().add_artist(circle)
plt.axis('equal')
plt.axis((-2,2,-2,2))
plt.show()
like image 472
Eskil Avatar asked Apr 17 '12 13:04

Eskil


1 Answers

I do not know why your code does not work, however, the following snippet works as you expect.

From my undestanding, clip_on is not related to applying a given clipping to a shape but wether the shape should clip in the display area.

import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Rectangle

rect = Rectangle((-2,-2),4,2, facecolor="none", edgecolor="none")
circle = Circle((0,0),1)

plt.axes().add_artist(rect)
plt.axes().add_artist(circle)

circle.set_clip_path(rect)

plt.axis('equal')
plt.axis((-2,2,-2,2))
plt.show()
like image 151
FabienAndre Avatar answered Sep 18 '22 20:09

FabienAndre