admin管理员组文章数量:1390531
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
1 Answer
Reset to default 1Repeat 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)
本文标签: How to get a discrete colour bar for plotly goScatterStack Overflow
版权声明:本文标题:How to get a discrete colour bar for plotly go.Scatter? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744634763a2616758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论