0

Suppose I have Pandas data. Any data. I import seaborn to make a colored version of the correlation between varibales. Instead of passing the correlation expression into the heatmap fuction, and instead of creating a one-time variable to store the correlation output, how can I use the with statement to create temporary variable that no longer existss after the heatmap is plotted?

Doesn't work

# Assume: season = sns, Data is heatmapable
with mypandas_df.correlation(method="pearson") as heatmap_input:
    # possible other statements
    sns.heatmap(heatmap_input)
    # possible other statements

If this exissted, then after seaborn plots the map, heatmap_input no longer exists as a variable. I would like tat functionality.

Long way

# this could be temporary but is now global
tcbtbing = mypandas_df.correlation(method="pearson")
sns.heatmap(tcbtbing)

Compact way

sns.heatmap( mypandas_df.correlation(method="pearson") )

I'd like to use the with statement (or similar short) construction to avoid the Long Way and the Compact way, but leave room for other manipulations, such as to the plot itself.

VISQL
  • 1,960
  • 5
  • 29
  • 41

1 Answers1

1

You need to implement enter and exit for the class you want to use it. see: Implementing use of 'with object() as f' in custom class in python

  • 2
    Wow. That looks quite horrible. Thanks for the insight. It would be too much rigmarole though. Especially since I'm not using it on a custom object of my own. – VISQL Apr 19 '20 at 18:13