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

ENH: expose max_labels in ColorMap #90

Merged
merged 8 commits into from
Jun 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions branca/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,27 @@ class ColorMap(MacroElement):
The right bound of the color scale.
caption: str
A caption to draw with the colormap.
max_labels : int
martinfleis marked this conversation as resolved.
Show resolved Hide resolved
Maximum number of legend values
martinfleis marked this conversation as resolved.
Show resolved Hide resolved
"""
_template = ENV.get_template('color_scale.js')

def __init__(self, vmin=0., vmax=1., caption=''):
def __init__(self, vmin=0., vmax=1., caption='', max_labels=10):
super(ColorMap, self).__init__()
self._name = 'ColorMap'

self.vmin = vmin
self.vmax = vmax
self.caption = caption
self.index = [vmin, vmax]
self.max_labels = max_labels

def render(self, **kwargs):
"""Renders the HTML representation of the element."""
self.color_domain = [self.vmin + (self.vmax-self.vmin) * k/499. for
k in range(500)]
self.color_range = [self.__call__(x) for x in self.color_domain]
self.tick_labels = legend_scaler(self.index)
self.tick_labels = legend_scaler(self.index, self.max_labels)

super(ColorMap, self).render(**kwargs)

Expand Down Expand Up @@ -180,11 +183,13 @@ class LinearColormap(ColorMap):
Values lower than `vmin` will be bound directly to `colors[0]`.
vmax : float, default 1.
The maximal value for the colormap.
Values higher than `vmax` will be bound directly to `colors[-1]`."""
Values higher than `vmax` will be bound directly to `colors[-1]`.
max_labels : int
Maximum number of legend values"""

def __init__(self, colors, index=None, vmin=0., vmax=1., caption=''):
def __init__(self, colors, index=None, vmin=0., vmax=1., caption='', max_labels=10):
super(LinearColormap, self).__init__(vmin=vmin, vmax=vmax,
caption=caption)
caption=caption, max_labels=max_labels)

n = len(colors)
if n < 2:
Expand Down Expand Up @@ -216,7 +221,7 @@ def rgba_floats_tuple(self, x):
in range(4))

def to_step(self, n=None, index=None, data=None, method=None,
quantiles=None, round_method=None):
quantiles=None, round_method=None, max_labels=10):
"""Splits the LinearColormap into a StepColormap.

Parameters
Expand All @@ -243,6 +248,8 @@ def to_step(self, n=None, index=None, data=None, method=None,
* If 'log10', all values will be rounded to the nearest
order-of-magnitude integer. For example, 2100 is rounded to
2000, 2790 to 3000.
max_labels : int
Maximum number of legend values

Returns
-------
Expand Down Expand Up @@ -322,9 +329,10 @@ def to_step(self, n=None, index=None, data=None, method=None,
index[i+1] * i/(n-1.)) for
i in range(n)]

return StepColormap(colors, index=index, vmin=index[0], vmax=index[-1])
return StepColormap(colors, index=index, vmin=index[0], vmax=index[-1],
max_labels=max_labels)

def scale(self, vmin=0., vmax=1.):
def scale(self, vmin=0., vmax=1., max_labels=10):
"""Transforms the colorscale so that the minimal and maximal values
fit the given parameters.
"""
Expand All @@ -334,6 +342,7 @@ def scale(self, vmin=0., vmax=1.):
vmin=vmin,
vmax=vmax,
caption=self.caption,
max_labels=max_labels
)


Expand Down Expand Up @@ -362,11 +371,13 @@ class StepColormap(ColorMap):
vmax : float, default 1.
The maximal value for the colormap.
Values higher than `vmax` will be bound directly to `colors[-1]`.
max_labels : int
Maximum number of legend values

"""
def __init__(self, colors, index=None, vmin=0., vmax=1., caption=''):
def __init__(self, colors, index=None, vmin=0., vmax=1., caption='', max_labels=10):
super(StepColormap, self).__init__(vmin=vmin, vmax=vmax,
caption=caption)
caption=caption, max_labels=max_labels)

n = len(colors)
if n < 1:
Expand All @@ -391,7 +402,7 @@ def rgba_floats_tuple(self, x):
i = len([u for u in self.index if u < x]) # 0 < i < n.
return tuple(self.colors[i-1])

def to_linear(self, index=None):
def to_linear(self, index=None, max_labels=10):
"""
Transforms the StepColormap into a LinearColormap.

Expand All @@ -401,6 +412,8 @@ def to_linear(self, index=None):
The values corresponding to each color in the output colormap.
It has to be sorted.
If None, a regular grid between `vmin` and `vmax` is created.
max_labels : int
Maximum number of legend values

"""
if index is None:
Expand All @@ -410,9 +423,9 @@ def to_linear(self, index=None):

colors = [self.rgba_floats_tuple(x) for x in index]
return LinearColormap(colors, index=index,
vmin=self.vmin, vmax=self.vmax)
vmin=self.vmin, vmax=self.vmax, max_labels=max_labels)

def scale(self, vmin=0., vmax=1.):
def scale(self, vmin=0., vmax=1., max_labels=10):
"""Transforms the colorscale so that the minimal and maximal values
fit the given parameters.
"""
Expand All @@ -422,6 +435,7 @@ def scale(self, vmin=0., vmax=1.):
vmin=vmin,
vmax=vmax,
caption=self.caption,
max_labels=max_labels
)


Expand Down
3 changes: 2 additions & 1 deletion branca/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ def legend_scaler(legend_values, max_labels=10.0):
if len(legend_values) < max_labels:
legend_ticks = legend_values
else:
spacer = int(math.ceil(len(legend_values)/max_labels))
spacer = int(math.ceil(len(legend_values) / (max_labels - 1)))
legend_ticks = []
for i in legend_values[::spacer]:
legend_ticks += [i]
legend_ticks += ['']*(spacer-1)
Copy link
Member

Choose a reason for hiding this comment

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

Not related to your change directly, but I don't understand why we add empty strings in between. There doesn't seem to be a need for it. If the idea is to make the length of legend_ticks correspond to legend_values, it's not even correct since we add one more tick than value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Don't know but commenting it out doesn't change anything apparent.

legend_ticks += [legend_values[-1]]
Copy link
Member

@Conengmo Conengmo Apr 18, 2021

Choose a reason for hiding this comment

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

I understand the desire to always show the last value. But this means we may have differing distances between labels. For example consider values [0, 1, 2, 3] and max_labels=3. Should the ticks then be [0, 2] (current situation, equal distance between values but missing last value) or [0, 2, 3] (your PR, unequal distance between values but including last value)? I don't know which is better at the moment, but in doubt I tend to stick with the status quo. What do you think?

Is there a way to have both? Include the first and last value, and have equal spacing? Or, at least, have the last and second to last label not be too close together.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That is a good point, I didn't realise that.

Is there a way to have both? Include the first and last value, and have equal spacing?

I don't think so. We're picking from a list of variable length. We're not interpolating values here (and I don't think we should).

I am not sure either which of the options is better. The addition of the last value makes it easier to read a bit but it can cause weird overlaps.

Let's keep the status quo for now. I will revert the addition of the last value.

return legend_ticks


Expand Down