I know this question has been asked several times, I did find some post on this forum, but they don't seem to work, that's why I ask again.
Basically I have to create a number of a graph and the code is below
fig1 = go.Figure()
fig1.update_layout(showlegend = True, xaxis_title = "Time(s)")
and I would like to tidy it up using a for loop, therefore replacing the number with a variable, but doing something like below doesn't work
exec('"fig"+str(i) = go.Figure()')
I receive
SyntaxError: cant' assign to operator
How can I tidy this number of "same" code into a neat form, please?
*I also know that there is a danger using exec if this is not the way to go, what would be the better python way, please?
Edit: This is my final working code, thanks for everyone's input
for i in range(5):
figures.update({f'fig{i}':go.Figure()})
eval('figures["fig'+str(i)+'"]').update_layout(showlegend = True, xaxis_title = "Time(s)")
In here, I can have control of the number of variable to be created (this code to be run. Also, putting it into the normal for loop form allows me to do more other things alone with the current figure. Lastly, the dictionary method allows a name to be linked to the figure so provides convenience
"fig"+str(i)does not define a variable! You're generating the string "fig1" on the left of=and assigning to a string is nonsense (hence the syntax error)