I am plotting 20 different lines on a single plot using matplotlib. I use a for loop for plotting and label every line with its key and then use the legend function
for key in dict.keys():
plot(x,dict[key], label = key)
graph.legend()
But using this way, the graph repeats a lot of colors in the legend. Is there any way to ensure a unique color is assigned to each line using matplotlib and over 20 lines?
thanks
To build off of Don Kirkby's answer, if you're willing to install/use seaborn, then you can have colors computed for you:
Aside from being able to use seaborn's various color palettes, you can get a list of RGB tuples that can be used/manipulated later on if need be. Obviously, you could compute something similar using matplotlib's colormaps, but I find this to be handy.
I had a plot with 12 lines, and I found it hard to distinguish lines with similar colours when I tried Yann's technique. My lines also appeared in pairs, so I used the same colour for the two lines in each pair, and used two different line widths. You could also vary the line style to get more combinations.
You could use
set_prop_cycle()
, but I just modified the line objects after callingplot()
.Here is Yann's example with three different line widths:
Here's the same example with different line styles. Of course you could combine the two if you wanted.
The answer to your question is related to two other SO questions.
The answer to How to pick a new color for each plotted line within a figure in matplotlib? explains how to define the default list of colors that is cycled through to pick the next color to plot. This is done with the
Axes.set_color_cycle
method.You want to get the correct list of colors though, and this is most easily done using a color map, as is explained in the answer to this question: Create a color generator from given colormap in matplotlib. There a color map takes a value from 0 to 1 and returns a color.
So for your 20 lines, you want to cycle from 0 to 1 in steps of 1/20. Specifically you want to cycle form 0 to 19/20, because 1 maps back to 0.
This is done in this example:
This is the resulting figure:
Alternative, better (debatable) solution
There is an alternative way that uses a
ScalarMappable
object to convert a range of values to colors. The advantage of this method is that you can use a non-linearNormalization
to convert from line index to actual color. The following code produces the same exact result:Deprecation Note
In more recent versions of mplib (1.5+), the
set_color_cycle
function has been deprecated in favour ofax.set_prop_cycle(color=[...])
.