简体   繁体   中英

how to set colorscale with reference to dates in plotly python

first question so go easy on me please, I'm trying to create a scatter plot in plotly where the points are coloured according to a datetime column. but it seems to error out, it works fine if I set the color to, say. a numeric column? is there a way to do this please?

Sample code below. if I change color to, say np.arange(0,graph_data.shape[0]) it would work fine but the colorbar labels would be meaningless.

    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'
                            }
                        )

There may be a better way to do this, but one possible workaround is to convert your datetime to seconds after a given date. You could try the following using the datetime module:

int(datetime.datetime.utcnow().timestamp())

This will then be an integer which will be understood by the scatter function.

Using the principles of Matt's work around, I created a 'number of days from start' column for the colorscale to reference and then customised tick labels and spacing on the colour bar as follows:

    # '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'
                            }
                        )
    )

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