Skip to content

Cleaning up kwargs in ContourSet #2369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
agoodm opened this issue Sep 3, 2013 · 4 comments · Fixed by #26278
Closed

Cleaning up kwargs in ContourSet #2369

agoodm opened this issue Sep 3, 2013 · 4 comments · Fixed by #26278

Comments

@agoodm
Copy link

agoodm commented Sep 3, 2013

This is a continuation of a mailing list thread where we talked about how to clip a plot inside a polygon. It is a very useful application for people who are visualizing data on maps since often times we want to plot everything inside one region (country, state or province).

http://matplotlib.1069221.n5.nabble.com/Clipping-a-plot-inside-a-polygon-td41950.html

Currently for many types of plots this is not that hard to do using the clip_path keyword for most of the plotting functions, since the kwargs are usually used to modify properties of the artists to be generated by the plotting function. For example, suppose that I had a polygon defining the border of a country, poly, and some data to overlay on top.

plt.pcolor(data, clip_path=poly)

Does what I want because the kwargs of pcolor let me modify the underlying PolyCollection instance. However, there are a few plotting functions where I cannot do this, most notably in contour / contourf:

plt.contourf(data, clip_path=poly)

Will work but the clip_path kwarg gets completely ignored. To get the result I want, I need to store the output of contourf and use the set_clip_path method on each collection instance:

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

So I looked at the code in contour.py and realized that no kwargs get passed when instantiating the collections. @pelson mentioned that this might call for an overhaul of how the kwargs get passed into a ContourSet. His suggestion was either adding a set_clip_path method directly to ContourSet, or a more thorough change of how the kwargs are getting passed so they are more consistent with the other plotting functions. Ideally, I would prefer the latter case since then for my usage case I could always get what I want just by passing in the kwarg directly. Additionally it would make the functionality of contour(f) more similar to the other plotting functions, ie some of the kwargs can be passed to the collections. Any thoughts on this?

@ianthomas23
Copy link
Member

@bassdx: The python contour code has been due a refactor for some time; in fact I seem to have half-heartedly promised to do that about a year ago! (see #367). The idea is to separate out the calculation/storage of the polygons from all the graphical stuff like colours, etc. This will allow users to perform contour calculations without any GUI overhead, plus the graphical-aware ContourSet class can inherit from Artist for improved consistency with other plotting operations. So in the long run we will get clipping from the Artist inheritance.

However, I won't be able to do this for a few months. In the meantime, if you want to write a temporary solution then go for the one that is consistent with the other plotting functions as this is how it will end up.

@tacaswell
Copy link
Member

@ianthomas23 Did this get dealt with in your most recent triangulation work?

@ianthomas23
Copy link
Member

@tacaswell: No, the two are unrelated.

@tacaswell tacaswell added this to the v1.5.x milestone Aug 17, 2014
@petehuang
Copy link
Contributor

Doesn't seem like there has been any movement on this since the last update. Seems like two options are listed in the OP:

Recommend looking at the first while the broader contour discussion can continue.

Recommend labeling as Difficulty: Medium.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants