Skip to content

Allow more than 2 colormaps again #493

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

Merged
merged 5 commits into from
Jun 3, 2016
Merged
Changes from 1 commit
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
37 changes: 18 additions & 19 deletions plotly/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,33 +1518,32 @@ def _map_face2color(face, colormap, vmin, vmax):
"and vmax. The vmin value cannot be "
"bigger than or equal to the value "
"of vmax.")
# find the normalized distance t of a triangle face between vmin and
#vmax where the distance is normalized between 0 and 1
t = (face - vmin) / float((vmax - vmin))

if len(colormap) <= 1:
t_color = colormap[0]
color = FigureFactory._convert_to_RGB_255(t_color)
color = FigureFactory._label_rgb(color)

if len(colormap) == 1:
# color each triangle face with the same color in colormap
face_color = colormap[0]
face_color = FigureFactory._convert_to_RGB_255(face_color)
face_color = FigureFactory._label_rgb(face_color)
else:
# account for colormaps with more than one color
incr = 1./(len(colormap) - 1)
low_color_index = int(t/incr)
# find the normalized distance t of a triangle face between vmin
# and vmax where the distance is normalized between 0 and 1
t = (face - vmin) / float((vmax - vmin))
low_color_index = int(t / (1./(len(colormap) - 1)))
Copy link
Contributor

Choose a reason for hiding this comment

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

🐄 Sorry if I'm just missing something, but isn't:

1 / ( 1 / val ) = val

And isn't iterable[len(iterable) - 1] = iterable[-1]

So

iterable[1 / ( 1 / (len(iterable) - 1)] = iterable[-1]

I feel like iterable[-1] is more understandable than iterable[1 / ( 1 / (len(iterable) - 1)]

This means that you don't need to calculate low_color_index = int(t / (1./(len(colormap) - 1))) until you're inside the else statement, which also improves readability here.


if t == 1:
t_color = colormap[low_color_index]
color = FigureFactory._convert_to_RGB_255(t_color)
color = FigureFactory._label_rgb(color)
face_color = colormap[low_color_index]
face_color = FigureFactory._convert_to_RGB_255(face_color)
face_color = FigureFactory._label_rgb(face_color)

else:
t_color = FigureFactory._find_intermediate_color(
face_color = FigureFactory._find_intermediate_color(
colormap[low_color_index],
colormap[low_color_index + 1],
(t - low_color_index * incr) / incr)
color = FigureFactory._convert_to_RGB_255(t_color)
color = FigureFactory._label_rgb(color)
t * (len(colormap) - 1) - low_color_index)
face_color = FigureFactory._convert_to_RGB_255(face_color)
face_color = FigureFactory._label_rgb(face_color)

return color
return face_color
Copy link
Contributor

Choose a reason for hiding this comment

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

💥 so much more readable! thanks!


@staticmethod
def _trisurf(x, y, z, simplices, colormap=None, color_func=None,
Expand Down