这里有一个解决方案,它使用axvspan 绘制多个高光,其中每个高光的限制是通过使用与波峰和低谷对应的股票数据的索引来设置的。
股票数据通常包含不连续的时间变量,其中不包括周末和节假日。在处理每日股票价格时,在 matplotlib 或 pandas 中绘制它们会在周末和节假日沿 x 轴产生间隙。这对于较长的日期范围和/或较小的数字(如本例中)可能不明显,但如果您放大它会变得很明显,并且可能是您想要避免的事情。
这就是我在这里分享一个完整示例的原因:
- 一个真实的样本数据集,其中包括一个不连续的
DatetimeIndex,它基于使用pandas_market_calendars 导入的纽约证券交易所交易日历以及看起来像真实事物的虚假股票数据。
- 使用
use_index=False 创建的pandas plot 通过使用x 轴的整数范围来消除周末和节假日的间隙。返回的 ax 对象的使用方式避免了导入 matplotlib.pyplot(除非您需要 plt.show)。
- 使用 scipy.signal
find_peaks 函数自动检测整个日期范围内的回撤,该函数返回用axvspan 绘制高光所需的索引。以更正确的方式计算回撤需要明确定义什么是回撤,并且会导致更复杂的代码,这是 another question 的主题。
- 在这种情况下,不能使用通过循环遍历
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);
为了完整起见,值得注意的是,您可以使用 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 模块的定位器和格式化程序。例如,您可以像本示例中那样保持主要刻度固定,并添加动态次要刻度以在放大时显示一年中的几天或几周。您可以在this answer 末尾找到如何执行此操作的示例。