简体   繁体   中英

Adding horizontal lines to timeseries plot with python + matplotlib

I am plotting data from a time series and I would like to add multiple horizontal lines, indicating value thresholds.

How do you recommend going about it?

date_today = datetime.now()
date_rng = pd.date_range(start='1/1/1989', end='1/2/1989', freq='H')[:-1]
np.random.seed(seed=100)
data1 = np.random.randint(1, high=100, size=len(date_rng))
data2 = np.random.randint(1, high=100, size=len(date_rng))
df = pd.DataFrame({'datetime': date_rng, 'col1': data1, 'col2': data2})
df = df.set_index('datetime')
df.plot(figsize=(10, 5));

滴滴

You can use the axhline like this:

plt.axhline(xmin,xmax,y)

https://matplotlib.org/3.3.1/api/_as_gen/matplotlib.pyplot.axhline.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM