# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt #from matplotlib import pyplot as plt
from matplotlib import ticker
import sqlite3
import pandas as pd
import os
st_list=input("請輸入您要分析財(cái)報(bào)的股票代碼(以逗號分開,如6011111,002241,603986):")
st_list=st_list.split(",")
for st in st_list:
# 數(shù)據(jù)庫文件
print(st)
db = 'stockfin.db'
# 連接數(shù)據(jù)庫,如果沒有則創(chuàng)建數(shù)據(jù)庫
conn = sqlite3.connect(db)
bs_sql='SELECT * from bs where 代碼="{}" and "年報(bào)日期" like "%2%" order by "年報(bào)日期";'.format(st)
bs=pd.read_sql_query(bs_sql, conn)
print(bs)
bs["歸屬母公司所有者權(quán)益(或股東權(quán)益)"]=bs["歸屬母公司所有者權(quán)益(或股東權(quán)益)"].apply(pd.to_numeric)
bs["歸屬母公司所有者權(quán)益均值"]=(bs["歸屬母公司所有者權(quán)益(或股東權(quán)益)"]+bs["歸屬母公司所有者權(quán)益(或股東權(quán)益)"].shift(4).fillna(0))/2 #期初+期末/2
st_name=bs["簡稱"][0]
print(st_name)
pl_sql='SELECT * from pl_yapcc where 代碼="{}" and "年報(bào)日期" like "%2%" order by "年報(bào)日期";'.format(st)
pl=pd.read_sql_query(pl_sql, conn)
print(pl)
#ROE=凈利潤 /凈資產(chǎn)=凈利潤 /(資產(chǎn)-負(fù)債)
BSPL=pd.merge(bs,pl, on="年報(bào)日期",how="inner") #取日期的交集
roe=pd.DataFrame(BSPL,columns=["歸屬母公司所有者權(quán)益均值", "歸屬于母公司所有者的凈利潤","年報(bào)日期"])
#roe[["歸屬母公司所有者權(quán)益(或股東權(quán)益)", "歸屬于母公司所有者的凈利潤"]]=roe[["歸屬母公司所有者權(quán)益(或股東權(quán)益)", "歸屬于母公司所有者的凈利潤"]].astype(float)
#roe[["歸屬母公司所有者權(quán)益(或股東權(quán)益)", "歸屬于母公司所有者的凈利潤"]]=roe[["歸屬母公司所有者權(quán)益(或股東權(quán)益)", "歸屬于母公司所有者的凈利潤"]].apply(pd.to_numeric)
print(roe)
#print(roe["歸屬母公司所有者權(quán)益(或股東權(quán)益)"])
#print(roe["歸屬母公司所有者權(quán)益(或股東權(quán)益)"].shift(4).fillna(0))
#average=(roe["歸屬母公司所有者權(quán)益(或股東權(quán)益)"]+roe["歸屬母公司所有者權(quán)益(或股東權(quán)益)"].shift(4).fillna(0))/2 #期初+期末/2
#print(average)
roe["凈資產(chǎn)收益率"]=roe["歸屬于母公司所有者的凈利潤"]/roe["歸屬母公司所有者權(quán)益均值"]
#roe["凈資產(chǎn)收益率"]=roe["凈資產(chǎn)收益率"].apply(lambda x: format(x, '.2%')) #Series.apply()讓序列的值依次在lambda函數(shù)中執(zhí)行;
print(roe)
line_lable=st+"_"+st_name
plt.cla()
plt.figure()
fig =plt.figure(figsize=(12,5)) #Figure:指整個(gè)圖形(可以通過plt.figure()設(shè)置畫布的大小和分辨率等)
## 可用MarkerEdgeColor或mec設(shè)置標(biāo)記邊緣顏色;MarkerFaceColor或mfc設(shè)置標(biāo)記填充顏色;MarkerSize設(shè)置標(biāo)記大小
x=roe["年報(bào)日期"]
y=roe["凈資產(chǎn)收益率"]
plt.plot(x, y, marker='o', mec='r', mfc='w',label=line_lable, linewidth=4)
print(type(plt.gca()))
plt.gca().yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1)) #百分比顯示
# zip joins x and y coordinates in pairs
for a,b in zip(x,y):
label = "{:.2f}".format(b) #b代表軸數(shù)據(jù)
plt.annotate(label, # this is the texts
(a,b), # this is the point to label
textcoords="offset points", # how to position the text
xytext=(0,10), # distance from text to points (x,y)
ha='center') # horizontal alignment can be left, right or center
# 設(shè)置圖表標(biāo)題,并給坐標(biāo)軸添加標(biāo)簽
title=st+"_"+st_name+"_"+"歸屬母公司凈資產(chǎn)收益率(ROE)趨勢"
plt.title(title, fontsize=20)
#顯示圖形的圖例
plt.legend()
plt.xlabel("報(bào)告日期", fontsize=12)
plt.ylabel("凈資產(chǎn)收益率", fontsize=12)
f_name=title+".jpg"
csv_name=title+".csv"
# 獲取今日日期,并轉(zhuǎn)換為字符串的形式。以此日期命名建立文件路徑
data = st+"_"+st_name+ "http://"
# 判斷是否存在此文件夾
folder = os.path.exists(data)
# 如果不存在就新建該文件夾
if not folder:
os.makedirs(data)
save_file = os.path.join(data, f_name)
print(save_file)
roe.to_csv(os.path.join(data, csv_name),encoding="utf_8_sig")
plt.savefig(save_file)
#plt.show()
plt.clf()
conn.close()
2020-08-19
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。