I’m trying to assign e.g. an opacity value to all markers of all data traces in patched_figure.
since I cannot use len(patched_figure['data']), since one cannot read from patched_figure. How would I achieve this?
current, non-working approach
for i, _ in enumerate(patched_figure['data']):
patched_figure['data'][i]['marker']['opacity'] = 0.5
I could do something like
i=0
while True:
try:
patched_figure['data'][i]['marker']['opacity'] = 0.5
except IndexError:
break
i += 1
Hi @luggie I think you can’t do that with partial updates. I understand the Patch() as an instruction to change a explicitly defined part of the component.
To test this, I tried to return a list of patch objects to a figure. An extract of the error I got :
If you know the number of traces in the figure, you could do this:
@app.callback(
Output(..., 'figure'),
Input(...),
prevent_initial_call=True
)
def new(_):
# Create a Patch object
patched_figure = Patch()
# assuming to have 7 traces in the figure
for i in range(7):
patched_figure["data"][i].update({"marker": {'opacity': 0.5}})
return patched_figure
EDIT: you don’t even have to know the number of traces. If you want to change all of the traces, you only have to assure, that the range is grater than the number of traces in your figure. Just keep in mind, that the process gets slower with increasing the range. An example:
@app.callback(
Output(..., 'figure'),
Input(...),
prevent_initial_call=True
)
def new(_):
# Create a Patch object
patched_figure = Patch()
# works even if there are only 7 traces in the figure
for i in range(100):
patched_figure["data"][i].update({"marker": {'opacity': 0.5}})
return patched_figure
Yes that’s true. One can iterate higher then what is present without running into IndexError or PlotlyKeyError.
However this is not true for a go.Figure object. This is what broke my app.
you are enumerating the number of figure datas, but you only have 1 figure data hence the error.
you need to enum the list inside figure[‘data’][‘enum this list’]
easier to create a list that stores the number of traces you have and just use that list range