I’m using plotly Python to make a graph showing my day. Here’s an example:
and here’s the code that does this:
timeentry = namedtuple("timeentry", ["delta", "category", "activity"])
def make_stacked_bar(entries: list[timeentry]):
import plotly.graph_objects as go
import plotly
secs_per_minute = 60
time_label = "minutes"
times = [d.delta.total_seconds()/secs_per_minute for d in entries]
categories = [d.category for d in entries]
color_map = {}
for category in set(categories):
color_map[category] = len(color_map)
colorscale = 'viridis'
# colors = plotly.colors.qualitative.dark24
fig = go.figure()
for idx, category in enumerate(categories):
# print(f"category: {category} with color = {color_map[category]}")
fig.add_trace(go.bar(
x=[time_label],
y=[times[idx]],
name=category,
text=[category],
textposition='auto',
marker=dict(colorscale=colorscale, cmin=0, cmax=len(color_map) - 1, color=color_map[category]),
# marker=dict(color=colors[color_map[category]]),
width=0.7,
showlegend=false
))
fig.update_layout(yaxis_title=time_label,height=1000, barmode='stack')
fig.show()
Note, that there’s a bug in my chart. Specifically, 2 different bars with the same name, like “Work” might have different colors. I can fix this bug by explicitly setting a color using something like marker=dict(color=plotly.colors.qualitative.dark24[color_map[category]]
, but this looks much less aesthetically pleasing than using the Viridis color scale.
Does anyone know how I can fix my code such that it uses the viridis color scale?