I am trying to graph families of curves using Matplotlib. I am graphing the data directly using scatter() and then plotting a fit line (least squares from scipy) using plot(). I do not know how many sets of data there will be beforehand, or the limits, etc.
I need to be able to cycle the colors of these lines and points so everything from one set of data matches. Plot rotates colors using some internal default and scatter is coming out as all one color. The data sets could get close together, so just going with the assumption that it will be clear from which points are close to which fit line is not good enough, and since I don’t know how many curves there will be manually making a color choice is not scalable.
Further, because these are families of curves (think transistor plots), I need to be able to show the relevant labeling with the curve. What I would like to do is to write the information on the fit line itself.
Does anyone know of a good way to either of these?
This tries to answer all your questions.
The code below cycles a maximum of 7 colors. If you need more you should create a more sofisticated generator, as that shown in another answer.
The generator in the above code is a bit of an overkilling for the example, but it gives the structure for a more complex calculation. If you only need a few colors you could use a simple iterator
and if you need to cycle endlessly, you can use
itertools.cycle:Edit: You have several options to get n diferent colors. As I indicated before you can use a generator using the method indicated in other answer. For example, replacing
get_colorwith a different generator:You get 15 different colors.
Similar colors are however contiguous not giving a good resolution/contrast. You can increase contrast by skipping hue values with:
The other problem when drawing many lines is the legend…