Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Set chart height to full browser window height or at least to a set value #1199

Answered by kernc
AlexStox asked this question in Q&A
Discussion options

Hi guys, is there a way to override 'plot' method to make the scalable height of the chart or at least to set some height?
For instance, right now it plots the chart only on a half of the page height in the browser, so I'd like to make it bigger in height.
I tried a few approaches, but no luck, e.g. this one:

from backtesting import Backtest, Strategy
from backtesting.test import SMA, GOOG # Example data
from bokeh.layouts import gridplot
from bokeh.plotting import show
from bokeh.models import DatetimeTickFormatter
class CustomBacktest(Backtest):
 def plot(self, **kwargs):
 # Call the original plot method
 fig = super().plot(**kwargs)
 # Apply full-height adjustments to all figures
 for f in fig.select(dict(type="Figure")):
 f.sizing_mode = "stretch_both" # Adjust each figure for full resizing
 f.plot_height = 800 # Ensure height is large enough to fill the screen
 if hasattr(f.xaxis[0], "formatter"):
 f.xaxis[0].formatter = DatetimeTickFormatter(days="%d %b", months="%m/%Y")
 # Apply sizing_mode to the overall layout
 fig.sizing_mode = "stretch_both"
 # Show the adjusted figure
 show(fig, browser=None if kwargs.get("open_browser", True) else 'none')
 return fig
# Define a simple strategy
class SmaCross(Strategy):
 def init(self):
 self.sma1 = self.I(SMA, self.data.Close, 10)
 self.sma2 = self.I(SMA, self.data.Close, 20)
 def next(self):
 if self.sma1[-1] > self.sma2[-1]:
 self.buy()
 elif self.sma1[-1] < self.sma2[-1]:
 self.sell()
# Run the backtest with the customized plot
bt = CustomBacktest(GOOG, SmaCross, cash=10_000, commission=0.002)
stats = bt.run()
bt.plot()
You must be logged in to vote

I've been playing around with this for a while, because ideally the plot would be fullscreen.
This works for me, but the result (the panes not retaining designated aspect) is an issue I didn't find how to overcome.

bt = Backtest(...)
fig = bt.plot(open_browser=False)
fig.sizing_mode = 'stretch_both'
from bokeh.io import show
show(fig)

If anybody finds a way to make them look pretty, please open a PR.

Replies: 3 comments 1 reply

Comment options

Referring to the documentation of Backtesting.py, it states that 'The height is currently non-adjustable.' Therefore, you likely won't be able to achieve your desired outcome using the built-in bt.plot(). Alternatively, you might consider exporting the necessary data to Excel to create the chart.

You must be logged in to vote
1 reply
Comment options

Referring to the documentation of Backtesting.py, it states that 'The height is currently non-adjustable.' Therefore, you likely won't be able to achieve your desired outcome using the built-in bt.plot(). Alternatively, you might consider exporting the necessary data to Excel to create the chart.

Well, thank you, I saw and read this in the documentation before posting the question here. But still think there is a way to override the library method.

I tried a few approaches and they didn't work.
So, maybe someone knows why this is impossible (to change the chart height).

Comment options

I've been playing around with this for a while, because ideally the plot would be fullscreen.
This works for me, but the result (the panes not retaining designated aspect) is an issue I didn't find how to overcome.

bt = Backtest(...)
fig = bt.plot(open_browser=False)
fig.sizing_mode = 'stretch_both'
from bokeh.io import show
show(fig)

If anybody finds a way to make them look pretty, please open a PR.

You must be logged in to vote
0 replies
Answer selected by kernc
Comment options

I've attempted to solve this by making the individual chart components flexible. PR: #1302

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /