Skip to content
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

Added the styler keyword to allow on-demand / callback to determine styling of geometries in the matplotlib interface. #1019

Merged
merged 2 commits into from
Nov 15, 2018

Conversation

pelson
Copy link
Member

@pelson pelson commented Feb 7, 2018

Replaces #910. Closes #580.

lib/cartopy/mpl/feature_artist.py Outdated Show resolved Hide resolved
lib/cartopy/mpl/geoaxes.py Outdated Show resolved Hide resolved
@has2k1
Copy link

has2k1 commented Feb 7, 2018

callbacks make for an API difference compared to MPL, is there any specific advantage for them.

For my use case, callbacks would not work because the decisions on the parameter values happen somewhere in an elaborate pipeline before the calls to ax.add_artist. So ax.add_geometries needs to handle parameters just like Matplotlib collection artists.

@pelson
Copy link
Member Author

pelson commented Feb 8, 2018

callbacks make for an API difference compared to MPL, is there any specific advantage for them.

It means we can style a feature that may not yet have all of the geometries to hand. For example, some features can return different geometries based on the zoom level.

For my use case, callbacks would not work because the decisions on the parameter values happen somewhere in an elaborate pipeline before the calls to ax.add_artist

That form would still work (untested):

geoms = [geom1, geom2, geom3, geom4, geom5, geom6, geom7]
facecolors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']  

def color_lookup(geom):
    index = geoms.index(geom)
    return {'facecolor': facecolors[index]}

ax.add_geometries(geoms, crs, styler=color_lookup)

If you really dislike that solution, I'd be supportive of creating a GeometryCollection object that follows the same interface as the PathCollection one. Namely:

ax.add_collection(GeometryCollection([geoms], facecolors=facecolors, transform=crs))

But I don't want to bleed in index information into Features because they are inherently limited to a fixed number of geometries.

@has2k1
Copy link

has2k1 commented Feb 9, 2018

That solution would work, but indeed it is not friendly. If I had not been aware of the underlying issue, it would be mysterious. It requires a little too much context to fully understand. Also, it is probably of little consequence (as geometry lengths tend to be small) but that lookup is O(n^2), though it could be fixed with a 2nd lookup dictionary.

On the other hand, inspired by that code #910 could altered to take the index stuff out of the features, so that the fix is self contained, but having the styler and other parameters would make for a complicated API.

As such, a styler for ax.add_features and GeometryCollection for ax.add_collection is very good idea.

lib/cartopy/tests/mpl/test_axes.py Outdated Show resolved Hide resolved
lib/cartopy/tests/mpl/test_axes.py Outdated Show resolved Hide resolved
lib/cartopy/tests/mpl/test_axes.py Outdated Show resolved Hide resolved
@pelson pelson changed the title WIP: Added the styler keyword to allow on-demand / callback to determine styling of geometries in the matplotlib interface. Added the styler keyword to allow on-demand / callback to determine styling of geometries in the matplotlib interface. Feb 9, 2018
@pelson pelson force-pushed the geometries_styling branch 3 times, most recently from 1c508d3 to 0a094a3 Compare February 9, 2018 08:15
Copy link
Contributor

@dopplershift dopplershift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me. @pelson you still feel good about this one?

@pelson
Copy link
Member Author

pelson commented Nov 15, 2018

Looks fine to me. @pelson you still feel good about this one?

Sure do. If you are at hand, please feel free to merge at will. I'm going to build something on top of this change now, and when I'm done I'll merge (if you haven't beaten me to it) before opening a new PR.

@dopplershift dopplershift merged commit 0ded576 into SciTools:master Nov 15, 2018
@pelson
Copy link
Member Author

pelson commented Nov 15, 2018

Actually, given how old the branch is, building something on top of it is a pretty bad idea, so I'm going to merge this and move on.

@pelson
Copy link
Member Author

pelson commented Nov 15, 2018

Actually, given how old the branch is, building something on top of it is a pretty bad idea, so I'm going to merge this and move on.

🤣 - man, you're fast!

@pelson pelson deleted the geometries_styling branch November 15, 2018 04:20
@dopplershift
Copy link
Contributor

Had just opened the issue when you commented. 😁

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

Successfully merging this pull request may close these issues.

4 participants