简体   繁体   中英

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

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.

Here is the screenshot: screenshot

Any idea on how to do this? Many thanks in advance

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

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