简体   繁体   中英

How can I manually color each point in my scatter-plot in plotly?

I have a list of RGB values for each point in my plot, that is not based on a single discrete value nor category, thus built-in methods in plotly express are not working. I tried using go.Figure(), and adding a new trace for each point, however, that is extremely slow. Is there another way of doing this? It seems extremely simple, I'm surprised there's no way to pass color values array to the plotting function.

marker_color can be an array. See below:

import math
import plotly.graph_objects as go
import numpy as np

S = 500
a = np.random.randint(0, 255, [S, 3]).astype(str)
a = [f'rgb({",".join(c)})' for c in a]

go.Figure(
    go.Scatter(
        x=np.linspace(0, S/10, S),
        y=np.sin(np.linspace(0, math.pi * S/100, S)),
        mode="markers",
        marker_color=a,
    )
)

在此处输入图像描述

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