Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a python list which contains 4 elements, each of which is a time-series dataset. e.g.,

full_set = [Apple_px, Banana_px, Celery_px]

I am charting them via matplotlib and I want to save the charts individually using the variable names.

for n in full_set:

*perform analysis

    plt.savefig("Chart_{}.png".format(n))

The ideal output would have Chart_Apple_px, Chart_Banana_px, Chart_Celery_px as the chart names.

share|improve this question
1  
Possible duplicate of How can I get the name of an object in Python? – Laur Ivan 8 hours ago
1  
what's wrong with Apple_px.__name__? – Laur Ivan 8 hours ago
up vote 0 down vote accepted

In Python names and values are two very different things, stored and dealt with in different ways - and more practically one value can have multiple names assigned to it.

So, instead of trying to get the name of some data value, use a tuple where you assign a title to the dataset in your list of datasets, something like this:

full_set = [('Chart_Apple_px', Apple_px), ('Chart_Banana_px', Banana_px)]

for chart_name, dataset in full_set:
   # do your calculations on the dataset
   plt.savefig("{}.png".format(chart_name))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.