我找到的最佳解决方案是使用Bokeh。这里有一个相关的问题 -
Bokeh,仅在单个轴上缩放,相应地调整另一轴。一个答案链接了一个
gist,其中提供了一个设置自动Y缩放蜡烛图的示例。
不幸的是,这远非“开箱即用”的解决方案,因为它涉及编写自定义JavaScript回调。然而,这个解决方案仍然相当简单。我无法运行gist上的代码,主要是由于Pandas DataReader存在问题。这里是代码的更新版本,它使用Bokeh提供的示例数据(根据Bokeh
Candlesicks example),增加了与TradingView更多的相似之处,并解决了我发现的一些其他问题:
import pandas as pd
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.sampledata.stocks import MSFT
def candlestick_plot(df):
fig = figure(sizing_mode='stretch_both',
tools="xpan,xwheel_zoom,undo,redo,reset,crosshair,save",
active_drag='xpan',
active_scroll='xwheel_zoom',
x_axis_type='datetime')
inc = df.close > df.open
dec = ~inc
fig.segment(df.date[inc], df.high[inc], df.date[inc], df.low[inc], color="green")
fig.segment(df.date[dec], df.high[dec], df.date[dec], df.low[dec], color="red")
width_ms = 12*60*60*1000
fig.vbar(df.date[inc], width_ms, df.open[inc], df.close[inc], color="green")
fig.vbar(df.date[dec], width_ms, df.open[dec], df.close[dec], color="red")
source = ColumnDataSource({'date': df.date, 'high': df.high, 'low': df.low})
callback = CustomJS(args={'y_range': fig.y_range, 'source': source}, code='''
clearTimeout(window._autoscale_timeout);
var date = source.data.date,
low = source.data.low,
high = source.data.high,
start = cb_obj.start,
end = cb_obj.end,
min = Infinity,
max = -Infinity;
for (var i=0; i < date.length; ++i) {
if (start <= date[i] && date[i] <= end) {
max = Math.max(high[i], max);
min = Math.min(low[i], min);
}
}
var pad = (max - min) * .05;
window._autoscale_timeout = setTimeout(function() {
y_range.start = min - pad;
y_range.end = max + pad;
});
''')
fig.x_range.callback = callback
show(fig)
df = pd.DataFrame(MSFT)
df["date"] = pd.to_datetime(df["date"])
output_file("candlestick.html")
candlestick_plot(df)