Vespa314/chan.py

画图性能提升

Closed this issue · 3 comments

draw_macd这个函数会把历史所有值都绘画一次,所以在回放时,会越来越卡
可否改进这个机制呢,只绘制近期的值

拉一下新代码再试试。

可以了,谢谢,然后我在此基础上优化了一下,能更快些
def draw_macd(self, meta: CChanPlotMeta, ax: Axes, x_limits, width=0.4):
macd_lst = [[klu.macd.DIF, klu.macd.DEA, klu.macd.macd] for klu in meta.klu_iter()]
assert macd_lst[0] is not None, "you can't draw macd until you delete macd_metric=False"
x_begin = x_limits[0]
macd_lst = np.array(macd_lst)
x_idx = np.arange(macd_lst.shape[0])[x_begin:]
dif_line = macd_lst[x_begin:, 0]
dea_line = macd_lst[x_begin:, 1]
macd_bar = macd_lst[x_begin:, 2]
y_min = np.min(macd_lst[x_begin:, :])
y_max = np.max(macd_lst[x_begin:, :])
ax.plot(x_idx, dif_line, "#FFA500")
ax.plot(x_idx, dea_line, "#0000ff")
colors = np.array(['red' if value > 0 else '#006400' for value in macd_bar], dtype=str)
_bar = ax.bar(x_idx, macd_bar, color=colors, width=width)
ax.set_ylim(y_min, y_max)

嗯,用numpy在量级大的时候是会有一定的优势的。