@Channelchan
2017-11-27T05:50:28.000000Z
字数 2032
阅读 137778
价格直方图
获取一个dataframe,并用直方图显示:
import pandas as pdimport matplotlib.pyplot as plt
stock1 = pd.read_excel('sz50.xlsx',sheetname='600036.XSHG', index_col='datetime')
plt.figure(figsize=(15, 7))plt.hist(stock1.close, bins=20)plt.xlabel('Price')plt.ylabel('Number of Days Observed')plt.title('Frequency Distribution of 000001 Prices, 2016')plt.show()
](http://static.zybuluo.com/Channelchan/j3zbb5p6hfvgzvh8khgn6i15/output_3_0.png)
R1 = stock1.close.pct_change()[1:]# 20 bins,代表柱的数量plt.figure(figsize=(15, 7))plt.hist(R1, bins=20)plt.xlabel('Return')plt.ylabel('Number of Days Observed')plt.title('Frequency Distribution of 000001 Returns, 2016')plt.show()
](http://static.zybuluo.com/Channelchan/wvy6bo92zfd88in5g7cap6vy/output_5_0.png)
plt.figure(figsize=(15, 7))plt.hist(R1, bins=20, cumulative=True)plt.xlabel('Return')plt.ylabel('Number of Days Observed')plt.title('Cumulative Distribution of 600036 Returns, 2016')plt.show()
](http://static.zybuluo.com/Channelchan/rp6qyattedaz0zrun9l7hsqv/output_7_0.png)
stock2 = pd.read_excel('sz50.xlsx',sheetname='601318.XSHG', index_col='datetime')
plt.figure(figsize=(15, 7))plt.scatter(stock1.close, stock2.close, c = ['c','r'], s = 20)plt.xlabel('600036')plt.ylabel('601318')plt.title('Daily Prices in 2017')plt.show()
](http://static.zybuluo.com/Channelchan/uy9ymax1tqi8b7pshand6509/output_9_0.png)
plt.figure(figsize=(15, 7))R2 = stock2.close.pct_change()[1:]plt.scatter(R1, R2,c=['c','r'],s=20)plt.xlabel('000001')plt.ylabel('000005')plt.title('Daily Returns in 2016')plt.show()
](http://static.zybuluo.com/Channelchan/li30qsrdhqo7v55i7b3j0sf2/output_11_0.png)
画出价格线图。
plt.figure(figsize=(15, 7))plt.plot(stock1.close)plt.plot(stock2.close)plt.ylabel('Price')plt.legend(['600036','601318'])plt.show()
](http://static.zybuluo.com/Channelchan/wa18ipcy23n87a67h5hceqru/output_13_0.png)
画出股票000001的回报率线图。
plt.figure(figsize=(15, 7))plt.plot(R1)plt.hlines(0,R1.index[0],R1.index[-1],linestyles='dashed',alpha=0.3)plt.ylabel('Return')plt.title('600036 Returns')plt.show()
](http://static.zybuluo.com/Channelchan/rbf4qorwfx2c7jcdo7fo1k9h/output_15_0.png)
plt.figure(figsize=(15, 7))plt.subplot(2,1,1)plt.plot(stock1.close)plt.subplot(2,1,2)plt.plot(stock2.close)plt.show()
](http://static.zybuluo.com/Channelchan/1u7s72rpomz7srr0sxx34wtu/output_17_0.png)
from matplotlib.pylab import date2numstock1['time'] = list(map(date2num, stock1.index))
candle = stock1.reindex_axis(["time", "open", "high", "low", "close"], 1).values
import matplotlib.finance as mpffig, (ax,ax1) = plt.subplots(2,1,sharex=True, figsize=(15,25))fig.subplots_adjust(bottom=0.5)ax.grid(True)mpf.candlestick_ohlc(ax, candle, width=0.6, colorup='r', colordown='g',alpha=1.0)ax1.bar(stock1.index,stock1.volume)ax.xaxis_date ()plt.show()
](http://static.zybuluo.com/Channelchan/emdkfpnrqokp4toitmfxqhve/output_20_0.png)
