Skip to content

Value By Alpha specification #24

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
ljwolf opened this issue Jul 14, 2018 · 5 comments
Closed

Value By Alpha specification #24

ljwolf opened this issue Jul 14, 2018 · 5 comments

Comments

@ljwolf
Copy link
Member

ljwolf commented Jul 14, 2018

@darribas and I talked, and we think it makes the most sense for value-by-alpha maps to be implemented as follows:

def value_by_alpha('attribute', 'alpha_attribute', ..., data=geodataframe):
    rgb = make_RGBA_array_from_attribute(data.attribute)
    alpha_channel = data['alpha_attribute'] / data['alpha_attribute'].max()
    rgba = np.vstack((rgb, np.asarray(alpha_channel).reshape(-1,1)))
    return geodataframe.plot('attribute', color=rgba)
@ljwolf
Copy link
Member Author

ljwolf commented Jul 14, 2018

also, we would need to check if the cmap is divergent. If it is, we may offer a diverging_alpha=True flag that lets the inside of the colormap become transparent, rather than the extreme in the specification shown above.

Essentially, we need to offer all options for the rgb matrix for the alpha channel as well.

@darribas
Copy link
Member

Here's an example on how to pass RGBA tuples to geopandas for fully customisation (provided by @jorisvandenbossche on Gitter):

In [35]: import geopandas

In [36]: from shapely.geometry import Polygon

In [37]: import random

In [26]: import matplotlib

In [30]: cols = matplotlib.colors.to_rgba_array(['r', 'g', 'k', 'b'])

In [31]: cols
Out[31]: 
array([[ 1. ,  0. ,  0. ,  1. ],
       [ 0. ,  0.5,  0. ,  1. ],
       [ 0. ,  0. ,  0. ,  1. ],
       [ 0. ,  0. ,  1. ,  1. ]])

In [32]: cols[:, -1] = [0.5, 0.7, 0.9, 0.2]

In [33]: s = geopandas.GeoSeries([Polygon([(random.random(), random.random()) for i in range(3)]) for _ in range(4)])

In [34]: s.plot(color=cols)
Out[34]: <matplotlib.axes._subplots.AxesSubplot at 0x7fc0d783cd68>

@slumnitz
Copy link
Member

pr #28 is a start dealing with value_by_alpha functionality. Matplotlib actually offers a great way to work with rgba cmaps:

def value_by_alpha_cmap(x, y, cmap='GnBu'):
    """
    Calculates Value by Alpha rgba values
    
    Parameters
    ----------
    x : array
        Variable determined by color
    y : array
        Variable determining alpha value
    cmap : str or list of str
        Matplotlib Colormap or list of colors used
        to create vba_layer
    
    Returns
    -------
    rgba : ndarray (n,4)
        RGBA colormap, where the alpha channel represents one
        attribute (x) and the rgb color the other attribute (y)
    """
    # option for cmap or colorlist input
    if isinstance(cmap, str):
        cmap = cm.get_cmap(cmap)
    elif isinstance(cmap, collections.Sequence):
        cmap = colors.LinearSegmentedColormap.from_list('newmap', cmap)
    rgba = cmap(x)
    rgba[:, 3] = y/y.max()
    return rgba

@slumnitz
Copy link
Member

slumnitz commented Jul 25, 2018

@ljwolf @darribas @sjsrey do you think it makes sense to allow mapclassifyed data to also be included into value by alpha data? E.g. x and y are classified by 'Quantiles` and then displayed with y in the alpha layer? I am unsure at the moment how to best connect all different options for choropleth visualisations together and was wondering if you had ideas? Maybe a topic to discuss on Friday.

as to say do we try and squeeze everything in one function eg.
choropleth(gdf, x, vba=y, divergent_vba=True, mapclassify='Quantiles', ..., ax=None)

or do we create a class and build something like:

choro = choropleth(gdf, x)
choro.Quantiles(x, y)
choro.vba(y, divergent)

@slumnitz
Copy link
Member

closing this issue, vba functionality will be added with #28 and #30

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

No branches or pull requests

3 participants