-1

我正在尝试在另一条线上绘制 ZigZag 线。我的基准线(价格)有最高点和最低点。我正在尝试用一条线连接顶部和底部点。这是一个例子:

在此处输入图像描述

这是我的数据集:

在此处输入图像描述

这就是我能走多远:

在此处输入图像描述

任何帮助表示赞赏。

编辑1:

这是我不工作的代码:

trend_index = target_out.columns.get_loc("trend_shifted")
close_index = target_out.columns.get_loc("price_close")

for i in range(1, len(target_out)):
       if target_out.iloc[i, trend_index] == target_out.iloc[i-1, trend_index] and target_out.iloc[i-1, trend_index] is not None:
            target_out.iloc[i, trend_index] = np.nan
            target_out.iloc[i-1, trend_index] = target_out.iloc[i-1, close_index]

# multiple line plot
plt.plot( 'ind', 'price_close', data=target_out, marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
plt.plot( 'ind', 'trend_shifted', data=target_out, marker='', color='olive', linewidth=2)
plt.legend()

trend_shifted列有oneszeros。连续 1 和 0 的第一个元素实际上是之字形的顶部和底部点。其余的点并不重要。确定了最高点和最低点后,我需要画一条线,但由于价格和趋势的值相对不同,图表不平衡(我的意思是,价格就像 0.00001 但趋势是 0 和 1)

编辑2:

@rgk 的代码有效。这是输出:

在此处输入图像描述

4

1 回答 1

1

尽管由于没有给出代码示例,这对您来说可能并不完美,但我认为使用掩码数组并将其与索引分开绘制将完成您正在寻找的内容:

df = pd.DataFrame({'price_close': np.random.uniform(low=1.186e-05, high=1.255e-05, size=9),
                   'trend_shifted': [bool(random.getrandbits(1)) for x in range(1, 10)]})

df['trend_plot'] = [np.nan] + [df.price_close[i] if df.trend_shifted[i] != df.trend_shifted[i-1] else np.nan for i in range(1, len(df))]
mask = np.isfinite(df.trend_plot)

plt.plot(df.index, df.price_close, linestyle='-', marker='o')
plt.plot(df.index[mask], df.trend_plot[mask], linestyle='-', marker='o')
plt.show()

示例图和数据

于 2019-03-06T16:05:14.333 回答