dnaiel
September 19, 2019, 3:53pm
1
Hi,
If I edit in chart studio I can choose a categorical color scale under Style -> General -> ColorScales -> Categorical -> Dark24. I attach image of chart studio here.
But when I try to do this in plot.ly in python API I cannot find an equivalent for this.
It seems to supposedly be in the layout definition, but this does not accept categorical as a valid colorscale definition. I.e.,
layout = {
'barmode':'relative',
'title_text':'Industrial Production Y/Y Growth Level %s by Subsector' %(desired_level),
'colorscale': {'categorical':'dark24'}
}
Thatโd give the error:
ValueError: Invalid property specified for object of type plotly.graph_objs.layout.Colorscale: โcategoricalโ
This is the full python code I am using:
import dash_core_components as dcc
import datetime
import plotly.graph_objs as go
trace1 = go.Bar(
x=[datetime.date(2019,1,1), datetime.date(2019,1,2)],
y=[0.5,0.8],
name='series 1')
trace2 = go.Bar(
x=[datetime.date(2019,1,1), datetime.date(2019,1,2)],
y=[0.3,0.4],
name='series 2')
traces = [trace1, trace2]
layout = {
'barmode':'relative',
'title_text':'test'
}
fig = go.Figure(data=traces, layout=layout)
dcc.Graph(figure=fig)
Output in plot.ly dash:
Would this syntax using plotly express be useful?
import plotly.express as px
tips = px.data.tips()
fig = px.bar(tips, x="sex", y="total_bill", color='tip', color_continuous_scale=px.colors.qualitative.Dark24)
fig.show()
Actually the color depends on how you want to set the color (is it from numerical or non-numerical values). If you could provide some code using go
(without the colorscale set properly) it would help.
dnaiel
September 19, 2019, 6:07pm
4
Thanks! yes, no problem, I will update the code in the Q
dnaiel
September 19, 2019, 6:13pm
5
Hi, I just provided the code in the Q.
You can set the color of each trace using a given colormap like
import dash_core_components as dcc
import datetime
import plotly.graph_objs as go
import plotly.express as px
trace1 = go.Bar(
x=[datetime.date(2019,1,1), datetime.date(2019,1,2)],
y=[0.5,0.8],
name='series 1',
marker_color=px.colors.qualitative.Dark24[0])
trace2 = go.Bar(
x=[datetime.date(2019,1,1), datetime.date(2019,1,2)],
y=[0.3,0.4],
name='series 2',
marker_color=px.colors.qualitative.Dark24[1])
traces = [trace1, trace2]
layout = {
'barmode':'relative',
'title_text':'test'
}
fig = go.Figure(data=traces, layout=layout)
fig.show()
3 Likes
dnaiel
September 19, 2019, 6:34pm
7
I like this solution, not as elegant as setting one colorscale in the layout but getโs the job done. thanks!
there might be a better solution but this is the only way I found!
1 Like