券商研报股价趋势图绘制
首先先要导入各种包和数据,定好画图的时间区间:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import warnings
warnings.filterwarnings("ignore")
import tushare as ts
ts.set_token('587c7ae5acfd0f08717963e1b4d6eff0c2dc33c5954f15e4074dd122')
pro = ts.pro_api()
start_time = '20200101'
end_time = '20220501'
raw_data_path = r'' #这里填写文件路径
ts_code = '' # 这里填写股票代码
从tushare获取数据
因为用的是MacOS,不能从Wind导出数据,所以使用Tushare代替。
地址:https://tushare.pro/
注意需要注册,获得一个token。
下载沪深300股票数据
csi300 = pro.index_daily(ts_code = '000300.SH', start_date=start_time, end_date=end_time)
csi300.to_csv(raw_data_path + '沪深300股票数据.csv')
下载前复权数据
ts_code = '' #这里填写需要的股票代码
df = ts.pro_bar(ts_code=ts_code, adj='qfq', start_date=start_time, end_date=end_time)
下载结果如下:
画图部分
import matplotlib.pyplot as plt
import seaborn as sns
# plt.style.use('seaborn-darkgrid')
close_price = df[['trade_date', 'close']]
import datetime
close_price['trade_date'] = pd.to_datetime(close_price['trade_date'])
close_price.set_index("trade_date", inplace=True) # 加时间索引
close_price
股价频率分布直方图
sns.set_style('whitegrid') # 图要贴在报告中,所以改成白底的主题
color_zt = '#BE002E'
plt.figure(figsize=(24,15))
p = sns.distplot(close_price['close'], color=color_zt)
p.set_xlabel("Close_Price", fontsize = 30)
p.set_ylabel("Density", fontsize = 30)
plt.xticks(fontsize=50)
plt.yticks(fontsize=50)
股票累计收益与指数累计收益对比图
- 样例股票是2019年上市的,所以以计算2020年以来的股票累计收益为例,与沪深300进行比较。
处理数据:
df['whole'] = df['close']/36.9781 - 1
df['CSI300_whole'] = csi300['close']/4152.2408 - 1
accumulated = df[['whole', 'CSI300_whole']]
accumulated['close'] = df['close']
accumulated['pct_chg'] = df['pct_chg']
accumulated
数据长这样:
这样就得到了画图所需的数据。四列分别是股票的累计收益,沪深300的累计收益,
color_zt = '#BE002E' # zt红
color_zt1 = '#C0C0C0' # 灰色
color_zt2 = '#DC3400' # zt橙
color_zt3 = '#FFAA96' # 灰色
x = accumulated.index
y1 = accumulated.close.values # 股票收盘价
y2 = accumulated.whole.values # 股票累计收益
y3 = accumulated.CSI300_whole.values # CSI300累计收益
fig, ax1 = plt.subplots(figsize=(20, 4), dpi=300)
# plt.xticks(rotation=60) 如想旋转横轴标签可以加上这句
ax2 = ax1.twinx() # 复制一份坐标轴
# ===================== 这里只画了目标公司和沪深300的累计股票收益 ===================== #
# lns1 = ax2.plot(x, y1, color=color_zt, ms=10, label='Close_Price') # 股价
lns2 = ax1.plot(x, y2, color=color_zt, ms=10, label='Stock_Accumulated_Return') # 累计收益
lns3 = ax2.plot(x, y3, color=color_zt1, ms=10, label='CSI300_Return') # CSI300累计收益
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
ax1.grid(False)
ax2.grid(False)
plt.grid(axis="y")
近一年股票累计收益图
# 2021.5.1数据 股票价格是34.8458,指数是5123.489
accumulated['one_year_whole'] = df['close']/34.8458 -1
accumulated['one_year_csi300'] = csi300['close']/5123.489 -1
accumulated['excess'] = accumulated['one_year_whole'] - accumulated['one_year_csi300']
accumulated1 = accumulated.truncate(before='2021-05-01 16:00:00') # 这里去掉了2021.5.1以前的数据
accumulated1
增加后三列数据,分别是股票1年累计收益,沪深300一年累计收益,超额收益
x = accumulated1.index
y2 = accumulated1.excess.values
fig, ax1 = plt.subplots(figsize=(10.5, 4), dpi=300)
# plt.xticks(rotation=60)
lns = ax1.plot(x, y2, color=color_zt, ms=10, label='Stock_Excess_Return') # 累计收益
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
ax1.fill_between(x, y2, 0, where=(y1>0), facecolor=color_zt, alpha=0.2)
ax1.fill_between(x, y2, 0, where=(y1<0), facecolor=color_zt1, alpha=0.5)
# 填充色块:累计收益大于0填红色,小于0填灰色
ax1.grid(False)
ax2.grid(False)
plt.grid(axis="y")
画行业指数趋势图
这里以计算机行业为例,原数据来自Wind的申万行业指数和中信行业指数。数据长这样👇
x = industry.index
y1 = industry.SW_Computer.values
y2 = industry.ZX_Computer.values
fig, ax1 = plt.subplots(figsize=(20, 4), dpi=300)
ax2 = ax1.twinx()
lns1 = ax1.plot(x, y1, color=color_zt, ms=10, label='SW_Computer_Index')
lns1 = ax2.plot(x, y2, color=color_zt1, ms=10, label='ZX_Computer_Index')
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
ax1.grid(False)
ax2.grid(False)
plt.grid(axis="y")
plt.axhline(y=4418, color=color_zt3, ms=10) # 画平行线:4418是收盘日2022.5.1的申万行业指数数据