使用pandas绘制股票均线,这里以5日均线和20日均线为例。
1. 首先导入pandas模块。
pd.set_option('display.max_columns', 1000) pd.set_option('display.max_rows', 1000) pd.set_option('display.width', 1000) pd.set_option('display.max_colwidth',1000) from matplotlib import pyplot as plt
2. 读取数据。
df = pd.read_csv(r'C:\Users\飘逸\Desktop\600837.csv')
3. 计算5日和20日均线。
df[MA5] = df['Close'].rolling(5).mean() df[MA20] = df['Close'].rolling(20).mean()
4. 绘图。为了方便观察,这里将均线图和实际走势分开,实际走势使用的是收盘价。
plt.subplot2grid((2,1),(0,0)) plt.title("MA5 and MA20") plt.legend(("MA5", "MA20"), loc="lower left") plt.subplot2grid((2,1),(1,0)) plt.legend(("Close",), loc="lower left")
绘图结果如下。上图:均线,下图:实际走势。
|