siegfried
siegfried

Reputation: 451

Customise plot labels in a loop

I am using a for loop to plot scatterplots of x_i and y for each xi by looping. I would like to set the xlabel (within the for loop) in such a manner:
plt.xlabel( 'Predictor x_i', X[i] )
Here X[i] should give the variable name of the ith element in the list X.

However this gives more than one argument to plt.xlabel. Is there some way to implement this idea? Thank you in advance.

Upvotes: 1

Views: 846

Answers (1)

tdy
tdy

Reputation: 41327

If I understand correctly, you can interpolate X[i] (or any of the other vars) using braces inside an f-string:

plt.xlabel(f'Predictor x_i {X[i]}')
           ^               ^    ^

Upvotes: 1

Related Questions