refer to this answer (thank you for this answer): Get the name of a pandas DataFrame I'm able to extract variable names in the function, but unfortunately, it doesn't always work (I think that may contain some punctuation like "_") In this case, I want to automatically save correlation plots with dataframe name. Sometimes, it works well while following codes below, however, I don't know why sometimes the saved plot name is just "cor__.png". Could somebody help me figure it out? Thank you so much!!
def get_df_name(df):
    name =[x for x in globals() if globals()[x] is df][0]
    return name
import seaborn as sns
import os
# function of correlation
def cor(data, figsize):
    fig = plt.figure(figsize=figsize)
    corr = data.corr()
    ax = sns.heatmap(
        corr, 
        vmin=-1, vmax=1, center=0,
        cmap=sns.diverging_palette(20, 220, n=200),
        square=True,
        annot = True
    )
    ax.set_xticklabels(
        ax.get_xticklabels(),
        rotation=45,
        horizontalalignment='right'
    )
    
    # path = os.path.join(os.getcwd(), "cor_plots", f"{get_df_namedat}.png")
    path = "cor_plots/cor_{a}.png".format(a = get_df_name(data))
    fig.savefig(fname = path)
