Plot only inside a disc

Thanks, your example works but what I must do so to plot for example

y=cos x

? I'm a very beginner.

line, = ax.plot(x, np.cos(x))
patch = patches.Circle((300,300), radius=100)
line.set_clip_path(patch)

Everything in the matplotlib figure is an "Artist" (lines, images,
text, rectangles) and you can set the clippath of any artist. See

http://matplotlib.sourceforge.net/users/artists.html
http://matplotlib.sourceforge.net/api/artist_api.html

JDH

Thanks a lot ! I'll look more precisely the concept of artist later.

Best regards.
Christophe.

[Christophe] Thanks, your example works but what I must do so to plot for example

y=cos x

? I'm a very beginner.

line, = ax.plot(x, np.cos(x))
patch = patches.Circle((300,300), radius=100)
line.set_clip_path(patch)

Everything in the matplotlib figure is an "Artist" (lines, images,
text, rectangles) and you can set the clippath of any artist. See

http://matplotlib.sourceforge.net/users/artists.html
http://matplotlib.sourceforge.net/api/artist_api.html

JDH

This is a very timely question for me. I'm needing to do something very similar, but I need to overlay a semi-transparent rectangle with a hole cut out of it. So, I'm making a rectangular patch, making a circular patch, setting the circular patch as the clip region for the rectangular patch, and then adding the clipped patch to the plot. However, I seem to be having trouble with the coordinate system, as there is no clipping on the rectangle.

My test code looks like this:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as path
import matplotlib.patches as patches

fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(-50,50,0.1)
line, = ax.plot(x, np.cos(x)*50)
r=patches.Rectangle((-10,-10), 20, 20, fc=(0.5,0.5,0.5,0.9))
r.set_zorder(100)

# shouldn't one of these work?
# Plot coordinate system
cutout = patches.Circle((0,0), radius=10)
# Window coordinate system
#~ cutout = patches.Circle((300,300), radius=50)

r.set_clip_path(cutout)
ax.add_patch(r)
plt.show()

The problem is that the "add_patch" command is setting the clippath to
the axes bounding box. I just committed a patch on the branch and
trunk which only sets the clippath to the default if it is not already
set. If you don't have access to svn, just make the call to
r.set_clip_path *after* you call ax.add_patch.

JDH

···

On Fri, Jan 30, 2009 at 8:54 AM, <Andy.Henshaw@...905...> wrote:

This is a very timely question for me. I'm needing to do something very similar, but I need to overlay a semi-transparent rectangle with a hole cut out of it. So, I'm making a rectangular patch, making a circular patch, setting the circular patch as the clip region for the rectangular patch, and then adding the clipped patch to the plot. However, I seem to be having trouble with the coordinate system, as there is no clipping on the rectangle.

My test code looks like this:

r.set_clip_path(cutout)
ax.add_patch(r)
plt.show()

Hmm ... this doesn't quite give me what I'm looking for. When I do that, I get a semitransparent circle that is clipped to a rectangle. What I need is a semi-transparent rectangle (with a hole cut out of the middle) that overlays the plot. The attached graphic demonstrates the concept.

Thanks for the response.

temp.png

···

-----Original Message-----
From: John Hunter [mailto:jdh2358@…287…]

On Fri, Jan 30, 2009 at 8:54 AM, <Andy.Henshaw@...905...> wrote:

> This is a very timely question for me. I'm needing to do something
very similar, but I need to overlay a semi-transparent rectangle with a
hole cut out of it. So, I'm making a rectangular patch, making a
circular patch, setting the circular patch as the clip region for the
rectangular patch, and then adding the clipped patch to the plot.
However, I seem to be having trouble with the coordinate system, as
there is no clipping on the rectangle.
>
> My test code looks like this:

> r.set_clip_path(cutout)
> ax.add_patch(r)
> plt.show()

The problem is that the "add_patch" command is setting the clippath to
the axes bounding box. I just committed a patch on the branch and
trunk which only sets the clippath to the default if it is not already
set. If you don't have access to svn, just make the call to
r.set_clip_path *after* you call ax.add_patch.

JDH

It sounds like what you want is a complex path and may not need to
muck with clipping at all. Take a look at

http://matplotlib.sourceforge.net/examples/api/donut_demo.html

···

On Fri, Jan 30, 2009 at 10:27 AM, <Andy.Henshaw@...905...> wrote:

Hmm ... this doesn't quite give me what I'm looking for. When I do that, I get a semitransparent circle that is clipped to a rectangle. What I need is a semi-transparent rectangle (with a hole cut out of the middle) that overlays the plot. The attached graphic demonstrates the concept.

Okay, although I wish the clipping was working as I expected, as it looks like it would have been **quite a bit** cleaner. It looks like your svn patch would enable me to do what I want with patches, wouldn't you agree?

···

-----Original Message-----
From: John Hunter [mailto:jdh2358@…287…]
Sent: Friday, January 30, 2009 11:29 AM
To: Henshaw, Andy
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Plot only inside a disc

On Fri, Jan 30, 2009 at 10:27 AM, <Andy.Henshaw@...905...> > wrote:

> Hmm ... this doesn't quite give me what I'm looking for. When I do
that, I get a semitransparent circle that is clipped to a rectangle.
What I need is a semi-transparent rectangle (with a hole cut out of the
middle) that overlays the plot. The attached graphic demonstrates the
concept.

It sounds like what you want is a complex path and may not need to
muck with clipping at all. Take a look at

http://matplotlib.sourceforge.net/examples/api/donut_demo.html

Thanks, that is working quite well, now.

···

-----Original Message-----
From: Andy.Henshaw@…905…
[mailto:Andy.Henshaw@…905…]
Sent: Friday, January 30, 2009 11:46 AM
To: jdh2358@…287…
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Plot only inside a disc

> -----Original Message-----
> From: John Hunter [mailto:jdh2358@…287…]
> Sent: Friday, January 30, 2009 11:29 AM
> To: Henshaw, Andy
> Cc: matplotlib-users@lists.sourceforge.net
> Subject: Re: [Matplotlib-users] Plot only inside a disc
>
> On Fri, Jan 30, 2009 at 10:27 AM, <Andy.Henshaw@…905…> > > wrote:
>
> > Hmm … this doesn't quite give me what I'm looking for. When I do
> that, I get a semitransparent circle that is clipped to a rectangle.
> What I need is a semi-transparent rectangle (with a hole cut out of
the
> middle) that overlays the plot. The attached graphic demonstrates
the
> concept.
>
> It sounds like what you want is a complex path and may not need to
> muck with clipping at all. Take a look at
>
> http://matplotlib.sourceforge.net/examples/api/donut_demo.html