I using code below to generate chart with multiple traces. However the only way that i know to apply different colours for each trace is using a randon function that ger a numerico RGB for color.
But random color are not good to presentations.
How can i use a pallet colour for code below and dont get more random colors?
groups53 = dfagingmedioporarea.groupby(by='Area')
data53 = []
colors53=get_colors(50)
for group53, dataframe53 in groups53:
dataframe53 = dataframe53.sort_values(by=['Aging_days'], ascending=False)
trace53 = go.Bar(x=dataframe53.Area.tolist(),
y=dataframe53.Aging_days.tolist(),
marker = dict(color=colors53[len(data53)]),
name=group53,
text=dataframe53.Aging_days.tolist(),
textposition='auto',
)
data53.append(trace53)
layout53 = go.Layout(xaxis={'title': 'Area', 'categoryorder': 'total descending', 'showgrid': False},
yaxis={'title': 'Dias', 'showgrid': False},
margin={'l': 40, 'b': 40, 't': 50, 'r': 50},
hovermode='closest',
template='plotly_white',
title={
'text': "Aging Médio (Dias)",
'y':.9,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})
figure53 = go.Figure(data=data53, layout=layout53)
FIGURE 27.1: Specifying a color in plotly. js' supported format. Just like in ggplot2, you'll have to specify a color in one of the following ways: A hexadecimal string of the form “#rrggbb” or “#rrggbbaa”.
Figures made with Plotly Express can be customized in all the same ways as figures made with graph objects, as well as with PX-specific function arguments.
Adding Traces To Subplots If a figure was created using plotly. subplots. make_subplots() , then supplying the row and col arguments to add_trace() can be used to add a trace to a particular subplot. In [12]: from plotly.subplots import make_subplots fig = make_subplots(rows=1, cols=2) fig.
Many questions on the topic of plotly colors have already been asked and answered. See for example Plotly: How to define colors in a figure using plotly.graph_objects and plotly.express? But it seems that you would explicitly like to add traces without using a loop. Perhaps because the attributes for trace not only differ in color? And to my knowledge there is not yet a description on how to do that efficiently.
dir(px.colors.qualitative)
, or['black', 'grey', 'red', 'blue']
, andnext(palette)
for each trace you decide to add to your figure.And next(palette)
may seem a bit cryptic at first, but it's easily set up using Pythons itertools
like this:
import plotly.express as px
from itertools import cycle
palette = cycle(px.colors.qualitative.Plotly)
palette = cycle(px.colors.sequential.PuBu)
Now you can use next(palette)
and return the next element of the color list each time you add a trace. The very best thing about this is, as the code above suggests, that the colors are returned cyclically, so you'll never reach the end of a list but start from the beginning when you've used all your colors once.
import plotly.graph_objects as go
import plotly.express as px
from itertools import cycle
# colors
palette = cycle(px.colors.qualitative.Bold)
#palette = cycle(['black', 'grey', 'red', 'blue'])
palette = cycle(px.colors.sequential.PuBu
# data
df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
# plotly setup
fig = go.Figure()
# add traces
country = 'Germany'
fig.add_traces(go.Bar(x=[country],
y = df[df['country']==country]['pop'],
name = country,
marker_color=next(palette)))
country = 'France'
fig.add_traces(go.Bar(x=[country],
y = df[df['country']==country]['pop'],
name = country,
marker_color=next(palette)))
country = 'United Kingdom'
fig.add_traces(go.Bar(x=[country],
y = df[df['country']==country]['pop'],
name = country,
marker_color=next(palette)))
fig.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With