代码拉取完成,页面将自动刷新
#!/usr/bin/env python
# coding: utf-8
# In[32]:
# 更多代码,请关注公众号:Crossin的编程教室
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tushare as ts
from datetime import *
#设置显示中文
plt.rcParams['font.sans-serif'] = 'SimHei' # windows可设为SimHei,mac可设为Hiragino Sans GB
ts.set_token('你的tushare token')
pro = ts.pro_api()
ts_code = '000001.SZ'
index_df = pro.daily(ts_code=ts_code, start_date='20130101', end_date='20201203')
# index_df = pro.index_dailybasic(ts_code = "000001.SH",start_date = "20130101",
# end_date = "20200311",fields="trade_date,pe_ttm")
index_df.sort_values(by="trade_date",inplace=True)
x = pd.to_datetime(index_df["trade_date"]).values
y = index_df["close"].values
# y = index_df["pe_ttm"].values
index_df
# print(x,y)
# In[33]:
fig,ax = plt.subplots(figsize=(12,8))
ax.plot(x,y)
# In[34]:
max = np.max(y)
pe_80 = np.percentile(y,80)
pe_20 = np.percentile(y,20)
min = np.min(y)
now = y[-1] #为了后面标注最新收盘价用
# In[35]:
fig,ax = plt.subplots(figsize=(12,8))
ax.plot(x,y,linewidth=3)
ax.fill_between(x,min-1,pe_20,facecolor = "#00ff80",alpha=0.2)
ax.fill_between(x,pe_20,pe_80,facecolor = "#ffff4d",alpha=0.2)
ax.fill_between(x,pe_80,max+1,facecolor = "#ff69b4",alpha=0.2)
# In[36]:
fig,ax = plt.subplots(figsize=(12,8))
ax.plot(x,y,linewidth=3)
ax.fill_between(x,min-1,pe_20,facecolor = "#00ff80",alpha=0.2)
ax.fill_between(x,pe_20,pe_80,facecolor = "#ffff4d",alpha=0.2)
ax.fill_between(x,pe_80,max+1,facecolor = "#ff69b4",alpha=0.2)
ax.tick_params(axis='both', which='both',length=0)
ax.tick_params(axis='both', which='major', labelsize=16)
ax.margins(0.01,0)
ax.text(0.75,0.9,"最新收盘价:{}".format(now),transform=ax.transAxes,fontdict={'size':18})
plt.xticks(rotation=45)
plt.box(False)
plt.title("{} 价格分布区间".format(ts_code), fontdict={'size':24})
# In[37]:
def pe_plot(ts_code = "",name="",period=5):
#准备数据
now = datetime.now()
end_date = str(now.date()).replace("-","")
start_date = str(now.year-period)+end_date[4:]
index_df = pro.daily(ts_code=ts_code, start_date='20130101', end_date='20201203')
index_df.sort_values(by="trade_date",inplace=True)
x = pd.to_datetime(index_df["trade_date"]).values
y = index_df["close"].values
#划定分割范围
max = np.max(y)
pe_80 = np.percentile(y,80)
pe_20 = np.percentile(y,20)
min = np.min(y)
now = y[-1]
#绘图
fig,ax = plt.subplots(figsize=(12,8))
ax.plot(x,y,linewidth=3)
ax.fill_between(x,min-1,pe_20,facecolor = "#00ff80",alpha=0.2)
ax.fill_between(x,pe_20,pe_80,facecolor = "#ffff4d",alpha=0.2)
ax.fill_between(x,pe_80,max+1,facecolor = "#ff69b4",alpha=0.2)
ax.tick_params(axis='both', which='both',length=0)
ax.tick_params(axis='both', which='major', labelsize=16)
ax.margins(0.01,0)
ax.text(0.75,0.9,"最新收盘价:{}".format(now),
transform=ax.transAxes,fontdict={'size':18})
plt.xticks(rotation=45)
plt.box(False)
plt.title("{} 价格分布区间".format(ts_code),fontdict={'size':24})
plt.show()
# In[38]:
pe_plot(ts_code="600519.SH",period=5)
# In[ ]:
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。