Skip to content

Commit 69bdfb7

Browse files
committed
Allow specification of the matplotlib backend with the parameter use
1 parent d20ff2a commit 69bdfb7

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

backtrader/cerebro.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ def getbroker(self):
866866

867867
def plot(self, plotter=None, numfigs=1, iplot=True, start=None, end=None,
868868
savefig=False, figfilename='backtrader-plot-{j}-{i}.png',
869-
width=16, height=9, dpi=300, tight=True,
869+
width=16, height=9, dpi=300, tight=True, use=None,
870870
**kwargs):
871871
'''
872872
Plots the strategies inside cerebro
@@ -880,6 +880,9 @@ def plot(self, plotter=None, numfigs=1, iplot=True, start=None, end=None,
880880
``iplot``: if ``True`` and running in a ``notebook`` the charts will be
881881
displayed inline
882882
883+
``use``: set it to the name of the desired matplotlib backend. It will
884+
take precedence over ``iplot``
885+
883886
``start``: An index to the datetime line array of the strategy or a
884887
``datetime.date``, ``datetime.datetime`` instance indicating the start
885888
of the plot
@@ -924,7 +927,7 @@ def plot(self, plotter=None, numfigs=1, iplot=True, start=None, end=None,
924927
for si, strat in enumerate(stratlist):
925928
rfig = plotter.plot(strat, figid=si * 100,
926929
numfigs=numfigs, iplot=iplot,
927-
start=start, end=end, savefig=savefig)
930+
start=start, end=end, use=use)
928931
# pfillers=pfillers2)
929932

930933
figs.append(rfig)

backtrader/plot/plot.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,17 @@ def drawtag(self, ax, x, y, facecolor, edgecolor, alpha=0.9, **kwargs):
114114
**kwargs)
115115

116116
def plot(self, strategy, figid=0, numfigs=1, iplot=True,
117-
start=None, end=None, savefig=False, **kwargs):
117+
start=None, end=None, use=None, **kwargs):
118118
# pfillers={}):
119119
if not strategy.datas:
120120
return
121121

122122
if not len(strategy):
123123
return
124124

125-
if savefig:
126-
matplotlib.use('agg')
127-
128-
if iplot:
125+
if use is not None:
126+
matplotlib.use(use)
127+
elif iplot:
129128
if 'ipykernel' in sys.modules:
130129
matplotlib.use('nbagg')
131130

0 commit comments

Comments
 (0)