如何在plotly python中根据日期设置colorscale

如何在plotly python中根据日期设置colorscale,python,date,colors,plotly,Python,Date,Colors,Plotly,第一个问题,请对我放松点! 我试图在plotly中创建散点图,其中点根据datetime列着色,但它似乎出错了。如果我将颜色设置为数字列,效果很好。请问有没有办法做到这一点 下面是示例代码。如果我将颜色更改为,比如说np.arange(0,graph\u data.shape[0])它可以正常工作,但是颜色条标签将毫无意义 fig1 = go.Figure() fig1.add_trace(go.Scatter( x=graph_

第一个问题,请对我放松点! 我试图在plotly中创建散点图,其中点根据datetime列着色,但它似乎出错了。如果我将颜色设置为数字列,效果很好。请问有没有办法做到这一点

下面是示例代码。如果我将颜色更改为,比如说
np.arange(0,graph\u data.shape[0])
它可以正常工作,但是颜色条标签将毫无意义

    fig1 = go.Figure()

    fig1.add_trace(go.Scatter(
                        x=graph_data['x_data'],
                        y=graph_data['y_data'],
                        mode='markers',
                        marker={
                            'size': 15,
                            'opacity': 0.95,
                            'line': {'width': 0.5, 'color': 'white'},
                            'color': graph_data['date'],
                            'colorbar': {'title': 'Date'},
                            'colorscale': 'Viridis'
                            }
                        )

也许有更好的方法可以做到这一点,但一个可能的解决方法是将
datetime
转换为给定日期后的秒数。您可以使用该模块尝试以下操作:


然后,这将是一个整数,散射函数可以理解。

利用Matt的工作原理,我创建了一个“从开始算起的天数”列,用于参考色标,然后在颜色栏上定制刻度标签和间距,如下所示:

    # 'date' column contains the dates I want to set colorscale on
    # set minimum date
    min_date = graph_data['date'].min()

    # create column giving number of days from the minimum date, as the colour reference
    graph_data['days'] = graph_data['date'].apply(lambda x: (x-min_date).days)

    # here I want colorbar tick labels every 7 days, so I create a list of the
    # required multiples of 7 
    max_days = graph_data['days'].max()
    fig1ticks = np.arange(0, (int(max_days/7)+1)*7, 7)

    # use datetime.timedelta function to create the dates that match the tick values
    fig1datetimes = [min_date + datetime.timedelta(days=i) for i in fig1ticks.tolist()]

    # and create text strings of these dates in a suitable format
    fig1text = [i.strftime("%d-%b-%Y") for i in fig1datetimes]

    fig1 = go.Figure()

    fig1.add_trace(go.Scatter(
                        x=graph_data['x_data'],
                        y=graph_data['y_data'],
                        mode='markers',
                        marker={
                            'size': 15,
                            'opacity': 0.95,
                            'line': {'width': 0.5, 'color': 'white'},

                            # set color reference to new 'days' column
                            'color': graph_data['days'],

                            # set 'tickvals' and 'ticktext' in colorbar dict
                            'colorbar': {'title': 'Date',
                                         'tickvals': fig1ticks,
                                         'ticktext': fig1text,
                                         },
                            'colorscale': 'Viridis'
                            }
                        )
    )


谢谢,这让我半途而废,但我仍然有一个问题,颜色栏上的标签不是颜色实际代表的日期。但您可以通过在colorbar字典中设置“ticktext”来定制您的colorbar标签。在我的例子中,几天而不是几秒钟就足够了,我想每7天显示一次日期,所以使用了一个稍微不同的函数,并使用了一些非常不符合python的代码——我将添加我的答案代码
    # 'date' column contains the dates I want to set colorscale on
    # set minimum date
    min_date = graph_data['date'].min()

    # create column giving number of days from the minimum date, as the colour reference
    graph_data['days'] = graph_data['date'].apply(lambda x: (x-min_date).days)

    # here I want colorbar tick labels every 7 days, so I create a list of the
    # required multiples of 7 
    max_days = graph_data['days'].max()
    fig1ticks = np.arange(0, (int(max_days/7)+1)*7, 7)

    # use datetime.timedelta function to create the dates that match the tick values
    fig1datetimes = [min_date + datetime.timedelta(days=i) for i in fig1ticks.tolist()]

    # and create text strings of these dates in a suitable format
    fig1text = [i.strftime("%d-%b-%Y") for i in fig1datetimes]

    fig1 = go.Figure()

    fig1.add_trace(go.Scatter(
                        x=graph_data['x_data'],
                        y=graph_data['y_data'],
                        mode='markers',
                        marker={
                            'size': 15,
                            'opacity': 0.95,
                            'line': {'width': 0.5, 'color': 'white'},

                            # set color reference to new 'days' column
                            'color': graph_data['days'],

                            # set 'tickvals' and 'ticktext' in colorbar dict
                            'colorbar': {'title': 'Date',
                                         'tickvals': fig1ticks,
                                         'ticktext': fig1text,
                                         },
                            'colorscale': 'Viridis'
                            }
                        )
    )