You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib:在图表上添加趋势线(连接标记点)的实现方法

How to Replace Scatter Markers with Colored Connecting Lines in Matplotlib

Hey there! Let's figure out how to swap those scatter markers for colored connecting lines to visualize your trend and return signals more clearly. Here's a straightforward, step-by-step solution tailored to your data setup:

Step 1: Cleanly Filter Your Signal Data

First, let's extract the subsets of data for each signal type—this makes the code easier to read and maintain:

import pandas as pd
import matplotlib.pyplot as plt

# Ensure time columns are recognized as datetime (skip if already done)
df['time'] = pd.to_datetime(df['time'])
df_trend['time'] = pd.to_datetime(df_trend['time'])
df_return['time'] = pd.to_datetime(df_return['time'])

# Filter trend events
crossing_up = df_trend[df_trend.event == 'crossing up']
crossing_down = df_trend[df_trend.event == 'crossing down']

# Filter return signals
return_positive = df_return[df_return.return12 > 0]
return_negative = df_return[df_return.return12 < 0]

Step 2: Plot Price Curve + Connecting Lines

Instead of plt.scatter(), use plt.plot() to draw lines between your signal points. We'll assign unique colors and line styles to each signal type for clarity:

# First plot the original price curve
df.plot(y='price', x='time', label='Price', color='darkgray', linewidth=2)

# Plot crossing up signals: green solid line with downward triangle markers
plt.plot(crossing_up['time'], crossing_up['price'], color='forestgreen', linestyle='-', marker='v', label='Crossing Up')

# Plot crossing down signals: red dashed line with "1" markers
plt.plot(crossing_down['time'], crossing_down['price'], color='crimson', linestyle='--', marker='1', label='Crossing Down')

# Plot positive return signals: blue dotted line with "2" markers
plt.plot(return_positive['time'], return_positive['price'], color='royalblue', linestyle=':', marker='2', label='Return12 > 0')

# Plot negative return signals: orange dash-dot line with "3" markers
plt.plot(return_negative['time'], return_negative['price'], color='darkorange', linestyle='-.', marker='3', label='Return12 < 0')

# Add labels and legend for readability
plt.xlabel('Time')
plt.ylabel('Price')
plt.title('Price Trend with Signal Lines')
plt.legend()

# Adjust x-axis to fit your time range (optional but helpful)
plt.xlim(pd.to_datetime('2019-01-01 01:00:00.000Z'), pd.to_datetime('2019-01-15 23:59:59.999Z'))

plt.show()

Key Notes:

  • Line Style Customization: Use linestyle to tweak line appearance:
    • - = solid line
    • -- = dashed line
    • : = dotted line
    • -. = dash-dot line
  • Markers (Optional): Keep the marker parameter if you want to highlight individual signal points alongside the connecting lines. Remove it if you only want the lines.
  • Empty Data Handling: If any signal subset has no rows (e.g., no "crossing up" events), plt.plot() will skip it silently without throwing errors.

内容的提问来源于stack exchange,提问作者Viktor.w

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:06:55