最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get a discrete colour bar for plotly go.Scatter? - Stack Overflow

programmeradmin3浏览0评论

I have defined a list of 5 colours, which I use to colour the markers in the go.Scatter plot below. I want the colour bar to show these discrete values, but the resulting colour bar is empty.

I can get a continuous colour bar without any problems, but I want a discrete one.

The gist of my code in relation to this is:

marker_palette=[
     'rgb(39, 100, 25)',   # ++
     'rgb(156, 207, 100)', # +
     'rgb(247, 247, 247)', # 0
     'rgb(232, 150, 196)', # -
     'rgb(142, 1, 82)',    # --
] 

scatter = go.Scatter(
    ...
    marker_color=[marker_palette[o] for o in outputs],
    marker=dict(
        colorbar={
            'tickmode': 'array',
            'tickvals': np.unique(outputs),
            'ticktext': ['++', '+', '0', '-', '--']
        },
    ),
)

How do I get a colour bar that shows the discrete colours?

I would prefer solutions using graph_objects as opposed to plotly.express, as I want to see how the internals get configured in your answer (though it's not a strict requirement).

Reproducible example

import numpy as np
import plotly.graph_objects as go

#Data for testing
outputs = np.random.choice([0, 1, 2, 3, 4], size=100)

#Figure
marker_palette=[
     'rgb(39, 100, 25)',   # ++
     'rgb(156, 207, 100)', # +
     'rgb(247, 247, 247)', # 0
     'rgb(232, 150, 196)', # -
     'rgb(142, 1, 82)',    # --
] 

scatter = go.Scatter(
    x=np.random.randn(100),
    y=np.random.randn(100),
    mode='markers',
    marker_color=[marker_palette[o] for o in outputs],
    marker=dict(
        # color=outputs, colorscale=marker_palette, #creates a continuous scale - not what I want
        colorbar={
            'title': 'outputs',
            'tickmode': 'array',
            'tickvals': np.unique(outputs),
            'ticktext': ['++', '+', '0', '-', '--']
        },
        #/colorbar
    ),
    #/marker
)
go.Figure(scatter)

I have defined a list of 5 colours, which I use to colour the markers in the go.Scatter plot below. I want the colour bar to show these discrete values, but the resulting colour bar is empty.

I can get a continuous colour bar without any problems, but I want a discrete one.

The gist of my code in relation to this is:

marker_palette=[
     'rgb(39, 100, 25)',   # ++
     'rgb(156, 207, 100)', # +
     'rgb(247, 247, 247)', # 0
     'rgb(232, 150, 196)', # -
     'rgb(142, 1, 82)',    # --
] 

scatter = go.Scatter(
    ...
    marker_color=[marker_palette[o] for o in outputs],
    marker=dict(
        colorbar={
            'tickmode': 'array',
            'tickvals': np.unique(outputs),
            'ticktext': ['++', '+', '0', '-', '--']
        },
    ),
)

How do I get a colour bar that shows the discrete colours?

I would prefer solutions using graph_objects as opposed to plotly.express, as I want to see how the internals get configured in your answer (though it's not a strict requirement).

Reproducible example

import numpy as np
import plotly.graph_objects as go

#Data for testing
outputs = np.random.choice([0, 1, 2, 3, 4], size=100)

#Figure
marker_palette=[
     'rgb(39, 100, 25)',   # ++
     'rgb(156, 207, 100)', # +
     'rgb(247, 247, 247)', # 0
     'rgb(232, 150, 196)', # -
     'rgb(142, 1, 82)',    # --
] 

scatter = go.Scatter(
    x=np.random.randn(100),
    y=np.random.randn(100),
    mode='markers',
    marker_color=[marker_palette[o] for o in outputs],
    marker=dict(
        # color=outputs, colorscale=marker_palette, #creates a continuous scale - not what I want
        colorbar={
            'title': 'outputs',
            'tickmode': 'array',
            'tickvals': np.unique(outputs),
            'ticktext': ['++', '+', '0', '-', '--']
        },
        #/colorbar
    ),
    #/marker
)
go.Figure(scatter)
Share Improve this question asked Mar 14 at 19:52 MuhammedYunusMuhammedYunus 5,1552 gold badges3 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Repeat each colour twice to define bands of constant colour and supply it as marker=dict(color=..., colorscale=...):

import numpy as np
import plotly.graph_objects as go

#Data for testing
outputs = np.random.choice([0, 1, 2, 3, 4], size=100)

#Figure
marker_colorscale=[
     (0, 'rgb(39, 100, 25)'), (1/5, 'rgb(39, 100, 25)'),
     (1/5, 'rgb(156, 207, 100)'), (2/5, 'rgb(156, 207, 100)'),
     (2/5, 'rgb(247, 247, 247)'), (3/5, 'rgb(247, 247, 247)'),
     (3/5, 'rgb(232, 150, 196)'), (4/5, 'rgb(232, 150, 196)'),
     (4/5, 'rgb(142, 1, 82)'), (5/5, 'rgb(142, 1, 82)'),
] 

scatter = go.Scatter(
    x=np.random.randn(100),
    y=np.random.randn(100),
    mode='markers',
    marker=dict(
        color=outputs,
        colorscale=marker_colorscale,
        colorbar={
            'title': 'outputs',
            'tickmode': 'array',
            'tickvals': np.unique(outputs),
            'ticktext': ['++', '+', '0', '-', '--']
        },
        #/colorbar
    ),
    #/marker
)
go.Figure(scatter)
发布评论

评论列表(0)

  1. 暂无评论