Plotly按组更改颜色

时间:2017-04-24 19:35:25

标签: python plotly

有没有办法在曲线内访问色彩循环器?我希望在我的绘图中为不同的组设置不同的颜色,但在该组中具有相同的颜色。我知道在matplotlib中,你必须使用颜色循环仪,如this。但是,我不知道我将拥有多少组,并且觉得这个解决方案不是很优雅。有更简单的方法,还是我必须首先定义颜色列表?

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)

1 个答案:

答案 0 :(得分: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