@@ -351,8 +351,9 @@ def read_mprofile_file(filename):
351351def plot_file (filename , index = 0 , timestamps = True , children = True , options = None ):
352352 try :
353353 import pylab as pl
354- except ImportError :
354+ except ImportError as e :
355355 print ("matplotlib is needed for plotting." )
356+ print (e )
356357 sys .exit (1 )
357358 import numpy as np # pylab requires numpy anyway
358359 mprofile = read_mprofile_file (filename )
@@ -455,11 +456,6 @@ def plot_action():
455456 raise OptionValueError ("'%s' option must contain two numbers separated with a comma" % value )
456457 setattr (parser .values , option .dest , newvalue )
457458
458- try :
459- import pylab as pl
460- except ImportError :
461- print ("matplotlib is needed for plotting." )
462- sys .exit (1 )
463459 desc = """Plots using matplotlib the data file `file.dat` generated
464460using `mprof run`. If no .dat file is given, it will take the most recent
465461such file in the current directory."""
@@ -477,8 +473,21 @@ such file in the current directory."""
477473 type = "str" , action = "callback" ,
478474 callback = get_comma_separated_args ,
479475 help = "Plot a time-subset of the data. E.g. to plot between 0 and 20.5 seconds: --window 0,20.5" )
476+ parser .add_option ("--backend" ,
477+ help = "Specify the Matplotlib backend to use" )
480478 (options , args ) = parser .parse_args ()
481479
480+ try :
481+ if options .backend is not None :
482+ import matplotlib
483+ matplotlib .use (options .backend )
484+
485+ import pylab as pl
486+ except ImportError as e :
487+ print ("matplotlib is needed for plotting." )
488+ print (e )
489+ sys .exit (1 )
490+
482491 profiles = glob .glob ("mprofile_??????????????.dat" )
483492 profiles .sort ()
484493
0 commit comments