0

I am iterating trough a directory with 500 csv files and I create a heatmap for each file, however this currently takes a lot of memory because Python/ Jupyter Notebook is displaying all the heatmaps.

Is there a way to just save the plots in the folder without displaying them ?

# The next line somehow always displays the plot, but is also necessary for the creation  
sns.heatmap(pivot_table,linewidths=.8, cmap="YlGnBu")

# This saves the plot to the file I want it to :
Path( str(os.getcwd()) +"/SBDD2021_Heatmaps_of_HTS_Results").mkdir(parents=True, exist_ok=True)
plt.savefig('SBDD2021_Heatmaps_of_HTS_Results/Heatmap of Plate_ID' + str(df.iat[0,0]))
Charly
  • 83
  • 1
  • 8
  • 2
    have you tried the solution [here?](https://stackoverflow.com/questions/18717877/prevent-plot-from-showing-in-jupyter-notebook) – meowulf May 16 '21 at 09:32
  • 1
    Does this answer your question? [Calling pylab.savefig without display in ipython](https://stackoverflow.com/questions/15713279/calling-pylab-savefig-without-display-in-ipython). I assume you can use: `matplotlib.use('Agg')`. – sandertjuh May 16 '21 at 09:33

1 Answers1

0

So for anyone having the same issue I found a solution: It was so silly... It was just the line plt.close() I tried this earlier but unfortunately I didnt put the line at the right spot it goes after the "sns.heatmap(...)" line

##Generate a heatmap :
# 1. break down the panda table to the only values we need
Row_Col_RawValue = df[["Plate_Row","Plate_Col", "Raw_value"]] 
#2. fit the table for seaborn 
pivot_table = Row_Col_RawValue.pivot("Plate_Row", "Plate_Col", "Raw_value") #general stuff
plt.figure(figsize=(10,10))
plt.xlabel("Plate_column", size = 15)
plt.ylabel("Plate_row",size = 15)

# the "df.iat[0,0] gives us the name for the plate we are currently working on     
plt.title("Heatmap showing HTS Results of Plate_ID: " + str(df.iat[0,0]) )   
sns.heatmap(pivot_table,linewidths=.8, cmap="YlGnBu")

Path( str(os.getcwd()) +"/SBDD2021_Heatmaps_of_HTS_Results").mkdir(parents=True, exist_ok=True)
plt.savefig('SBDD2021_Heatmaps_of_HTS_Results/Heatmap of Plate ' + str(df.iat[0,0]))
plt.close()