简体   繁体   中英

change color scale in plotly line chart

I use the following function to plot one line per year of data (overlayed so that I can compare one year to the others). What I would like to get to is for each line to have a color defined as a gradient instead of the default color scale (ie 2008 line in light blue to 2017 line in very dark blue).

What is the correct way to do this? I've tried using the colorscale parameter (see code) but can't quite figure it out where to put it.

def plotPMByYear_plotly(df, minyear):
    allYears = list(set(list(df.Year)))
    allYears = [int(x) for x in allYears if str(x) != 'nan']
    data = []
    df = df[df["Year"]>=minyear]
    for year in allYears:
        yeardf = df[df["Year"] == year]
        trace = go.Scatter(
            x = yeardf['Month'],
            y = yeardf['Value'],
            mode = 'lines',
            name = year
        )
        colorscale='Jet',
        data.append(trace)
    iplot(data, filename='line-mode')

在此处输入图片说明

After a bit of digging, I found the solution as in the code below:

from colour import Color

def plotPMByYear_plotly(df, minyear,color1,color2):
    df = df[df["Year"]>=minyear]
    allYears = list(set(list(df.Year)))
    allYears = [int(x) for x in allYears if str(x) != 'nan']
    allYears.sort()
    
    data = []
    i = 0

    for year in allYears:
        yeardf = df[df["Year"] == year]
        trace = go.Scatter(
            x = yeardf['Month'],
            y = yeardf['Value'],
            mode = 'lines',
            
            name = year
        )
        i+=1
        data.append(trace)

    layout = go.Layout(
        title = 'Year on Year comparison'
    )

    fig = go.Figure(data=data, layout=layout)  
    iplot(fig, filename='line-mode')

which would work for example with the following arguments:

plotPMByYear_plotly(dfM, 2008,'white','red')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM