金融学图表 matplotlib.finance

0
回复
4467
查看
[复制链接]

209

主题

26

回帖

1424

积分

超级版主

积分
1424
来源: 2019-8-24 16:59:37 显示全部楼层 |阅读模式
1.烛柱图 candlestick
  1. #!/etc/bin/python
  2. #coding=utf-8
  3. import matplotlib.pyplot as plt
  4. import matplotlib.finance as mpf
  5. start = (2014, 5,1)
  6. end = (2014, 7,1)
  7. quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end)
  8. # print quotes[:2]

  9. fig, ax = plt.subplots(figsize=(8,5))
  10. fig.subplots_adjust(bottom = 0.2)
  11. mpf.candlestick(ax, quotes, width=0.6, colorup='b',colordown='r')
  12. plt.grid(True)
  13. ax.xaxis_date() #x轴上的日期
  14. ax.autoscale_view()
  15. plt.setp(plt.gca().get_xticklabels(),rotation=30) #日期倾斜
  16. plt.show()
复制代码

022.png

2. plot_day_summary
该函数提供了一个相当类似的图标类型,使用方法和 candlestick 函数相同,使用类似的参数. 这里开盘价和收盘价不是由彩色矩形表示,而是由两条短水平线表示.
  1. #!/etc/bin/python
  2. #coding=utf-8
  3. import matplotlib.pyplot as plt
  4. import matplotlib.finance as mpf
  5. start = (2014, 5,1)
  6. end = (2014, 7,1)
  7. quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end)
  8. # print quotes[:2]

  9. fig, ax = plt.subplots(figsize=(8,5))
  10. fig.subplots_adjust(bottom = 0.2)
  11. mpf.plot_day_summary(ax, quotes,  colorup='b',colordown='r')
  12. plt.grid(True)
  13. ax.xaxis_date() #x轴上的日期
  14. ax.autoscale_view()
  15. plt.setp(plt.gca().get_xticklabels(),rotation=30) #日期倾斜
  16. plt.show()
复制代码
023.png
3.股价数据和成交量
  1. #!/etc/bin/python
  2. #coding=utf-8
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. import matplotlib.finance as mpf
  6. start = (2014, 5,1)
  7. end = (2014, 7,1)
  8. quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end)
  9. # print quotes[:2]

  10. quotes = np.array(quotes)
  11. fig, (ax1, ax2) = plt.subplots(2, sharex=True, figsize=(8,6))
  12. mpf.candlestick(ax1, quotes, width=0.6,colorup='b',colordown='r')
  13. ax1.set_title('Yahoo Inc.')
  14. ax1.set_ylabel('index level')
  15. ax1.grid(True)
  16. ax1.xaxis_date()
  17. plt.bar(quotes[:,0] - 0.25, quotes[:, 5], width=0.5)

  18. ax2.set_ylabel('volume')
  19. ax2.grid(True)
  20. ax2.autoscale_view()
  21. plt.setp(plt.gca().get_xticklabels(),rotation=30)
  22. plt.show()
复制代码
024.png

3.3D 绘图
  1. #!/etc/bin/python
  2. #coding=utf-8
  3. import numpy as np
  4. import matplotlib.pyplot as plt

  5. stike = np.linspace(50, 150, 24)
  6. ttm = np.linspace(0.5, 2.5, 24)
  7. stike, ttm = np.meshgrid(stike, ttm)
  8. print  stike[:2]

  9. iv = (stike - 100) ** 2 / (100 * stike) /ttm
  10. from mpl_toolkits.mplot3d import Axes3D
  11. fig = plt.figure(figsize=(9,6))
  12. ax = fig.gca(projection='3d')
  13. surf = ax.plot_surface(stike, ttm, iv, rstride=2, cstride=2, cmap=plt.cm.coolwarm, linewidth=0.5, antialiased=True)
  14. ax.set_xlabel('strike')
  15. ax.set_ylabel('time-to-maturity')
  16. ax.set_zlabel('implied volatility')

  17. plt.show()
复制代码
025.png



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册
关注微信