简体   繁体   中英

Python, connect two data points at the beginning and end of a series

I would really like to know how I can plot the mean of two points on a chart with Python. I have stock data with 200 data points, and I want to take the mean of the first 20 points and the mean of the last 20 points, and then plot a line connecting those two points. I do not want any of the data points between those two to be taken into account.

my entire program is as such

stock = web.get_data_yahoo('clh.ax', '10/01/2017', interval='d')

stock['ema']=stock['Adj Close'].ewm(span=100,min_periods=0).mean()

stock['std']=stock['Adj Close'].rolling(window = 20,min_periods=0).std()

# bollinger bands
stock['close 20 day mean'] = stock['Close'].rolling(20,min_periods=0).mean()
# upper band
stock['upper'] = stock['close 20 day mean'] + 2 * (stock['Close'].rolling(20, min_periods=0).std())
# lower band
stock['lower'] = stock['close 20 day mean'] - 2 * (stock['Close'].rolling(20, min_periods=0).std())
# end bollinger bands

fig,axes = plt.subplots(nrows=3, ncols =1, figsize=(10,6))

axes[0].plot(stock['Close'], color='red')
axes[0].plot(stock['ema'], color='blue')
axes[0].plot(stock['close 20 day mean'], color='black')
axes[0].plot(stock['upper'], color='black')
axes[0].plot(stock['lower'], color='black')

axes[1].plot(stock['Volume'],color='purple')
axes[2].plot(stock['std'], color='black')

Not 100% sure i understood the question right, but:
a) Take the mean of the first 20 points,
b) Take the mean of the last 20 points.
c) Plots a line between those two values.

%matplotlib inline
import matplotlib.pyplot as plt

plt.plot([stock["Close"].iloc[:20].mean(), stock["Close"].iloc[-20:].mean()])

This plots: 在此处输入图片说明

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