0

Is there a way to access the color cycler within plotly? I want to have a different color for different groups in my plot, but have the same color within that group. I know in matplotlib, you have to use a color cycler, like this. However, I don't know how many groups I will have and feel like this solution is not very elegant. Is there an easier way, or do I have to define a list of colors first?

import plotly.offline as py
import plotly.graph_objs as go

colors = ['rgba(250,0,0,1)', 'rgba(0,0,250,1)']

x1=[2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013]
x2=[2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013]
x3=[2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013]
x4=[2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013]

y1=[74, 82, 80, 74, 73, 72, 74, 70, 70, 66, 66, 69]
y2=[45, 42, 50, 46, 36, 36, 34, 35, 32, 31, 31, 28]
y3=[13, 14, 20, 24, 20, 24, 24, 40, 35, 41, 43, 50]
y4=[18, 21, 18, 21, 16, 14, 13, 18, 17, 16, 19, 23]

g1=[(x1,y1),(x2,y2)]
g2=[(x3,y3),(x4,y4)]

data=[g1,g2]

traces = []

for i,group in enumerate(data):
    gColor=colors[i] #pick a new color every time
    for trace in group: 
        traces.append(go.Scatter(
            x=trace[0],
            y=trace[1],
            mode='lines',
            line=dict(color=gColor),
            connectgaps=True,
        ))

py.plot(traces)
Community
  • 1
  • 1
IrppyDirppy
  • 61
  • 2
  • 7

1 Answers1

0

You could use colorlover for that purpose, e.g.

colors = colorlover.scales['11']['qual']['Paired']

for i,group in enumerate(data):
    gColor = colors[(i * 2 + 1) % 11] #pick a new color every time
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99