0

I'm running Backtrader for the first time and am having this issue when running cerebro.plot():

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[3], line 158
    155 print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    157 # Plot the result
--> 158 cerebro.plot()

File /opt/homebrew/lib/python3.10/site-packages/backtrader-1.9.76.123-py3.10.egg/backtrader/cerebro.py:989, in Cerebro.plot(self, plotter, numfigs, iplot, start, end, width, height, dpi, tight, use, **kwargs)
    987 for stratlist in self.runstrats:
    988     for si, strat in enumerate(stratlist):
--> 989         rfig = plotter.plot(strat, figid=si * 100,
    990                             numfigs=numfigs, iplot=iplot,
    991                             start=start, end=end, use=use)
    992         # pfillers=pfillers2)
    994         figs.append(rfig)

File /opt/homebrew/lib/python3.10/site-packages/backtrader-1.9.76.123-py3.10.egg/backtrader/plot/plot.py:262, in Plot_OldSync.plot(self, strategy, figid, numfigs, iplot, start, end, **kwargs)
    257 # Applying fig.autofmt_xdate if the data axis is the last one
    258 # breaks the presentation of the date labels. why?
    259 # Applying the manual rotation with setp cures the problem
    260 # but the labels from all axis but the last have to be hidden
    261 for ax in laxis:
--> 262     self.mpyplot.setp(ax.get_xticklabels(), visible=False)
    264 self.mpyplot.setp(lastax.get_xticklabels(), visible=True,
    265                   rotation=self.pinf.sch.tickrotation)
    267 # Things must be tight along the x axis (to fill both ends)

File /opt/homebrew/lib/python3.10/site-packages/matplotlib/axes/_base.py:74, in _axis_method_wrapper.__set_name__.<locals>.wrapper(self, *args, **kwargs)
     73 def wrapper(self, *args, **kwargs):
---> 74     return get_method(self)(*args, **kwargs)

File /opt/homebrew/lib/python3.10/site-packages/matplotlib/axis.py:1456, in Axis.get_ticklabels(self, minor, which)
   1454 if minor:
   1455     return self.get_minorticklabels()
-> 1456 return self.get_majorticklabels()

File /opt/homebrew/lib/python3.10/site-packages/matplotlib/axis.py:1413, in Axis.get_majorticklabels(self)
   1411 def get_majorticklabels(self):
   1412     """Return this Axis' major tick labels, as a list of `~.text.Text`."""
-> 1413     self._update_ticks()
   1414     ticks = self.get_major_ticks()
   1415     labels1 = [tick.label1 for tick in ticks if tick.label1.get_visible()]

File /opt/homebrew/lib/python3.10/site-packages/matplotlib/axis.py:1262, in Axis._update_ticks(self)
   1257 def _update_ticks(self):
   1258     """
   1259     Update ticks (position and labels) using the current data interval of
   1260     the axes.  Return the list of ticks that will be drawn.
   1261     """
-> 1262     major_locs = self.get_majorticklocs()
   1263     major_labels = self.major.formatter.format_ticks(major_locs)
   1264     major_ticks = self.get_major_ticks(len(major_locs))

File /opt/homebrew/lib/python3.10/site-packages/matplotlib/axis.py:1484, in Axis.get_majorticklocs(self)
   1482 def get_majorticklocs(self):
   1483     """Return this Axis' major tick locations in data coordinates."""
-> 1484     return self.major.locator()

File /opt/homebrew/lib/python3.10/site-packages/matplotlib/dates.py:1378, in AutoDateLocator.__call__(self)
   1375 def __call__(self):
   1376     # docstring inherited
   1377     dmin, dmax = self.viewlim_to_dt()
-> 1378     locator = self.get_locator(dmin, dmax)
   1379     return locator()

File /opt/homebrew/lib/python3.10/site-packages/backtrader-1.9.76.123-py3.10.egg/backtrader/plot/locator.py:226, in AutoDateLocator.get_locator(self, dmin, dmax)
    222     locator = MicrosecondLocator(interval, tz=self.tz)
    224 locator.set_axis(self.axis)
--> 226 locator.set_view_interval(*self.axis.get_view_interval())
    227 locator.set_data_interval(*self.axis.get_data_interval())
    228 return locator

AttributeError: 'RRuleLocator' object has no attribute 'set_view_interval'

I've tried looking into RRuleLocator but there's very little documentation about this online. Previously, I was running into this issue ImportError Cannot import name 'warnings' from 'matplotlib.dates and solved it by git cloning from this repo https://github.com/mementum/backtrader. Any thoughts?

I tried getting rid of these lines and then installing backtrader, but that results in a plot like this (all jumbled up):enter image description here

1 Answers1

3

I had the exact same problem and my solution was to call the set_view_interval() method directly on the axis object associated with the RRuleLocator.

Open your locator.py file. Then remove these two lines:

locator.set_view_interval(*self.axis.get_view_interval())
locator.set_data_interval(*self.axis.get_data_interval())

And replace it with:

self.axis.set_view_interval(*self.axis.get_view_interval())