Remove portion of plot that lies outside of circle
<p>I have written a python script that produces vector field plots of the electric and magnetic field vectors for EM waves in various modes of propagation in a cylindrical waveguide. I have used <code>streamplot</code> to produce the vector field plots. This question would be asked much more easily if I were able to post an image of my plot but I am currently unable to since my reputation is currently below 10.</p>

<p>Although the mathematics of the script is formulated in cylindrical coordinates, due to the difficulty of using <code>streamplot</code> on a polar plot, I have plotted the cross sectional (electric) field vectors on a standard square grid using: 
<code>pyplot.streamplot(X, Y, UE, VE, linewidth=lwE, density=6,color='r')</code>. If you're interested, lwE is defined elsewhere in my script and simply scales the line widths according to the magnitude of the vector at that point.</p>

<p>Anyway, my problem is that, naturally, due to being plotted on a square grid, the field vectors extend slightly outside the boundary of the cross section of the cylinder in the corners of the plot. I just want the plot to display the vector fields <em>inside</em> the cylinder, not outside. This problem can be solved by using a polar plot, but I cannot get <code>streamplot</code> to work on a polar plot. In short - matplotlib returns a square plot; I want to 'cut out' the parts of the plot that lie outside of the circular cross section of my cylinder.</p>

<p>I would like to know the best way to 'cut out' these unwanted portions of the plot. My first thought was to plot a 'circ' function (circular aperture type function available in some software packages) that is only nonzero inside of a certain radius; sadly, there does not appear to be such a function available. I have tried overlaying transparent circles onto my plot, and whilst they do clearly highlight the region of interest, the 'unwanted' regions outside the circle are still present; I want these portions completely removed.</p>

<p>I can set the (x,y) limits of the plot using <code>axes.set_xlim([-a,a])</code> and
<code>axes.set_ylim([-a,a])</code> ; what would be useful is a polar version of this (say <code>axes.set_rlim([0,a]</code>). This is obviously trivial if one is able to use a polar plot in the first place, but does such a limit command exist if one is using Cartesian axes?</p>

<p>You can use a clip path. Matplotib's documentation has a nice example: <a href="http://matplotlib.org/examples/images_contours_and_fields/image_demo_clip_path.html" rel="nofollow">http://matplotlib.org/examples/images_contours_and_fields/image_demo_clip_path.html</a></p>

python matplotlib