earthpy icon indicating copy to clipboard operation
earthpy copied to clipboard

Vector colorbar compatibility

Open nkorinek opened this issue 4 years ago • 2 comments

closes #557

Updated the ep.colorbar function to work with GeoDataFrame plots. This was a bit tricky, and I'm not sure this is the exact way we want to implement this, so feedback would be greatly appreciated @lwasser .

There are two ways I've figured out to make this work, but neither of them is ideal, so I thought I'd ask for input. I went with what I thought was the simpler way, but wasn't sure if there was a better way.

The way GeoPandas does colorbars is just by setting legend=True in the plot() function. To adjust the colorbar, you can give it a cax argument, which will implement the scale and pad arguments we have in our ep.colorbar() function.

In the ep.colorbar() function, we create a cax object and assign it to the figure. The problem is, this assumes that the object that was passed into the ep.colorbar() is what matplotlib calls a ScalarMappable, so like an array or a contour map is a ScalarMappable. A GeoDataFrame is not inheritely a ScalarMappable like numpy arrays are, so we can do one of two things. We can either make the function have an argument to return the cax before it's applied to the figure and then that can be used in gpd.plot(), which is what I did.

The other way would be to create a ScalarMappable in the function for the plot. This needs a cmap and a norm object for the map. That seemed like a lot of information to get/create in the function, so I avoided this solution.

The return cax option does change how the function is implemented a little. See below for how it changes implementation.

f, ax1 = plt.subplots(figsize=(10,6))

cax_plot = ep.colorbar(ax1, return_cax=True)

countries.plot(column='POP_EST', ax=ax1, cmap="viridis", legend=True, cax=cax_plot)

plt.show()

nkorinek avatar Jun 19 '20 22:06 nkorinek

@lwasser this pr is failing CI because I'm using gdf.plot() to test the new functionality, since that's what we made the functionality for. However, descartes is not installed on the earthpy environment, so I can't use that function in the tests. Thoughts?

nkorinek avatar Jun 19 '20 22:06 nkorinek

hey @ravi5175 just checking in. did you review this PR or are you just practicing your skills? generally PRs are reviewed by maintainers on OS projects.

lwasser avatar Jul 23 '20 16:07 lwasser