如何使用matplotlib绘制经典股票图表?

前端之家收集整理的这篇文章主要介绍了如何使用matplotlib绘制经典股票图表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
经典股票价格图表包含一条垂直线,连接每个时间段的最高价和最低价,左侧为开盘价(第一个),右侧为收盘价(最后一个价).

Matplotlib可以绘制日文版,称为烛台,但我无法找到西方版本的解决方案,简称为“条形图”. Matplotlib可以绘制这样的图表吗?

解决方法

调整matplotlib财务包( documentation,code)中的烛台功能
  1. def westerncandlestick(ax,quotes,width=0.2,colorup='k',colordown='r',ochl=True,linewidth=0.5):
  2.  
  3. """
  4. Plot the time,open,high,low,close as a vertical line ranging
  5. from low to high. Use a rectangular bar to represent the
  6. open-close span. If close >= open,use colorup to color the bar,otherwise use colordown
  7. Parameters
  8. ----------
  9. ax : `Axes`
  10. an Axes instance to plot to
  11. quotes : sequence of quote sequences
  12. data to plot. time must be in float date format - see date2num
  13. (time,close,...) vs
  14. (time,...)
  15. set by `ochl`
  16. width : float
  17. fraction of a day for the open and close lines
  18. colorup : color
  19. the color of the lines close >= open
  20. colordown : color
  21. the color of the lines where close < open
  22. ochl: bool
  23. argument to select between ochl and ohlc ordering of quotes
  24. linewidth: float
  25. linewidth of lines
  26. Returns
  27. -------
  28. ret : tuple
  29. returns (lines,openlines,closelines) where lines is a list of lines
  30. added
  31. """
  32.  
  33. OFFSET = width / 2.0
  34.  
  35. lines = []
  36. openlines = []
  37. closelines = []
  38. for q in quotes:
  39. if ochl:
  40. t,low = q[:5]
  41. else:
  42. t,close = q[:5]
  43.  
  44. if close >= open:
  45. color = colorup
  46. else:
  47. color = colordown
  48.  
  49. vline = Line2D( xdata=(t,t),ydata=(low,high),color=color,linewidth=linewidth,antialiased=True)
  50. lines.append(vline)
  51.  
  52. openline = Line2D(xdata=(t - OFFSET,ydata=(open,open),antialiased=True)
  53. openlines.append(openline)
  54.  
  55. closeline = Line2D(xdata=(t,t+OFFSET),ydata=(close,close),antialiased=True)
  56. closelines.append(closeline)
  57.  
  58. ax.add_line(vline)
  59. ax.add_line(openline)
  60. ax.add_line(closeline)
  61.  
  62. ax.autoscale_view()
  63.  
  64. return lines,closelines

叫它,例如像这样:

  1. westerncandlestick(ax,width=0.6,linewidth=1.44,ochl=False)

当然,您可以使用colorup和colordown参数调整颜色.

完成上述情节的完整代码

  1. import matplotlib.pyplot as plt
  2.  
  3. from matplotlib.finance import quotes_historical_yahoo_ohlc
  4. from matplotlib.lines import Line2D
  5.  
  6.  
  7. def westerncandlestick(ax,linewidth=0.5):
  8.  
  9. """
  10. Plot the time,close as a vertical line ranging
  11. from low to high. Use a rectangular bar to represent the
  12. open-close span. If close >= open,otherwise use colordown
  13. Parameters
  14. ----------
  15. ax : `Axes`
  16. an Axes instance to plot to
  17. quotes : sequence of quote sequences
  18. data to plot. time must be in float date format - see date2num
  19. (time,...) vs
  20. (time,...)
  21. set by `ochl`
  22. width : float
  23. fraction of a day for the open and close lines
  24. colorup : color
  25. the color of the lines close >= open
  26. colordown : color
  27. the color of the lines where close < open
  28. ochl: bool
  29. argument to select between ochl and ohlc ordering of quotes
  30. linewidth: float
  31. linewidth of lines
  32. Returns
  33. -------
  34. ret : tuple
  35. returns (lines,closelines) where lines is a list of lines
  36. added
  37. """
  38.  
  39. OFFSET = width / 2.0
  40.  
  41. lines = []
  42. openlines = []
  43. closelines = []
  44. for q in quotes:
  45. if ochl:
  46. t,low = q[:5]
  47. else:
  48. t,close = q[:5]
  49.  
  50. if close >= open:
  51. color = colorup
  52. else:
  53. color = colordown
  54.  
  55. vline = Line2D( xdata=(t,antialiased=True)
  56. lines.append(vline)
  57. openline = Line2D(xdata=(t - OFFSET,antialiased=True)
  58. openlines.append(openline)
  59. closeline = Line2D(xdata=(t,antialiased=True)
  60. closelines.append(closeline)
  61.  
  62. ax.add_line(vline)
  63. ax.add_line(openline)
  64. ax.add_line(closeline)
  65.  
  66. ax.autoscale_view()
  67.  
  68. return lines,closelines
  69.  
  70.  
  71. from matplotlib.dates import DateFormatter,WeekdayLocator,\
  72. DayLocator,MONDAY
  73. # (Year,month,day) tuples suffice as args for quotes_historical_yahoo
  74. date1 = (2004,2,1)
  75. date2 = (2004,4,12)
  76.  
  77. mondays = WeekdayLocator(MONDAY) # major ticks on the mondays
  78. alldays = DayLocator() # minor ticks on the days
  79. weekFormatter = DateFormatter('%b %d') # e.g.,Jan 12
  80. dayFormatter = DateFormatter('%d') # e.g.,12
  81.  
  82. quotes = quotes_historical_yahoo_ohlc('INTC',date1,date2)
  83. if len(quotes) == 0:
  84. raise SystemExit
  85.  
  86. fig,ax = plt.subplots()
  87. fig.subplots_adjust(bottom=0.2)
  88. ax.xaxis.set_major_locator(mondays)
  89. ax.xaxis.set_minor_locator(alldays)
  90. ax.xaxis.set_major_formatter(weekFormatter)
  91.  
  92.  
  93.  
  94. westerncandlestick(ax,ochl=False)
  95.  
  96.  
  97. ax.xaxis_date()
  98. ax.autoscale_view()
  99. plt.setp(plt.gca().get_xticklabels(),rotation=45,horizontalalignment='right')
  100.  
  101. plt.show()

猜你在找的Python相关文章