This link has R code to replicate ggplot's colours: Plotting family of functions with qplot without duplicating data
I have had a go at replicating the code in python - but the results are not right ...
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
import colorsys
# function to return a list of hex colour strings
def colorMaker(n=12, start=15.0/360.0, saturation=1.0, valight=0.65) :
listOfColours = []
for i in range(n) :
hue = math.modf(float(i)/float(n) + start)[0]
#(r,g,b) = colorsys.hsv_to_rgb(hue, saturation, valight)
(r,g,b) = colorsys.hls_to_rgb(hue, valight, saturation)
listOfColours.append( '#%02x%02x%02x' % (int(r*255), int(g*255), int(b*255)) )
return listOfColours
# made up data
x = np.array(range(20))
d = {}
d['y1'] = pd.Series(x, index=x)
d['y2'] = pd.Series(1.5*x + 1, index=x)
d['y3'] = pd.Series(2*x + 2, index=x)
df = pd.DataFrame(d)
# plot example
plt.figure(num=1, figsize=(10,5), dpi=100) # set default image size
colours = colorMaker(n=3)
df.plot(linewidth=2.0, color=colours)
fig = plt.gcf()
fig.savefig('test.png')
The result ...
Looking closely at the gg code - it looks like I had two problems. The first is the denominator should have been n+1, rather than n. The second problem is that I needed a hue-chroma-luma conversion tool; rather than the hue-luma-saturation approach I had been taking. Unfortunately, I was not aware of a hcl_to_rgb tool in python; so one was written.
My solution (below) addresses these two problems. To my eye, I think it replicates ggplot2's colours.