0

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

2
  • "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) Commented Jan 24, 2020 at 9:33
  • @Victor Check the below code Commented Jan 24, 2020 at 9:41

1 Answer 1

2

Why not try this:

figures = []

for i in range(loop):
    figures.append(go.Figure())

#at this point you will now have n figures each of which you can reference

This way you still can find all of your plots if you know which order you made them in. Otherwise you could simply use a dict. I think its cleaner than making 10 diffrent variables.

Sign up to request clarification or add additional context in comments.

7 Comments

Even better: figures = [go.Figure() for _ in range(num_figures)]
List comprehension would definitely be faster :D Although coding a normal for loop is much more readable at least for prototyping :)
I disagree on the readability, it's only more readable for people not used to python and much more succint (no need to track the meaning of "figures", it's self described in the same line). Aside from that, comprehensions are preferred to for loops in python ("it's more pythonic" some would say...)
Just to add an alternative spin to the neat suggestion by @GPhilo - if you prefer dictionaries, you can figures = {f'fig{i}': fig for i, fig in enumerate(go_figures)} or similar! The repeated 'fig' might be redundant but it's convenient for labelling plots ;)
Thanks GPhilo for all your comments including those from other answer, I agree that good practice is very important, especially I really just step into the python world for not long.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.