阿里巴巴股价数据获取案例(含matplotlib中文显示问题的解决)

linker   ·   发表于 14天前   ·   Python

import pandas as pd
from pandas_datareader import data # 需要安装 pip install git+https://github.com/pydata/pandas-datareader.git

gafataDict={'谷歌':'GOOG',
        '亚马逊':'AMZM',
        'Facebook':'FB',
        '苹果':'AAPL',
        '阿里巴巴':'BABA',
        '腾讯':'0700.hk'}

start_date = '2018-01-01'
end_date = '2019-08-10'

babaDf = data.get_data_yahoo(gafataDict['阿里巴巴'],start_date,end_date)

babaDf.head()


High Low Open Close Volume Adj Close
Date





2018-01-02 184.100006 175.699997 176.399002 183.649994 29916900 183.649994
2018-01-03 185.634995 181.399994 185.190002 184.000000 20121900 184.000000
2018-01-04 187.746994 184.429993 185.899994 185.710007 19473800 185.710007
2018-01-05 190.750000 186.300995 187.169998 190.699997 18168300 190.699997
2018-01-08 191.660004 189.070007 190.460007 190.330002 16230100 190.330002
#上述是阿里巴巴的股票数据。
def change(column):
    #买入价格
    buyPrice=column[0]
    #现在股价
    curPrice=column[-1]
    #累计涨幅
    priceChange=(curPrice-buyPrice)/buyPrice
    #判断走势
    if(priceChange>0):
        print('累计上涨:',priceChange)
    elif(priceChange<0):
        print('累计下跌:',priceChange)
    else:
        print('股价累计无波动')
    return priceChange

closeCol=babaDf['Close']
babaChange=change(closeCol)

print(babaChange)
    
    
    
    
    
累计下跌: -0.13356928720141087
-0.13356928720141087
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

plt.rcParams['font.sans-serif']=['Noto Serif CJK JP']
plt.rcParams['axes.unicode_minus']=False
plt.plot(babaDf['Close'])
plt.title(u'阿里巴巴')
plt.grid(True)
plt
<module 'matplotlib.pyplot' from '/usr/local/lib/python3.7/dist-packages/matplotlib/pyplot.py'>
#可用中文字体
#作者:司毅
#链接:https://www.zhihu.com/question/25404709/answer/128171562
#来源:知乎
#著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

#! /usr/bin/env python
# -*- coding: utf-8 -*-
from matplotlib.font_manager import FontManager
import subprocess

fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
#print(mat_fonts)
output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True)
#print( '*' * 10, '系统可用的中文字体', '*' * 10)
#print (output)
zh_fonts = set(f.split(',', 1)[0] for f in output.decode('utf-8').split('\n'))
available = mat_fonts & zh_fonts
print ('*' * 10, '可用的字体', '*' * 10)
for f in available:
     print (f)
********** 可用的字体 **********
Noto Serif CJK JP
Droid Sans Fallback
Noto Sans CJK JP
import matplotlib 
print(matplotlib.matplotlib_fname()) 
/usr/local/lib/python3.7/dist-packages/matplotlib/mpl-data/matplotlibrc
#需要修改上述的配置文件, 去掉font.family和font.serif
0 Reply   |  Until 14天前 | 12 View
LoginCan Publish Content
程序耗时 0.0229秒 内存:399KB