0

I have several lists that I want to write a function and use loop to make multiple heatmaps out of those lists. Then I want to name each heatmap by the name of the list. How can I turn the list name into string?

list1 = [[1,2,3], [4,5,6], [4,5,6], [4,5,6]]
list2 = [[2,2,3], [1,5,1], [4,2,3], [3,3,6]]
data = (list1, list2)
def hm(df):
   sns.set_style("whitegrid")
   ax = sns.heatmap(df, cmap=cmap)
   plt.title('df')
   return fig
for l in data:
   hm(1)
     

1 Answer 1

1

In a normal case, you cannot retroactively retrieve the name of a variable from it's value. There might be some hacky ways around this, but I would recommend to just use a dictionary to retain the lists name:

list1 = [[1,2,3], [4,5,6], [4,5,6], [4,5,6]]
list2 = [[2,2,3], [1,5,1], [4,2,3], [3,3,6]]
data = {
    "list1": list1,
    "list2": list2,
}

def hm(name, df):
   sns.set_style("whitegrid")
   ax = sns.heatmap(df, cmap=cmap)
   plt.title('df')
   # Do something with the name variable
   return fig
   
for name, l in data.items():
    hm(name, l)
Sign up to request clarification or add additional context in comments.

Comments

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.