如何突出特定的x值范围

102 投票
2 回答
86204 浏览
提问于 2025-04-17 07:03

我正在做一个项目,想要展示历史股票数据,并且想要突出显示股票下跌的区域。比如,当股票出现大幅下跌时,我想用红色区域来标记出来。

我可以自动做到这一点吗?还是说我需要手动画一个矩形之类的东西?

2 个回答

16

这里有一个解决方案,使用了 axvspan 来绘制多个高亮区域,每个高亮区域的边界是通过股票数据中对应于峰值和谷值的索引来设置的。

股票数据通常包含不连续的时间变量,因为周末和节假日的数据是缺失的。在使用 matplotlib 或 pandas 绘制这些数据时,处理每日股票价格时,x轴上会因为周末和节假日而出现空白。这在日期范围很长或者图形很小(像这个例子)时可能不太明显,但如果你放大查看,就会发现这个问题,可能你会想要避免这种情况。

这就是我在这里分享一个完整示例的原因,示例包含:

  • 一个真实的样本数据集,包含基于纽约证券交易所交易日历的不连续 DatetimeIndex,这个日历是通过 pandas_market_calendars 导入的,还有一些看起来像真实股票数据的虚假数据。
  • 一个使用 use_index=False 创建的 pandas 绘图,通过使用整数范围作为 x 轴,消除了周末和节假日的空白。返回的 ax 对象的使用方式避免了需要导入 matplotlib.pyplot(除非你需要 plt.show)。
  • 通过使用 scipy.signal 的 find_peaks 函数自动检测整个日期范围内的回撤,这个函数返回绘制高亮区域所需的索引。更准确地计算回撤需要明确什么算作回撤,这会导致代码变得更复杂,这是 另一个问题
  • 通过遍历 DatetimeIndex 的时间戳来创建格式正确的刻度,因为所有方便的 matplotlib.dates 刻度定位器和格式化器,以及 DatetimeIndex 的一些属性(比如 .is_month_start)在这种情况下都无法使用。

创建样本数据集

import numpy as np                        # v 1.19.2
import pandas as pd                       # v 1.1.3
import pandas_market_calendars as mcal    # v 1.6.1
from scipy.signal import find_peaks       # v 1.5.2

# Create datetime index with a 'trading day end' frequency based on the New York Stock
# Exchange trading hours (end date is inclusive)
nyse = mcal.get_calendar('NYSE')
nyse_schedule = nyse.schedule(start_date='2019-10-01', end_date='2021-02-01')
nyse_dti = mcal.date_range(nyse_schedule, frequency='1D').tz_convert(nyse.tz.zone)

# Create sample of random data for daily stock closing price
rng = np.random.default_rng(seed=1234)  # random number generator
price = 100 + rng.normal(size=nyse_dti.size).cumsum()
df = pd.DataFrame(data=dict(price=price), index=nyse_dti)
df.head()

#                                    price
#   2019-10-01 16:00:00-04:00    98.396163
#   2019-10-02 16:00:00-04:00    98.460263
#   2019-10-03 16:00:00-04:00    99.201154
#   2019-10-04 16:00:00-04:00    99.353774
#   2019-10-07 16:00:00-04:00   100.217517

绘制回撤的高亮区域,并且刻度格式正确

# Plot stock price
ax = df['price'].plot(figsize=(10, 5), use_index=False, ylabel='Price')
ax.set_xlim(0, df.index.size-1)
ax.grid(axis='x', alpha=0.3)

# Highlight drawdowns using the indices of stock peaks and troughs: find peaks and 
# troughs based on signal analysis rather than an algorithm for drawdowns to keep
# example simple. Width and prominence have been handpicked for this example to work.
peaks, _ = find_peaks(df['price'], width=7, prominence=4)
troughs, _ = find_peaks(-df['price'], width=7, prominence=4)
for peak, trough in zip(peaks, troughs):
    ax.axvspan(peak, trough, facecolor='red', alpha=.2)

# Create and format monthly ticks
ticks = [idx for idx, timestamp in enumerate(df.index)
         if (timestamp.month != df.index[idx-1].month) | (idx == 0)]
ax.set_xticks(ticks)
labels = [tick.strftime('%b\n%Y') if df.index[ticks[idx]].year
          != df.index[ticks[idx-1]].year else tick.strftime('%b')
          for idx, tick in enumerate(df.index[ticks])]
ax.set_xticklabels(labels)
ax.figure.autofmt_xdate(rotation=0, ha='center')

ax.set_title('Drawdowns are highlighted in red', pad=15, size=14);

highlights_drawdowns


为了完整起见,值得注意的是,你也可以使用 fill_between 绘图函数来实现完全相同的效果,不过这需要多写几行代码:

ax.set_ylim(*ax.get_ylim())  # remove top and bottom gaps with plot frame
drawdowns = np.repeat(False, df['price'].size)
for peak, trough in zip(peaks, troughs):
    drawdowns[np.arange(peak, trough+1)] = True
ax.fill_between(np.arange(df.index.size), *ax.get_ylim(), where=drawdowns,
                facecolor='red', alpha=.2)

你在使用 matplotlib 的交互界面,并且希望在放大时有动态刻度吗? 那么你需要使用来自 matplotlib.ticker 模块的定位器和格式化器。例如,你可以像这个例子一样保持主要刻度固定,并在放大时添加动态次要刻度来显示年份中的天数或周数。你可以在 这个答案 的最后找到如何做到这一点的示例。

172

你可以看看 axvspan 这个功能(还有 axhspan 用来高亮显示y轴的某个区域)。

import matplotlib.pyplot as plt

plt.plot(range(10))
plt.axvspan(3, 6, color='red', alpha=0.5)
plt.show()

enter image description here

如果你在使用日期数据,那么你需要把你的最小值和最大值转换成 matplotlib 可以识别的日期格式。可以用 matplotlib.dates.date2num 来处理 datetime 对象,或者用 matplotlib.dates.datestr2num 来处理各种字符串格式的时间戳。

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt

t = mdates.drange(dt.datetime(2011, 10, 15), dt.datetime(2011, 11, 27),
                  dt.timedelta(hours=2))
y = np.sin(t)

fig, ax = plt.subplots()
ax.plot_date(t, y, 'b-')
ax.axvspan(*mdates.datestr2num(['10/27/2011', '11/2/2011']), color='red', alpha=0.5)
fig.autofmt_xdate()
plt.show()

enter image description here

撰写回答