0

the following code is an example from bokeh documentation:


import numpy as np

from bokeh.layouts import column
from bokeh.models import ColumnDataSource, RangeTool
from bokeh.plotting import figure, show
from bokeh.sampledata.stocks import AAPL

dates = np.array(AAPL['date'], dtype=np.datetime64)
source = ColumnDataSource(data=dict(date=dates, close=AAPL['adj_close']))

p = figure(height=300, width=800, tools="xpan", toolbar_location=None,
           x_axis_type="datetime", x_axis_location="above",
           background_fill_color="#efefef", x_range=(dates[1500], dates[2500]))

p.line('date', 'close', source=source)
p.yaxis.axis_label = 'Price'

select = figure(title="Drag the middle and edges of the selection box to change the range above",
                height=130, width=800, y_range=p.y_range,
                x_axis_type="datetime", y_axis_type=None,
                tools="", toolbar_location=None, background_fill_color="#efefef")

range_tool = RangeTool(x_range=p.x_range)
range_tool.overlay.fill_color = "navy"
range_tool.overlay.fill_alpha = 0.2

select.line('date', 'close', source=source)
select.ygrid.grid_line_color = None
select.add_tools(range_tool)

show(column(p, select))

I am curious, is it possible to add a custom SaveTool that saves the whole figure instead of the currently displayed part of it? To be more precise, suppose the range of the domain is equal to (0,1000), but x_range in the figure options is set to (0, 200), then the default SaveTool will save the plot with x_range equal to (x+0, x+200), where x is the starting position of the currently displayed part of the figure. But I want the SaveTool to save the whole plot, i.e. range should be equal to (0,1000).

1
  • 1
    Not really. The toolbar SaveTool is purely browser-based and just dumps the raw pixel data from the HTML canvas that the plot is drawn on into a PNG image. There are Python APIs for saving PNGs from plot or layout objects directly, so you could potentially create a second version of the plot just for exporting. But this would require running things as a Bokeh Server appliation so that a save button could trigger actual Python callback code. Commented Dec 3, 2024 at 21:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.