Skip to content
Advertisement

How to extend the color palette in matplotlib?

I coded the following function:

JavaScript

And then the for-loop:

JavaScript

As the for loop iterates over num_columns which has 40 variables, the standard palette only offers 10 colors. However, I want to have every variable its own color. Is there a way to code it also being flexible when it comes to the number of variables?

Advertisement

Answer

Matplotlib offers tab20, which is too restrictive for your case. Since you have a lot of lines, a possible solution is to use a colormap, or more than one. Take a look at the available color maps.

A single colormap

By choosing an appropriate colormap, we will have a decent capability to understand the plot. For example, using hsv:

JavaScript

enter image description here

As you can see, the first and last lines uses similar colors, so if the colormap is cyclic (such as hsv) it might be a good idea to restrict the discretization range, for example discr = np.linspace(0, 0.75, N).

Creating colors from multiple colormaps

Matplotlib offers many diverging colormaps. We can use them to create a combination of colors, for example:

JavaScript

enter image description here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement