当前位置: 首页 > 后端技术 > Python

基于Python构建自定义图形和指标(三)

时间:2023-03-26 13:46:30 Python

undefineddefbutton_release(event):globalcurrent_x,button_flagbutton_flag=Falseidx=int(event.xdata)diff=idx-current_x全局s_idx,e_idx,show_datas_idx-=diffe_idx-=diffife_idx>0:e_idx=-1s_idx=-90show_data=data[s_idx:e_idx]foraxinaxes_data.values():ax.cla()plt.clf()show_chart()current_x=-1放大缩小实现放大缩小其实就是鼠标滚轮事件处理。fig.canvas.mpl_connect('scroll_event',on_scroll)默认设置为显示90天的数据。放大和缩小时,每次都会在当前基础上变化10%,然后使用新的开始和结束日期获取数据,清理当前画布新数据后重新显示。defon_scroll(event):ax=event.inaxes如果ax为None:返回全局s_idx,e_idx,show_datadiff=int(data_size*0.1/2)ifevent.button=="down":s_idx+=diffe_idx-=diffelifevent.button=="up":s_idx-=diffe_idx+=diffife_idx>0:e_idx=-1show_data=data[s_idx:e_idx]axes_data.values():ax.cla()中的axplt.clf()show_chart()CrossCursorMatplotlib自带两种游标,一种显示在单子图,一种显示在多子图,但是显示多子图时,叉号会出现在所有的子图中,这里修改了motion_notify_event的事件处理函数,跟随MultiCursor的实现。使用dict结构存储竖线和横线,它们的key是subplot的名字,value是matplotlib中的line对象vlines={}hlines={}图表显示时,竖线显示在默认所有子图,仅在K线所在的子图中显示水平线。其中,垂直线的X轴坐标为当前显示数据大小的一半,水平线的Y轴坐标为最高价的最高值与最低值的差值当前显示数据中的最低价,得到显示区域Y轴上的距离,然后将最低价的最低值加上其值的一半即为水平线的Y轴坐标。undefinedundefinedax.draw_artist(line)line=hlines.get(key)iflineisnotNone:ax.draw_artist(line)fig.canvas.blit()最终效果完整代码见:https://github.com/just4alpha/GreedyAlpha