@Channelchan
2018-09-29T22:59:58.000000Z
字数 3986
阅读 78073
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
价格直方图
获取一个dataframe,并用直方图显示:
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()
回报率直方图:
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()
累积直方图
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()
价格散点图
stock2 = pd.read_excel('sz50.xlsx',sheetname='601318.XSHG', index_col='datetime')
plt.figure(figsize=(15, 7))
plt.scatter(stock1.close, stock2.close, c = ['b','r'], s = 20)
plt.xlabel('600036')
plt.ylabel('601318')
plt.title('Daily Prices in 2017')
plt.show()
回报率散点图:
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()
画出价格线图。
plt.figure(figsize=(15, 7))
plt.plot(stock1.close)
plt.plot(stock2.close)
plt.ylabel('Price')
plt.legend(['600036','601318'])
plt.show()
回报率线图
plt.figure(figsize=(15, 7))
plt.plot(R1)
plt.ylabel('Return')
plt.title('600036 Returns')
plt.show()
from matplotlib.pylab import date2num
stock1['time'] = list(map(date2num, stock1.index))
candle = stock1.reindex_axis(["time", "open", "high", "low", "close"], 1).values
import matplotlib.finance as mpf
fig, (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.time,stock1.volume)
ax.xaxis_date ()
plt.show()
设置标题,轴标签,多图展示
stock1 = pd.read_excel('sz50.xlsx',sheetname='601318.XSHG', index_col='datetime').close
stock2 = pd.read_excel('sz50.xlsx',sheetname='600000.XSHG', index_col='datetime').close
stock3 = pd.read_excel('sz50.xlsx',sheetname='600016.XSHG', index_col='datetime').close
stock4 = pd.read_excel('sz50.xlsx',sheetname='600028.XSHG', index_col='datetime').close
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2,figsize=(20, 8))
ax1.plot(stock1)
ax1.set_title('stock')
ax1.set_ylabel('price')
ax1.set_xlabel('date')
ax2.plot(stock2)
ax3.plot(stock3)
ax4.plot(stock4)
plt.subplots_adjust(wspace=0.1, hspace=0.5)
plt.show()
plt.figure(figsize=(7, 5))
plt.subplot(221)
plt.plot(stock1)
plt.xlabel('date')
plt.ylabel('price')
plt.title('stock1')
plt.subplot(222)
plt.plot(stock2)
plt.subplot(223)
plt.plot(stock3)
plt.subplot(224)
plt.plot(stock4)
plt.subplots_adjust(wspace=0.5, hspace=0.5)
plt.show()
fig, ax1 = plt.subplots(figsize=(15, 7))
plt.plot(stock1,'r')
plt.grid(True)
plt.axis('tight')
plt.xlabel('date')
plt.ylabel('Price 1nd')
plt.title('two stock')
plt.legend(['601318'],loc =2)
ax2 = ax1.twinx() #双坐标
plt.plot(stock4)
plt.ylabel('Price 2nd')
plt.legend(['600028'],loc = 4)
plt.show()
legend()里的参数
'best' : 0, (only implemented for axes legends)(自适应方式)
'upper right' : 1,
'upper left' : 2,
'lower left' : 3,
'lower right' : 4,
'right' : 5,
'center left' : 6,
'center right' : 7,
'lower center' : 8,
'upper center' : 9,
'center' : 10,
plt.plot?
填空题:根据以下要求填空
fig, ax = plt.subplots(figsize=(12,6))
x = np.linspace(0, 5, 10)
# # line 1 -- line 4 ,线形图,蓝色线,线宽分别为 0.25 ,0.50 , 1.00 ,2.00
ax.plot(x, x+1, color=?, linewidth=?)
ax.plot(x, x+2, ? ,?)
ax.plot(x, x+3, ? ,?)
ax.plot(x, x+4, ? ,?)
# line 5 -- line 8 ,线形图,红色,线宽为2 ,线型(linestyle=)分别为 ‘-‘ ,‘-.’, ‘:’, ‘steps’
ax.plot(x, x+5, )
ax.plot(x, x+6, )
ax.plot(x, x+7, )
ax.plot(x, x+8, )
# line 9 -- line 12 ,线形图,绿色,线宽为2 , 线型为 ‘--’,点的样式(marker=) '+' , 'o' , 's' , '>'
# possible marker symbols: marker = '+', 'o', '*', 's', ',', '.', '1', '2', '3', '4', ...
ax.plot(x, x+ 9, )
ax.plot(x, x+10, )
ax.plot(x, x+11, )
ax.plot(x, x+12, )
# line 13-- line 15,
# 线形图,紫色,线宽为1 , 线型为 ‘-’,点的样式 'o' ,样式尺寸(markersize)分别为 2,4,8 ,样式颜色(markerfacecolor):红色
ax.plot(x, x+13, )
ax.plot(x, x+14, )
ax.plot(x, x+15, )