pysal / splot

Lightweight plotting for geospatial analysis in PySAL
BSD 3-Clause "New" or "Revised" License
97 stars 27 forks source link

vba_chroropleth does not allow `None` #50

Open slumnitz opened 5 years ago

slumnitz commented 5 years ago

One more from playing with this for a workshop this past week. It seems the function throws an error if alpha_mapclassify and rgb_mapclassify don't have a value (the default None don't seem to work). I picked this up from the example notebook, where everything is passed and it works great, but from the docs, it seems to imply that it'd be just fine by passing the variables. Is that the case? If so, happy to open up a separate issue.

referred to in #45

renanxcortes commented 5 years ago

Can you share here some reproducible example of this bug? I'm not really sure that I follow. I tried to run this function removing both arguments and the results were generated as follows:

import splot
import libpysal as lp
from libpysal import examples
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import numpy as np

% matplotlib inline

link_to_data = examples.get_path('columbus.shp')
gdf = gpd.read_file(link_to_data)

x = gdf['HOVAL'].values
y = gdf['CRIME'].values

from splot.mapping import vba_choropleth

# Create new figure
fig, axs = plt.subplots(1,2, figsize=(20,10))

# use gdf.plot() to create regular choropleth
gdf.plot(column='HOVAL', scheme='quantiles', cmap='RdBu', ax=axs[0])

# use vba_choropleth to create Value-by-Alpha Choropleth
vba_choropleth(x, 
               y, 
               gdf, 
               #rgb_mapclassify = dict(classifier = 'quantiles'),
               #alpha_mapclassify = dict(classifier = 'quantiles'),
               cmap = 'RdBu', 
               ax = axs[1])

# set figure style
axs[0].set_title('normal Choropleth')
axs[0].set_axis_off()
axs[1].set_title('Value-by-Alpha Choropleth')

# plot
plt.show()

image

I also reproduced the first example of the doc:

from libpysal import examples
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from splot.mapping import vba_choropleth
link_to_data = examples.get_path('columbus.shp')
gdf = gpd.read_file(link_to_data)
fig, _ = vba_choropleth('HOVAL', 'CRIME', gdf)
plt.show()

And the map was generated:

image

renanxcortes commented 5 years ago

Even changing to

rgb_mapclassify = None,
alpha_mapclassify = None,

Both arguments of the function the same graphs are generated.

slumnitz commented 5 years ago

I think @darribas meant if you leave out the y variable or set y=None? So that instead of a vba_choropleth the user sees a normal choropleth?

renanxcortes commented 5 years ago

I'm not sure if I follow still... cause he mentions about being None as the default and currently the alpha_mapclassify and rgb_mapclassify as default.

image

I think a reproducible example of this bug would be cool to debug it :)

darribas commented 5 years ago

Thanks for this, my bad for very poorly communicating my point. Yes, it's what @slumnitz suggests. With the following pre:

from libpysal import examples
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from splot.mapping import vba_choropleth
link_to_data = examples.get_path('columbus.shp')
gdf = gpd.read_file(link_to_data)

I'm proposing we allow the user to pass:

fig, _ = vba_choropleth('HOVAL', gdf)
plt.show()

And obtain the same we'd get with:

gdf.plot(column='HOVAL', scheme='quantiles')

I think that'd make vba_choropleth a lot more useful and get people to use it in more contexts (for one, geopandas doesn't support all of our classifiers while we could here), thus hopefully "nudging" them to include more VBA in their work :-)

My only concern on this, on a second reflection, is whether we should do that in vba_choropleth or have a more general, swiss-army knife, all-encompassing choropleth (or something like that) that allows for all the functionality and essentially just dispatches to other functions (such as vba_choropleth for VBA mapping).

What do you think @slumnitz and others?

renanxcortes commented 5 years ago

Got it, @darribas ! Thanks for the reproducible example! Well, I think that what would make more sense, in this case, is your "swiss-army knife" suggestion, since I believe that a 'vba' mapping function should expect to receive a value for the alpha itself... that's just my humble opinion.

In addition, we were thinking about building some kind of 'general' choropleth explorer interactively with ipywidgets taking into advantages the classification of mapclassify. Something that would look like this:

image

In this case, I think this issue would be addressed in a certain way if we manage to add some feature like this, what do you think and others?

darribas commented 5 years ago

Sounds great! I'd be game for leaving vba_choropleth as a VBA tool only and then creating something that wraps around all choropleth functionality somewhere else, but preferably in splot? What do others think?

Just out of curiosity, how are you building the explorer in the image? Having the swiss-army knife would come in handy for that too, no?

renanxcortes commented 5 years ago

Yes, I agree with your first point! I think would make more sense to build this under splot! Regarding your question, this is a screenshot that Serge shared with us here in the CGS of a project that he developed some time ago... We didn't find the source code, yet, but wanna to build this (possibly) from scratch. But basically, it is built using ipywidgets, colorbrewer, and mapclassify and definitely the swiss-army knife would be helpful for that! hehe We're thinking about to make sprints once a while for specific projects. This one is on the list, for sure!

sjsrey commented 5 years ago

In terms of the explorer graphic renan shared, i think that should stay in mapclassify as the intention originally, was to provide a simple interface to explore the classifiers visually and mimic colorbrewer. We repurposed it when building the old pysal.contrib.viz and I lost the original interface code since then.

Of course that doesn't preculde doing a more comprehensive thing for splot. I just want to make sure the lightweight thing for mapclassify doesn't get lost in the shuffle.

darribas commented 5 years ago

Yes, I agree the interactive from the screenshot fits in mapclassify better. What I was suggesting was a method that wraps around all the choropleth functionality we have between splot, geopandas and mapclassify. Basically one function that lets you create choropleths with all the classifiers in mapclassify, with the option for VBA or not, with a unified API. My first thought was that'd fit better in splot but I'm not particularly married to the idea and very happy to discuss further.

slumnitz commented 5 years ago

I like the idea of also including a choropleth() mapping functionality in splot. I would however leave the via_choropleth as is, I think users would maybe not get to know about the vba option if it is hidden as a parameter int he function call. We could then use splot functionality to call map classify.Quantiles.plot() similarly how we do that in e.g. giddy or esda?