Question - Save plot without displaying the plot interactively?
ahishsujay opened this issue · 4 comments
Hi! Thank you for creating pyMSAviz
, I've been actively using it to quickly view some MSAs I've generated. While parsing through each MSA and saving them using the .savefig()
function, I noticed that the plots are always displayed interactively and quickly ran out of memory while I was doing this automatically for a bunch of files. Apologize for any ignorance, but is there a way to save the plot and not display it interactively? Added the way I am reading, and saving my MSAs below:
mv = MsaViz(rha_file, show_label=False, color_scheme='Clustal', show_consensus=True)
mv.savefig(f'../figures/{output_directory}_MSA_figures/{format_rha_file}/{format_rha_file}_RHA_MSA_figure.png')
Thank you!
Hi @ahishsujay,
I think the problem can be solved by calling plt.clf()
and plt.close()
as follows.
import matplotlib.pyplot as plt
from pymsaviz import MsaViz
msa_file = "./example.fa"
for i in range(100):
mv = MsaViz(msa_file, wrap_length=60)
mv.savefig(f"result{i}.png")
plt.clf() # Clear figure to reduce memory usage during many figure plots
plt.close() # Close window to suppress show interactive window
Thank you @moshi4, this works! The only thing I noticed is that when I do this, the plots background are transparent (attached picture below):
Any idea how I might be able to fix this?
I don't know why the background of the pyMSAviz plot figure is transparent. I could not reproduce the same result in my environment. Are you tweaking some matplotlib setting?
If you put the following code first, it might plot correctly.
import matplotlib as mpl
mpl.rcParams["figure.facecolor"] = "white"
It may also be possible to fix this by updating the matplotlib version.
@moshi4 Thanks, this seems to be working perfectly. Thank you again!