Python colors

#basic

Getting different colors for your visualizations is out-of-the-box in Python, so don't need to overthink it. But... you can get as much picky as you want... with "a bit" of additional coding.

Python's most used plotting library is matplotlib. With it you can do most of the visualizations, and if stuff gets complicated you can turn to other libraries (build on top of matplotlib) as seaborn, bokeh, vaex, and others. That being said, the named colors we are going to "unveil" here can be used across most (if not all) the available plotting libraries. Sounds good? Let's get them!

Let's start simple: there are some base colors in matplotlib, as can be seen from the left plot. But they're not very attractive, at least for me. These are named:

  • 'b'- blue
  • 'c' - cyan
  • 'g' - green
  • 'k' - black
  • 'm' - magenta
  • 'r' - red
  • 'w' - white
  • 'y' - yellow

If we want to take more advantage of the available colors, we will need the whole list (name and a little sample). I made a piece of code to generate both set of colors. In both cases, matplotlib has some methods you can use to extract the colors information, so we are not creating the wheel again, just using what is provided.

We could get some additional sophistication, like use HTML codes and experiment creating your own colors. My take home advice is: start simple, use what is provided, and focus more in your analysis/visualizations. If after a while you're comfortable, do some experimentation!

Fun fact: What matplotlib does is to transform a set of 3 or 4 numbers to a color (transforming these numbers to a range between 0 and 1, think of it as a percentage where 1 is 100%) When 3 numbers are used, the result is a color that is a combination of red, blue, and green... the so called RGB scale!. When a fourth number is also used, this additional parameter adds the transparency of the color (called "alpha")

Code: If you're interested in play with it, you'll find it on my Github