Great question. The contour set itself does not have a set_clip_path method
but you can iterate over each of the contour collections and set their
respective clip paths, i.e.:

cs = plt.contourf(data)
for collection in cs.collections:
    collection.set_clip_path(poly)

Of course, you can use this approach in either Basemap or cartopy, but I've
put together an example of doing it in cartopy to demonstrate the neat
Shapely integration: http://nbviewer.ipython.org/6410510

HTH,

Phil


On 2 September 2013 05:40, Alex Goodman <alex.good...@colostate.edu> wrote:

> Hi all,
>
> I want to be able to plot data on maps (using basemap or cartopy) inside
> specific regions, eg a single state, province or country. A similar
> question was asked a long time ago on the mailing list and the suggested
> solution back then was to read the bounding polygon from a shapefile and
> then check if each individual point was inside that polygon. Currently I
> have no problem doing this if I use matplotlib.path.Path.contains_points()
> to mask the original data array, but the disadvantage to this solution is
> that it is very slow. Another solution that I have discovered recently is
> to use the set_clip_path() method for artists. In addition to being much
> faster, this also makes the areas near the polygon boundary look much
> smoother since the actual items being clipped are individual pixels and not
> data points.
>
> Here is an example script that plots an image via imshow, but the only
> part of the image that gets shown is inside the hexagon.
>
> import numpy as np
> import matplotlib.pyplot as plt
> from matplotlib.patches import RegularPolygon
>
> data = np.arange(100).reshape(10, 10)
> fig = plt.figure()
> ax = fig.add_subplot(111)
> im = ax.imshow(data)
> poly = RegularPolygon([ 0.5,  0.5], 6, 0.4, fc='none',
>   ec='k', transform=ax.transAxes)
> im.set_clip_path(poly)
> ax.add_patch(poly)
> ax.axis('off')
> plt.show()
>
> While this does seem like an ideal solution, it doesn't work for every
> type of plot. The most notable example is contourf(). It returns a
> QuadContourSet instance which does not inherit from Artist, so it does not
> contain the set_clip_path() method. My main question is whether there is a
> mechanism in matplotlib that can convert something like a QuadContourSet
> into an image so I can make use of this solution for contourf() as well. Or
> better yet, is there perhaps another artist within the axes that I can use
> the set_clip_path() method for and still get what I want?
>
> Thanks,
> Alex
> --
> Alex Goodman
> Graduate Research Assistant
> Department of Atmospheric Science
> Colorado State University
>
>
> ------------------------------------------------------------------------------
> Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
> Discover the easy way to master current and previous Microsoft technologies
> and advance your career. Get an incredible 1,500+ hours of step-by-step
> tutorial videos with LearnDevNow. Subscribe today and save!
> http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>
------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to