How to make Plotly chart with year mapped to line color and months on x-axis

When looking at seasonal data I like to use charts that show a few years at once, with the months running from January to December on the x-axis and the values in the y-axis, using color to distinguish the years. This chart below was created in R using ggplot2. How can I replicate it in Plotly using the Python API?

ggplot2 chart

I can only post one image in this post, but I have included the code for my Plotly attempt below. Suffice it to say that itโ€™s pretty weak. Ideally I want to be able to give Plotly five years of data and an array with five colors and have it map each year to a color without me having to manually specify things. Basically I just donโ€™t understand Plotlyโ€™s color-mapping paradigm well enough yet.

Code for Python example:

import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
import numpy as np

monthindex=pd.date_range('1/1/2014', periods=num_periods, freq='MS')
dd = pd.DataFrame(data={'date':monthindex,
                        'c1': np.random.uniform(10, 20, size=num_periods),
                        'c2': np.random.uniform(30, 40, size=num_periods)},
dd['year'] = dd['date'].dt.year
dd['monthname'] = dd['date'].dt.strftime('%b')

outdata = [
        x=dd['monthname'], # assign x as the dataframe column 'x'

layout = go.Layout(
    title="'Stacking' years in plotly",

Code for R example:


dd <- data.frame(date = seq(as.Date("2014/1/1"),
                     by = "month",
                     length.out = 24),
                 c1 = runif(24, min = 10, max = 20))

dd$month <- as.integer(strftime(dd$date, "%m"))
dd$year <- strftime(dd$date, "%Y")

xscale <- data.frame(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12),
                     labels = c('Jan','Feb','Mar','Apr',

ggplot(dd, aes(month, c1)) +
    geom_line(aes(colour = factor(year))) +
        scale_x_continuous(breaks = xscale$breaks,
                           labels = xscale$labels) +
            scale_colour_manual("year",values=c("Red","Blue")) +
                ggtitle("'Stacking' years in ggplot2")

The following docs should be helpful for you: line chart with pandas and styling line plots in python. The second link will direct you to a very similar line graph with months on the x-axis and blue and red coloring

Thank you for your response.

The latter link in particular uses a method of explicitly creating n different traces, each one styled manually. That appears to be a little different to the result I am aiming for, which is to have one trace (a monthly time series n years long) the color of which is automatically assigned according to a categorical variable in the data (the year).

Plots achieving a similar effect to the one I want would, for example, color a line in a time series depending on whether the value is greater than a certain value e.g. if temperature water is more than 100C, color line red, else color blue. That sort of thing. Do you have any suggestions for this?