Python 在matplotlib绘图中,我可以高亮显示特定的x值范围吗?

Python 在matplotlib绘图中,我可以高亮显示特定的x值范围吗?,python,statistics,matplotlib,Python,Statistics,Matplotlib,我正在为一个项目对历史股票数据进行可视化,我想突出显示下跌区域。例如,当股票大幅下跌时,我想用红色区域突出显示 我可以自动完成吗,还是必须画一个矩形或其他东西 查看(以及用于突出显示y轴区域的axhspan) 如果使用的是日期,则需要将最小和最大x值转换为matplotlib日期。对于datetime对象使用matplotlib.dates.date2num,对于各种字符串时间戳使用matplotlib.dates.datestr2num import matplotlib.pyplot as

我正在为一个项目对历史股票数据进行可视化,我想突出显示下跌区域。例如,当股票大幅下跌时,我想用红色区域突出显示

我可以自动完成吗,还是必须画一个矩形或其他东西

查看(以及用于突出显示y轴区域的axhspan)

如果使用的是日期,则需要将最小和最大x值转换为matplotlib日期。对于
datetime
对象使用
matplotlib.dates.date2num
,对于各种字符串时间戳使用
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()

这里有一个解决方案,用于绘制多个高光,其中每个高光的限制是通过使用与波峰和波谷对应的股票数据的指数来设置的

股票数据通常包含不连续的时间变量,其中不包括周末和节假日。在matplotlib或pandas中绘制它们将在周末和假日处理每日股价时沿x轴产生间隙。对于长日期范围和/或小数字(如本例中),这可能不明显,但如果放大,它将变得明显,这可能是您希望避免的事情

这就是为什么我在这里分享一个完整的示例,其特点是:

  • 一个真实的样本数据集,包括一个不连续的
    DatetimeIndex
    ,该数据集基于与导入的纽约证券交易所交易日历,以及看起来像真实事物的假股票数据
  • use_index=False
    创建的,它通过使用x轴的整数范围来消除周末和节假日的间隙。返回的
    ax
    对象的使用方式避免了导入matplotlib.pyplot的需要(除非需要
    plt.show
  • 通过使用scipy.signal函数自动检测整个日期范围内的提取,该函数返回使用
    axvspan
    绘制高光所需的索引。以更正确的方式计算缩编需要明确定义什么是缩编,并将导致更复杂的代码,这是我们的主题
  • 通过循环查看
    DatetimeIndex
    的时间戳创建的格式正确的记号,因为在这种情况下不能使用所有方便的记号定位器和格式化程序以及
    DatetimeIndex
    属性

创建样本数据集

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)

使用格式正确的记号打印绘图的高光

# 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);


为完整起见,值得注意的是,使用plotting函数可以获得完全相同的结果,尽管还需要几行代码:

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的交互界面,并希望在放大时具有动态刻度?则需要使用模块中的定位器和格式化程序。例如,您可以像本例中那样固定主刻度,并在放大时添加动态次刻度以显示一年中的几天或几周。您可以在结尾处找到一个如何执行此操作的示例。

您可以自动绘制一个矩形。。。您是否已经具有定义要高亮显示区域的开始/停止的x值?如果是这样的话,你可以画一个矩形补丁来突出显示这个部分(如果你需要一个例子,请告诉我!)我正在使用我的x轴的日期,但我很难让它工作。有什么建议吗?请参阅编辑。您需要将日期转换为matplotlib的内部日期格式(它只是一个浮点数,其中1的范围对应于1天)。请查看
matplotlib.dates
,了解处理此问题的各种函数。希望有帮助!这可以在3D绘图中完成吗?我想突出显示沿z轴的特定切片的XY平面。这在直接使用熊猫绘制时也有效。事实证明,熊猫在整个事情上是“聪明”的(至少在一年之内)。不用像
pd.to_datetime
那样设置日期,只需将其放在引号中就足够了(
'1999'
)。这可以对多个跨度进行设置,还是我必须单独编写每个跨度?
# 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);
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)