Re: [Matplotlib-users] Plot only inside a disc

 Re: [Matplotlib-users] Plot only inside a disc From: John Hunter - 2009-01-29 19:50:53 ```On Thu, Jan 29, 2009 at 1:40 PM, wrote: > 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 ```

 Re: [Matplotlib-users] Plot only inside a disc From: - 2009-01-29 19:40:51 ``` Flashmail

Thanks, your example works but what I must do so to plot for example y=cos x ? I'm a very beginner.

Christophe.

----Message d'origine----
>Date: Thu, 29 Jan 2009 09:34:11 -0600
>Sujet: Re: [Matplotlib-users] Plot only inside a disc
>De: John Hunter
>A: projetmbc@...
>Copie à: matplotlib-users@...
>
>On Thu, Jan 29, 2009 at 9:19 AM, wrote:
>> Hello,
>> I would like to make a kind of magnifying glass. So I need to have a piece of
>> a graph and I would like it to have the form of a disc rather than of a box.
>> So is-it possible to only draw in a disc (I'm searching for a fast way to do
>> that) ?
>
>You can turn off the rendering of the normal axes and axis, and clip
>your data to an arbitrary patch or path; eg
>
>
> """
> Clipping to arbitrary patches and paths
> """
> 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, frameon=False, xticks=[], yticks=[])
>
> im = ax.imshow(np.random.rand(10,10))
>
> im.set_clip_path(patch)
>
> plt.show()
>

```
 Re: [Matplotlib-users] Plot only inside a disc From: - 2009-01-29 20:05:54 ```>> 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. ```
 Re: [Matplotlib-users] Plot only inside a disc From: - 2009-01-30 14:54:27 ```>>[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() ```
 Re: [Matplotlib-users] Plot only inside a disc From: John Hunter - 2009-01-30 16:12:38 ```On Fri, Jan 30, 2009 at 8:54 AM, 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 ```
 Re: [Matplotlib-users] Plot only inside a disc From: - 2009-01-30 16:27:17 Attachments: temp.png ``` > -----Original Message----- > From: John Hunter [mailto:jdh2358@...] > > On Fri, Jan 30, 2009 at 8:54 AM, 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 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. ```
 Re: [Matplotlib-users] Plot only inside a disc From: John Hunter - 2009-01-30 16:29:18 ```On Fri, Jan 30, 2009 at 10:27 AM, 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 ```
 Re: [Matplotlib-users] Plot only inside a disc From: - 2009-01-30 16:46:10 ``` > -----Original Message----- > From: John Hunter [mailto:jdh2358@...] > Sent: Friday, January 30, 2009 11:29 AM > To: Henshaw, Andy > Cc: matplotlib-users@... > Subject: Re: [Matplotlib-users] Plot only inside a disc > > On Fri, Jan 30, 2009 at 10:27 AM, > 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 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? ```
 Re: [Matplotlib-users] Plot only inside a disc From: - 2009-01-30 18:14:54 ``` > -----Original Message----- > From: Andy.Henshaw@... > [mailto:Andy.Henshaw@...] > Sent: Friday, January 30, 2009 11:46 AM > To: jdh2358@... > Cc: matplotlib-users@... > Subject: Re: [Matplotlib-users] Plot only inside a disc > > > > > -----Original Message----- > > From: John Hunter [mailto:jdh2358@...] > > Sent: Friday, January 30, 2009 11:29 AM > > To: Henshaw, Andy > > Cc: matplotlib-users@... > > Subject: Re: [Matplotlib-users] Plot only inside a disc > > > > On Fri, Jan 30, 2009 at 10:27 AM, > > 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. ```
 Re: [Matplotlib-users] Plot only inside a disc From: John Hunter - 2009-01-29 19:50:53 ```On Thu, Jan 29, 2009 at 1:40 PM, wrote: > 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 ```