如何突出显示特定的 x 值范围
- 2025-03-05 09:17:00
- admin 原创
- 85
问题描述:
我正在为一个项目制作历史股票数据的可视化,我想突出显示下跌区域。例如,当股票经历大幅下跌时,我想用红色区域突出显示它。
我可以自动完成此操作吗,还是必须绘制一个矩形或类似的东西?
解决方案 1:
看一下axvspan
(以及用于突出显示 y 轴区域的 axhspan)。
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.axvspan(3, 6, color='red', alpha=0.5)
plt.show()
如果您使用日期,则需要将最小和最大 x 值转换为 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()
解决方案 2:
这是一个用于axvspan
绘制多个亮点的解决方案,其中每个亮点的限制是使用与峰值和谷值相对应的股票数据的指数来设置的。
股票数据通常包含不连续的时间变量,其中不包括周末和节假日。在处理每日股票价格时,在 matplotlib 或 pandas 中绘制它们会在周末和节假日的 x 轴上产生间隙。对于较长的日期范围和/或较小的数字(如本例中所示),这可能并不明显,但如果您放大,就会变得明显,这可能是您想要避免的事情。
这就是为什么我在这里分享一个完整的示例,其特点是:
一个真实的样本数据集,其中包括基于使用pandas_market_calendars
DatetimeIndex
导入的纽约证券交易所交易日历的不连续数据以及看起来像真实数据的虚假股票数据。用它创建的pandas 图通过
use_index=False
使用 x 轴的整数范围来消除周末和节假日的间隙。返回ax
对象的使用方式避免了导入 matplotlib.pyplot(除非您需要plt.show
)。使用 scipy.signal 函数自动检测整个日期范围内的亏损,
find_peaks
该函数返回绘制亮点所需的索引axvspan
。以更正确的方式计算亏损需要明确定义什么算作亏损,并会导致更复杂的代码,这是另一个问题的主题。通过循环遍历时间戳创建的正确格式的刻度
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
%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模块中的定位器和格式化程序。例如,您可以像本例中一样保持主刻度固定,并在放大时添加动态次要刻度以显示一年中的几天或几周。您可以在此答案的末尾找到如何执行此操作的示例。
扫码咨询,免费领取项目管理大礼包!