Вот минимальный пример использования SpanSelector
с графиком подсвечника в matplotlib.
import datetime
import matplotlib.pyplot as plt
from matplotlib.widgets import SpanSelector
from mpl_finance import candlestick_ohlc
import matplotlib.dates as mdates
ohlc = [[1502928000000, 4261.48, 4485.39, 4200.74, 4285.08, 795.150377],
[1503014400000, 4285.08, 4371.52, 3938.77, 4108.37, 1199.888264],
[1503100800000, 4108.37, 4184.69, 3850.0, 4139.98, 381.309763],
[1503187200000, 4120.98, 4211.08, 4032.62, 4086.29, 467.083022],
[1503273600000, 4069.13, 4119.62, 3911.79, 4016.0, 691.74306],
[1503360000000, 4016.0, 4104.82, 3400.0, 4040.0, 966.684858],
[1503446400000, 4040.0, 4265.8, 4013.89, 4114.01, 1001.136565],
[1503532800000, 4147.0, 4371.68, 4085.01, 4316.01, 787.418753]]
for row in ohlc:
row[0] = mdates.date2num(datetime.datetime.fromtimestamp(row[0]/1000.))
fig, (ax1, ax2) = plt.subplots(nrows=2, gridspec_kw=dict(height_ratios=[3,1]),
constrained_layout=True)
candlestick_ohlc(ax1,ohlc,width=0.2)
candlestick_ohlc(ax2,ohlc,width=0.2)
loc1 = mdates.AutoDateLocator()
ax1.xaxis.set_major_locator(loc1)
ax1.xaxis.set_major_formatter(mdates.AutoDateFormatter(loc1))
loc2 = mdates.AutoDateLocator()
ax2.xaxis.set_major_locator(loc2)
ax2.xaxis.set_major_formatter(mdates.AutoDateFormatter(loc2))
def onselect(xmin, xmax):
ax1.set_xlim(xmin, xmax)
fig.canvas.draw()
# Set useblit=True on most backends for enhanced performance.
span = SpanSelector(ax2, onselect, 'horizontal', useblit=True, span_stays=True,
rectprops=dict(alpha=0.5, facecolor='red'))
plt.setp(ax1.get_xticklabels()+ ax2.get_xticklabels(), rotation=-34, ha="left")
ax2.set_xlabel('Date')
plt.show()
