tushare做一点财务分析

先贴三张图,

上图




代码

图一

import tushare as ts
import matplotlib.pyplot as plt

import numpy as np


stock_basics = ts.get_stock_basics()
print'please input the stock code:'      #输入代码时,加引号。  ‘002705’
scode = input()
stock__basics = stock_basics[stock_basics.index == scode]

name = stock__basics.name[stock__basics.index == scode]
industry = str(stock__basics.industry[stock__basics.index == scode])
area = stock__basics.area[stock__basics.index == scode]
pe = float(stock__basics.pe[stock__basics.index == scode])
pb = float(stock__basics.pb[stock__basics.index == scode])

liquidasset = float(stock__basics.liquidAssets[stock__basics.index == scode])
fixedasset = float(stock__basics.fixedAssets[stock__basics.index == scode])
totalasset = float(stock__basics.totalAssets[stock__basics.index == scode])

outstanding = float(stock__basics.outstanding[stock__basics.index == scode])
totals = float(stock__basics.totals[stock__basics.index == scode])

esp = float(stock__basics.esp[stock__basics.index == scode])
bvps = float(stock__basics.bvps[stock__basics.index == scode])
reservedpershare = float(stock__basics.reservedPerShare[stock__basics.index == scode])
perundp = float(stock__basics.perundp[stock__basics.index == scode])

plt.figure(figsize=(12,6))

#资产状况饼图
plt.subplot(131)
labels_asset = 'liquid asset','fixed asset'
sizes_asset = liquidasset, fixedasset
colors_asset = 'yellowgreen', 'gold'
plt.axis('equal')
plt.pie(sizes_asset, explode=None,labels=labels_asset,colors=colors_asset,autopct='%1.1f%%',shadow=True,startangle=50)
plt.title('Asset situation')

col_labels = ['liquid(wanyuan)', 'fixed(wanyuan)','outstanding(yigu)', 'totals(yigu)']
row_labels = ['%s'%scode]
table_vals = [[liquidasset,fixedasset,outstanding,totals]]
my_table = plt.table(cellText=table_vals,colWidths=[0.4]*4,\
                     rowLabels=row_labels,colLabels=col_labels
                     )

#股本状况饼图
plt.subplot(132)
labels_share = 'outstanding', 'totals'
sizes_share = outstanding, totals
colors_share = 'yellowgreen', 'gold'
plt.axis('equal')
plt.pie(sizes_share, explode=None, labels=labels_share,colors=colors_share,autopct='%1.1f%%',shadow=True,startangle=50)
plt.title('Equity situation')

#每股净资,每股未分配利润
plt.subplot(133)
ind = np.arange(2)
numlist = [bvps,perundp]
plt.bar(ind, numlist)
plt.xlabel('DATA PS')
plt.ylabel('YUAN')
plt.title('NA&UNDNI PS')
plt.xticks(ind, ('NAPS', 'UNDNIPS'))
for a, b in zip(ind, numlist):

    plt.text(a, b+0.05, '%.2f'%b, ha='center', va='bottom', fontsize=7)


plt.show()

图二

import tushare as ts
import matplotlib.pyplot as plt
import numpy as np

print 'please input the year'

year1 = input()
year2 = input()
year3 = input()
year4 = input()
year5 = input()
timelist = []
timelist.append(year1)
timelist.append(year2)
timelist.append(year3)
timelist.append(year4)
timelist.append(year5)

print 'please input the stock code'
scode = input()

roe = []
net_profit_ratio = []
gross_profit_rate = []
net_profits = []
eps = []
business_income = []
bips = []

for i in timelist:
    
    profit_data = ts.get_profit_data(i,4)
    profit_data.index = profit_data.code
    data = profit_data[profit_data.index == scode]
    
    roe.append(float(data.roe))
    net_profit_ratio.append(float(data.net_profit_ratio))
    gross_profit_rate.append(float(data.gross_profit_rate))
    net_profits.append(float(data.net_profits))
    eps.append(float(data.eps))
    business_income.append(float(data.business_income))
    bips.append(float(data.bips))

plt.figure(figsize=(12,6))

#营业收入柱状图
plt.subplot(231)
ind = np.arange(5)
plt.bar(ind, business_income, color='yellowgreen')
plt.title('Business Income(BaiWan Yuan)')
plt.xticks(ind, (year1, year2, year3, year4, year5))
for a, b in zip(ind, business_income):
    plt.text(a, b+0.05, '%.2f'%b, ha='center', va='bottom', fontsize=7)

#净利润柱状图
plt.subplot(232)
ind = np.arange(5)
plt.bar(ind, net_profits, color='gold')
plt.title('Net_profits(Wan Yuan)')
plt.xticks(ind, (year1, year2, year3, year4, year5))
for a, b in zip(ind, net_profits):
    plt.text(a, b+0.05, '%.2f'%b, ha='center', va='bottom', fontsize=7)

#每股收益柱状图
plt.subplot(233)
ind = np.arange(5)
plt.bar(ind, eps, color='#FFA500')
plt.title('EPS')
plt.xticks(ind, (year1, year2, year3, year4, year5))
for a, b in zip(ind, eps):
    plt.text(a, b+0.05, '%.2f'%b, ha='center', va='bottom', fontsize=7)

#roe折线图
plt.subplot(234)
plt.title('ROE(%)')
plt.plot(roe, 'r', label='ROE')
plt.xticks(ind, (year1, year2, year3, year4, year5))

#净利润率折线图
plt.subplot(235)
plt.title('Net_Profit_Ratio(%)')
plt.plot(net_profit_ratio, 'b', label='Net_Profit_Ratio')
plt.xticks(ind, (year1, year2, year3, year4, year5))

#毛利率折线图
plt.subplot(236)
plt.title('Gross_Profit_Rate(%)')
plt.plot(gross_profit_rate, 'g', label='Gross_Profit_Ratio')

plt.xticks(ind, (year1, year2, year3, year4, year5))

plt.show()

图三

import tushare as ts
import matplotlib.pyplot as plt
import numpy as np

print 'please input the year'

year1 = input()
year2 = input()
year3 = input()
year4 = input()
year5 = input()
timelist = []
timelist.append(year1)
timelist.append(year2)
timelist.append(year3)
timelist.append(year4)
timelist.append(year5)

print 'please input the stock code'
scode = input()

arturnover = []
arturndays = []
inventory_turnover = []
inventory_days = []
currentasset_turnover = []
currentasset_days = []

for i in timelist:
    
    operation_data = ts.get_operation_data(i,4)
    operation_data.index = operation_data.code
    data = operation_data[operation_data.index == scode]
    
    arturnover.append(float(data.arturnover))
    arturndays.append(float(data.arturndays))
    inventory_turnover.append(float(data.inventory_turnover))
    inventory_days.append(float(data.inventory_days))
    currentasset_turnover.append(float(data.currentasset_turnover))
    currentasset_days.append(float(data.currentasset_days))
 
plt.figure(figsize=(12,6))
ind = np.arange(5)

#应收账款折线图
plt.subplot(231)
plt.title('AR Turnover(ci)')
plt.plot(arturnover, 'r', label='AR Turnover')
plt.xticks(ind, (year1, year2, year3, year4, year5))

#存货折线图
plt.subplot(232)
plt.title('Inventory Turnover(ci)')
plt.plot(inventory_turnover, 'b', label='Inventory Turnover')
plt.xticks(ind, (year1, year2, year3, year4, year5))

#流动资产折线图
plt.subplot(233)
plt.title('CA Turnover(ci)')
plt.plot(currentasset_turnover, 'g', label='CA Turnover')
plt.xticks(ind, (year1, year2, year3, year4, year5))

#应收账款柱状图
plt.subplot(234)
plt.bar(ind, arturndays, color='yellowgreen')
plt.title('AR Turnover Days')
plt.xticks(ind, (year1, year2, year3, year4, year5))
for a, b in zip(ind, arturndays):

    plt.text(a, b+0.05, '%.2f'%b, ha='center', va='bottom', fontsize=7)

#存货柱状图

plt.subplot(235)
ind = np.arange(5)
plt.bar(ind, inventory_days, color='gold')
plt.title('Inventory Turnover Days')
plt.xticks(ind, (year1, year2, year3, year4, year5))
for a, b in zip(ind, inventory_days):
    plt.text(a, b+0.05, '%.2f'%b, ha='center', va='bottom', fontsize=7)

#流动资产柱状图
plt.subplot(236)
plt.bar(ind, currentasset_days, color='#FFA500')
plt.title('CA Turnover Days')
plt.xticks(ind, (year1, year2, year3, year4, year5))
for a, b in zip(ind, currentasset_days):
    plt.text(a, b+0.05, '%.2f'%b, ha='center', va='bottom', fontsize=7)

plt.show()












































































展开阅读全文

没有更多推荐了,返回首页