How to create a scatter plot with one color for each dot (plotly)

2.2k views Asked by At

I need to create a plot in which each dot corresponds to one color, using the online version of plot.ly. I saw that its possible to do this by adding a column with the codes of the colors in the data section, next to the x and y column, but not i don't know how to indicate to plot.ly that this is the color column. For instance, take this example: color gradient

click on the "fork and edit" button and then try to reproduce the result with the provided data, i can't! The options available in "Traces" are much more complete than the ones that I can access when I create a data-set.

1

There are 1 answers

0
DataDoge On

Make a list for each 'attribute' you want to customize, for each dot. I needed different color and text for each dot.

colors = []
texts = []
x_values = []
y_values = []

Just to make it clear these were the values that were put in the lists:

init_anchor_text = "Initial Anchor No.%d"
anchor_text_real = "Localized Anchor No.%d - real values"
anchor_text = "Localized Anchor No.%d"
unknown_text = "Unknown Node No.%d"
init_anchor_color = 'rgba(50, 96, 171,.9)'
anchor_color = 'rgba(50, 171, 96,.9)'
anchor_color_real = 'rgba(171, 171, 50,.9)'
unknown_color = 'rgba(171, 50, 96,.9)'

After a couple of ifs in my for loop this is how i put the values, by simply using append to the lists:

x_values.append(node.calc_x)
y_values.append(node.calc_y)
colors.append(init_anchor_color)
texts.append((init_anchor_text % node.number))

This is the scatter and plotting:

trace = go.Scatter(
    x=x_values,
    y=y_values,
    mode='markers',
    text=text,
    marker=dict(
        size=10,
        color=colors,
        line=dict(
            width=2,
        )
    )
)

fig = {
    'data': [trace],
    'layout': layout,
}
plotly.offline.plot(fig, filename='TriangulationExercise.html')