|
18 | 18 | floats between 0 and 1 inclusive.
|
19 | 19 |
|
20 | 20 | -----
|
21 |
| -Types of colormap: |
| 21 | +Colormaps and Colorscales: |
| 22 | +A colormap or a colorscale is a correspondence between values - Pythonic |
| 23 | +objects such as strings and floats - to colors. |
| 24 | +
|
22 | 25 | There are typically two main types of colormaps that exist: numerical and
|
23 | 26 | categorical colormaps.
|
24 | 27 |
|
25 |
| -Numerical colormaps are used when a the coloring column being used takes a |
26 |
| -spectrum of values or numbers. Alternatively, a categorical colormap is used |
27 |
| -to assign a specific value in a color column to a specific color everytime it |
28 |
| -appears in the plot at hand. For instance, a column of strings in a dataframe |
29 |
| -would naturally use a categorical colormap. You can choose however to use a |
30 |
| -categorical colormap with a column of numbers. Be careful though, as if you |
31 |
| -have a large set of unique numbers in your column you'll get a lot of colors. |
32 |
| -
|
| 28 | +Numerical: |
| 29 | +---------- |
| 30 | +Numerical colormaps are used when the coloring column being used takes a |
| 31 | +spectrum of values or numbers. |
| 32 | +
|
| 33 | +A classic example from the Plotly library: |
| 34 | +``` |
| 35 | +rainbow_colorscale = [ |
| 36 | + [0, 'rgb(150,0,90)'], [0.125, 'rgb(0,0,200)'], |
| 37 | + [0.25, 'rgb(0,25,255)'], [0.375, 'rgb(0,152,255)'], |
| 38 | + [0.5, 'rgb(44,255,150)'], [0.625, 'rgb(151,255,0)'], |
| 39 | + [0.75, 'rgb(255,234,0)'], [0.875, 'rgb(255,111,0)'], |
| 40 | + [1, 'rgb(255,0,0)'] |
| 41 | +] |
| 42 | +``` |
| 43 | +
|
| 44 | +Notice that this colorscale is a list of lists with each inner list containing |
| 45 | +a number and a color. These left hand numbers in the nested lists go from 0 to |
| 46 | +1, and they are like pointers tell you when a number is mapped to a specific |
| 47 | +color. |
| 48 | +
|
| 49 | +If you have a column of numbers `col_num` that you want to plot, and you know |
| 50 | +
|
| 51 | +``` |
| 52 | +min(col_num) = 0 |
| 53 | +max(col_num) = 100 |
| 54 | +``` |
| 55 | +
|
| 56 | +then if you pull out the number `12.5` in the list and want to figure out what |
| 57 | +color the corresponding chart element (bar, scatter plot, etc) is going to be, |
| 58 | +you'll figure out that proportionally 12.5 to 100 is the same as 0.125 to 1. |
| 59 | +So, the point will be mapped to 'rgb(0,0,200)'. |
| 60 | +
|
| 61 | +All other colors between the pinned values in a colorscale are linearly |
| 62 | +interpolated. |
| 63 | +
|
| 64 | +Categorical: |
| 65 | +------------ |
| 66 | +Alternatively, a categorical colormap is used to assign a specific value in a |
| 67 | +color column to a specific color everytime it appears in the dataset. |
| 68 | +
|
| 69 | +A column of strings in a panadas.dataframe that is chosen to serve as the |
| 70 | +color index would naturally use a categorical colormap. However, you can |
| 71 | +choose to use a categorical colormap with a column of numbers. |
| 72 | +
|
| 73 | +Be careful! If you have a lot of unique numbers in your color column you will |
| 74 | +end up with a colormap that is massive and may slow down graphing performance. |
33 | 75 | """
|
34 | 76 | from __future__ import absolute_import
|
35 | 77 |
|
|
0 commit comments