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

Conversation

martinfleis
Copy link
Collaborator

Closes #88

This PR introduces 2 changes to the ColorMap labels.

  1. It exposes max_labels keyword to control how many labels should be shown. At the moment there is a hard-coded value of 10, which often causes overlaps.
  2. It changes which labels are used to always have the minimum and the maximum. Current behaviour shows min, 8 in between and the second to last, not the last (max). This issue is not so apparent with 10 labels but if you use 2 you get (without this change) one in the beginning and one in the middle of the color bar which is probably not intended.

We are currently working on folium-based plotting to be included in GeoPandas and this is one of the things which would be great to fix to make a smoother user experience.

One question - how do you want to test it? I see that branca tests are essentially written as a check that it the code does not fail.

@martinfleis
Copy link
Collaborator Author

CI failures are not related to this PR.

Copy link
Member

@Conengmo Conengmo left a comment

Choose a reason for hiding this comment

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

I'm okay with exposing this new argument: it's pretty straight forward, it doesn't yet clutter the __init__ and setting the number of labels is a pretty important part of a colorbar.

I made #93, so you can adapt/extend these test cases based on your changes.

branca/colormap.py Outdated Show resolved Hide resolved
branca/colormap.py Outdated Show resolved Hide resolved
legend_ticks = []
for i in legend_values[::spacer]:
legend_ticks += [i]
legend_ticks += ['']*(spacer-1)
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.

@@ -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.

@martinfleis
Copy link
Collaborator Author

I've added basic tests for LinearColormap and StepColormap, fixed docstrings and reverted the addition of the last value to the legend. Should be ready for another round of review/merge.

@Conengmo Conengmo self-requested a review April 20, 2021 07:51
Copy link
Member

@Conengmo Conengmo left a comment

Choose a reason for hiding this comment

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

Changes look good 👍 You may have noticed that we are working on getting the tests running again. When they do, let's make sure this PR passes, then we can merge it. If #95 is not enough and it'll take longer to get the tests working, we can also merge this earlier.

tests/test_colormap.py Outdated Show resolved Hide resolved
@Conengmo Conengmo merged commit 1a4b712 into python-visualization:master Jun 19, 2021
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

Successfully merging this pull request may close these issues.

Expose legend_scaler's max_labels in ColorMap
2 participants