[关闭]
@Channelchan 2018-09-29T22:59:58.000000Z 字数 3986 阅读 78073
  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import warnings
  5. warnings.filterwarnings('ignore')

1、直方图_hist()

价格直方图

获取一个dataframe,并用直方图显示:

  1. stock1 = pd.read_excel('sz50.xlsx',sheetname='600036.XSHG', index_col='datetime')
  1. plt.figure(figsize=(15, 7))
  2. plt.hist(stock1.close, bins=20)
  3. plt.xlabel('Price')
  4. plt.ylabel('Number of Days Observed')
  5. plt.title('Frequency Distribution of 000001 Prices, 2016')
  6. plt.show()

output_3_0.png-10.4kB

回报率直方图:

  1. R1 = stock1.close.pct_change()[1:]
  2. # 20 bins,代表柱的数量
  3. plt.figure(figsize=(15, 7))
  4. plt.hist(R1, bins=20)
  5. plt.xlabel('Return')
  6. plt.ylabel('Number of Days Observed')
  7. plt.title('Frequency Distribution of 000001 Returns, 2016')
  8. plt.show()

output_5_0.png-11.1kB

累积直方图

  1. plt.figure(figsize=(15, 7))
  2. plt.hist(R1, bins=20, cumulative=True)
  3. plt.xlabel('Return')
  4. plt.ylabel('Number of Days Observed')
  5. plt.title('Cumulative Distribution of 600036 Returns, 2016')
  6. plt.show()

output_7_0.png-11.5kB

2、 散点图_scatter()

价格散点图

  1. stock2 = pd.read_excel('sz50.xlsx',sheetname='601318.XSHG', index_col='datetime')
  1. plt.figure(figsize=(15, 7))
  2. plt.scatter(stock1.close, stock2.close, c = ['b','r'], s = 20)
  3. plt.xlabel('600036')
  4. plt.ylabel('601318')
  5. plt.title('Daily Prices in 2017')
  6. plt.show()

output_11_0.png-24.3kB

回报率散点图:

  1. plt.figure(figsize=(15, 7))
  2. R2 = stock2.close.pct_change()[1:]
  3. plt.scatter(R1, R2,c=['c','r'],s=20)
  4. plt.xlabel('000001')
  5. plt.ylabel('000005')
  6. plt.title('Daily Returns in 2016')
  7. plt.show()

output_13_0.png-32kB

3、 线图_plot

画出价格线图。

  1. plt.figure(figsize=(15, 7))
  2. plt.plot(stock1.close)
  3. plt.plot(stock2.close)
  4. plt.ylabel('Price')
  5. plt.legend(['600036','601318'])
  6. plt.show()

output_15_0.png-34kB

回报率线图

  1. plt.figure(figsize=(15, 7))
  2. plt.plot(R1)
  3. plt.ylabel('Return')
  4. plt.title('600036 Returns')
  5. plt.show()

![png](output_17_0.png)

4、 蜡烛图_candlestick_ohlc

  1. from matplotlib.pylab import date2num
  2. stock1['time'] = list(map(date2num, stock1.index))
  1. candle = stock1.reindex_axis(["time", "open", "high", "low", "close"], 1).values
  1. import matplotlib.finance as mpf
  2. fig, (ax,ax1) = plt.subplots(2,1,sharex=True, figsize=(15,25))
  3. fig.subplots_adjust(bottom=0.5)
  4. ax.grid(True)
  5. mpf.candlestick_ohlc(ax, candle, width=0.6, colorup='r', colordown='g',alpha=1.0)
  6. ax1.bar(stock1.time,stock1.volume)
  7. ax.xaxis_date ()
  8. plt.show()

output_21_0.png-18.1kB

5、 多图_subplots & subplot

设置标题,轴标签,多图展示

  1. stock1 = pd.read_excel('sz50.xlsx',sheetname='601318.XSHG', index_col='datetime').close
  2. stock2 = pd.read_excel('sz50.xlsx',sheetname='600000.XSHG', index_col='datetime').close
  3. stock3 = pd.read_excel('sz50.xlsx',sheetname='600016.XSHG', index_col='datetime').close
  4. stock4 = pd.read_excel('sz50.xlsx',sheetname='600028.XSHG', index_col='datetime').close
  1. fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2,figsize=(20, 8))
  2. ax1.plot(stock1)
  3. ax1.set_title('stock')
  4. ax1.set_ylabel('price')
  5. ax1.set_xlabel('date')
  6. ax2.plot(stock2)
  7. ax3.plot(stock3)
  8. ax4.plot(stock4)
  9. plt.subplots_adjust(wspace=0.1, hspace=0.5)
  10. plt.show()

output_24_0.png-69.5kB

  1. plt.figure(figsize=(7, 5))
  2. plt.subplot(221)
  3. plt.plot(stock1)
  4. plt.xlabel('date')
  5. plt.ylabel('price')
  6. plt.title('stock1')
  7. plt.subplot(222)
  8. plt.plot(stock2)
  9. plt.subplot(223)
  10. plt.plot(stock3)
  11. plt.subplot(224)
  12. plt.plot(stock4)
  13. plt.subplots_adjust(wspace=0.5, hspace=0.5)
  14. plt.show()

output_25_0.png-30.7kB

双坐标图_twinx

  1. fig, ax1 = plt.subplots(figsize=(15, 7))
  2. plt.plot(stock1,'r')
  3. plt.grid(True)
  4. plt.axis('tight')
  5. plt.xlabel('date')
  6. plt.ylabel('Price 1nd')
  7. plt.title('two stock')
  8. plt.legend(['601318'],loc =2)
  9. ax2 = ax1.twinx() #双坐标
  10. plt.plot(stock4)
  11. plt.ylabel('Price 2nd')
  12. plt.legend(['600028'],loc = 4)
  13. plt.show()

output_27_0.png-60kB

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,

  1. plt.plot?

课堂作业题 (线型、颜色、线宽、样式)

填空题:根据以下要求填空

  1. fig, ax = plt.subplots(figsize=(12,6))
  2. x = np.linspace(0, 5, 10)
  3. # # line 1 -- line 4 ,线形图,蓝色线,线宽分别为 0.25 ,0.50 , 1.00 ,2.00
  4. ax.plot(x, x+1, color=?, linewidth=?)
  5. ax.plot(x, x+2, ? ,?)
  6. ax.plot(x, x+3, ? ,?)
  7. ax.plot(x, x+4, ? ,?)
  8. # line 5 -- line 8 ,线形图,红色,线宽为2 ,线型(linestyle=)分别为 ‘-‘ ,‘-.’, ‘:’, ‘steps’
  9. ax.plot(x, x+5, )
  10. ax.plot(x, x+6, )
  11. ax.plot(x, x+7, )
  12. ax.plot(x, x+8, )
  13. # line 9 -- line 12 ,线形图,绿色,线宽为2 , 线型为 ‘--’,点的样式(marker=) '+' , 'o' , 's' , '>'
  14. # possible marker symbols: marker = '+', 'o', '*', 's', ',', '.', '1', '2', '3', '4', ...
  15. ax.plot(x, x+ 9, )
  16. ax.plot(x, x+10, )
  17. ax.plot(x, x+11, )
  18. ax.plot(x, x+12, )
  19. # line 13-- line 15,
  20. # 线形图,紫色,线宽为1 , 线型为 ‘-’,点的样式 'o' ,样式尺寸(markersize)分别为 2,4,8 ,样式颜色(markerfacecolor):红色
  21. ax.plot(x, x+13, )
  22. ax.plot(x, x+14, )
  23. ax.plot(x, x+15, )

1.png-127.1kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注