[Matplotlib-users] equally spaced nice colors?

Saito Kotaro (PSI) kotaro.saito at psi.ch
Mon Apr 1 17:23:20 EDT 2019


Dear Neal,

Answer 1: If you are looking for equally spaced colors from a given colormap.

Choose your favorite “nice" colormap from here.
https://matplotlib.org/tutorials/colors/colormaps.html

I use viridis for example.

import matplotlib.pyplot as plt

cmap = plt.get_cmap('viridis')
print('Default number of colors in the lookup table:', len(cmap.colors)) # default: 256 colors
print('First color:', cmap.colors[0])
print('Last color:', cmap.colors[-1])
print('10 equally spaced colors:', cmap(np.linspace(0, 1, 10)))
print('wrong example:', cmap(range(10)))

And you’ll get this:

Default number of colors in the lookup table: 256
First color: [0.267004, 0.004874, 0.329415]
Last color: [0.993248, 0.906157, 0.143936]
10 equally spaced colors: [[ 0.267004  0.004874  0.329415  1.      ]
 [ 0.281412  0.155834  0.469201  1.      ]
 [ 0.244972  0.287675  0.53726   1.      ]
 [ 0.190631  0.407061  0.556089  1.      ]
 [ 0.147607  0.511733  0.557049  1.      ]
 [ 0.119699  0.61849   0.536347  1.      ]
 [ 0.20803   0.718701  0.472873  1.      ]
 [ 0.430983  0.808473  0.346476  1.      ]
 [ 0.709898  0.868751  0.169257  1.      ]
 [ 0.993248  0.906157  0.143936  1.      ]]
wrong example: [[ 0.267004  0.004874  0.329415  1.      ]
 [ 0.26851   0.009605  0.335427  1.      ]
 [ 0.269944  0.014625  0.341379  1.      ]
 [ 0.271305  0.019942  0.347269  1.      ]
 [ 0.272594  0.025563  0.353093  1.      ]
 [ 0.273809  0.031497  0.358853  1.      ]
 [ 0.274952  0.037752  0.364543  1.      ]
 [ 0.276022  0.044167  0.370164  1.      ]
 [ 0.277018  0.050344  0.375715  1.      ]
 [ 0.277941  0.056324  0.381191  1.      ]]

See these for details.
https://matplotlib.org/api/cm_api.html#matplotlib.cm.get_cmap
https://matplotlib.org/tutorials/colors/colormap-manipulation.html#getting-colormaps-and-accessing-their-values

Answer 2: If you are looking for "evenly-spaced colors in a circular color space" with constant brightness and saturation like hls in seaborn,

I guess matplotlib doesn’t support that type of colormaps for the moment as you can find here https://matplotlib.org/tutorials/colors/colormaps.html.
You need to make your own or find ones that someone made like hls in seaborn.

Best regards,

Kotaro

//================//================//
Paul Scherrer Institut
Kotaro SAITO 斉藤耕太郎
Laboratory for Neutron Scattering and Imaging
WHGA/348
5232 Villigen PSI, Schweiz
+41 56 310 3179
kotaro.saito at psi.ch
https://sites.google.com/view/kotarosaito/
//================//================//

> 2019/02/22 14:01、Neal Becker <ndbecker2 at gmail.com>のメール:
> 
> I'm currently using seaborn to get equally spaced nice colors:
> 
>        import seaborn as sns
>        colors = np.array(sns.color_palette("hls", len (solution)))
> 
> Now that mpl has all new improved color models, do I still need to use sns
> or can I easily do something similar with mpl directly?
> 
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users



More information about the Matplotlib-users mailing list