2017-04-24 1338 views
0

有沒有辦法在繪圖中訪問色循環儀?我想爲我的情節中的不同羣體設置不同的顏色,但在該羣組中顏色相同。我知道在matplotlib中,您必須使用色循環器,如this。但是,我不知道我會有多少組,並且覺得這個解決方案不是很優雅。有沒有更簡單的方法,還是必須先定義顏色列表?Plotly按組別更改顏色

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) 

回答

0

您可以使用colorlover用於此目的,例如,

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

for i,group in enumerate(data): 
    gColor = colors[(i * 2 + 1) % 11] #pick a new color every time