I'm looking for a fast way to map scalars to hex colors in python:
import matplotlib
import matplotlib.cm as cm
import matplotlib.colors as mcol
np.random.seed(0)
df = pd.DataFrame(np.random.rand(20000,1))
df.head()
0
0 0.548814
1 0.715189
2 0.602763
3 0.544883
4 0.423655
I have 20 colors only, so I wonder if matplotlib is the best solution, or a simple lookup table would be better.
colors = ["#084594", "#0F529E", "#1760A8", "#1F6EB3", "#2979B9", "#3484BE", "#3E8EC4",
"#4A97C9", "#57A0CE", "#64A9D3", "#73B2D7", "#83BBDB", "#93C4DE", "#A2CBE2",
"#AED1E6", "#BBD6EB", "#C9DCEF", "#DBE8F4", "#EDF3F9", "#FFFFFF"]
values = df[0].values
@profile
def apply_method(): # 6.9 sec
cm1 = mcol.ListedColormap(colors)
norm = matplotlib.colors.Normalize(vmin=np.min(values), vmax=np.max(values), clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm1)
return df[0].apply(lambda row: mcol.to_hex(mapper.to_rgba(row)))
%time apply_method()
From the profiler I see that to_rgba()
is the most expensive method (6.5 sec for only 20.000 values).
So I'm looking at a way to bypass the to_rgba() method. Is there a way to get the color ranges from cm.ScalarMappable? And then do a lookup to the right hex color?