如何在带有OHLC图表的Matplotlib上添加滑块选择器?

时间:2019-03-29 20:35:42

标签: python matplotlib

我希望在matplotlib中使用财务数据来做到这一点,但我不知道这怎么可能有人能帮助我吗?

所以我现在有:

dataframe = pd.DataFrame(HistoricalRequestData[0]['priceData'])
dataframe = dataframe[dataframe.columns[0:5]]

dataframe.columns = ['O', 'date', 'H', 'L', 'C']


print('*** Program Started ***')

dataframe['date'] = pd.to_datetime(dataframe['date'])
dataframe["date"] = dataframe["date"].apply(mdates.date2num)

ohlc= dataframe[['date', 'O', 'H', 'L','C']].copy()

f1, ax = plt.subplots(figsize = (10,5))

candlestick_ohlc(ax, ohlc.values, width=.0015, colorup='green', colordown='red')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))             

plt.show()

print('*** Program ended ***')

这将创建:

Chart it makes when i run the code above

但是我想在其下制作一个滑块,但是我不知道如何……它看起来应该像这样:

https://cdn.discordapp.com/attachments/455074133211086869/560918626211856412/2019-03-28_21h03_51.png

如果有人可以帮助我,那太好了。

Jan

编辑:

因此,由于我使用的是OHLC,所以我不确定将oneselect中的x和y放在什么位置...

fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)

def onselect(xmin, xmax):
    indmin, indmax = np.searchsorted(x, (xmin, xmax))
    indmax = min(len(x) - 1, indmax)

    thisx = x[indmin:indmax]
    thisy = y[indmin:indmax]
    line2.set_data(thisx, thisy)
    ax1.set_xlim(thisx[0], thisx[-1])
    ax1.set_ylim(thisy.min(), thisy.max())
    fig.canvas.draw()

# Set useblit=True on most backends for enhanced performance.
span = SpanSelector(ax1, onselect, 'horizontal', useblit=True,
                    rectprops=dict(alpha=0.5, facecolor='red'))

candlestick_ohlc(ax1, ohlc.values, width=.0015, colorup='green', colordown='red')

1 个答案:

答案 0 :(得分:0)

这是有关如何在matplotlib中将SpanSelector与烛台图一起使用的最小示例。

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()

enter image description here