为此,首先我们需要定义一个新的轴域,但是这个轴域是仅带有x轴的『双生子』。

    这足以创建轴域了。我们叫它ax2v,因为这个轴域是ax2加交易量。

    现在,我们在轴域上定义绘图,我们将添加:

    我们在 0 和当前交易量之间填充,给予它蓝色的前景色,然后给予它一个透明度。 我们想要应用幽冥毒,以防交易量最终覆盖其它东西,所以我们仍然可以看到这两个元素。

    1. import matplotlib.pyplot as plt
    2. import matplotlib.dates as mdates
    3. import matplotlib.ticker as mticker
    4. from matplotlib.finance import candlestick_ohlc
    5. from matplotlib import style
    6. import numpy as np
    7. import urllib
    8. import datetime as dt
    9. style.use('fivethirtyeight')
    10. print(plt.style.available)
    11. print(plt.__file__)
    12. MA1 = 10
    13. MA2 = 30
    14. def moving_average(values, window):
    15. weights = np.repeat(1.0, window)/window
    16. smas = np.convolve(values, weights, 'valid')
    17. return smas
    18. def high_minus_low(highs, lows):
    19. return highs-lows
    20. def bytespdate2num(fmt, encoding='utf-8'):
    21. strconverter = mdates.strpdate2num(fmt)
    22. def bytesconverter(b):
    23. s = b.decode(encoding)
    24. return strconverter(s)
    25. return bytesconverter
    26. def graph_data(stock):
    27. fig = plt.figure()
    28. ax1 = plt.subplot2grid((6,1), (0,0), rowspan=1, colspan=1)
    29. plt.title(stock)
    30. plt.ylabel('H-L')
    31. ax2 = plt.subplot2grid((6,1), (1,0), rowspan=4, colspan=1, sharex=ax1)
    32. plt.ylabel('Price')
    33. ax2v = ax2.twinx()
    34. ax3 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1, sharex=ax1)
    35. plt.ylabel('MAvgs')
    36. stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
    37. source_code = urllib.request.urlopen(stock_price_url).read().decode()
    38. stock_data = []
    39. split_source = source_code.split('\n')
    40. for line in split_source:
    41. split_line = line.split(',')
    42. if len(split_line) == 6:
    43. if 'values' not in line and 'labels' not in line:
    44. stock_data.append(line)
    45. date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,
    46. delimiter=',',
    47. unpack=True,
    48. converters={0: bytespdate2num('%Y%m%d')})
    49. x = 0
    50. y = len(date)
    51. ohlc = []
    52. while x < y:
    53. append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x]
    54. ohlc.append(append_me)
    55. x+=1
    56. ma1 = moving_average(closep,MA1)
    57. ma2 = moving_average(closep,MA2)
    58. start = len(date[MA2-1:])
    59. h_l = list(map(high_minus_low, highp, lowp))
    60. ax1.plot_date(date[-start:],h_l[-start:],'-')
    61. ax1.yaxis.set_major_locator(mticker.MaxNLocator(nbins=4, prune='lower'))
    62. candlestick_ohlc(ax2, ohlc[-start:], width=0.4, colorup='#77d879', colordown='#db3f3f')
    63. ax2.yaxis.set_major_locator(mticker.MaxNLocator(nbins=7, prune='upper'))
    64. ax2.grid(True)
    65. bbox_props = dict(boxstyle='round',fc='w', ec='k',lw=1)
    66. xytext = (date[-1]+4, closep[-1]), bbox=bbox_props)
    67. ## # Annotation example with arrow
    68. ## ax2.annotate('Bad News!',(date[11],highp[11]),
    69. ## xytext=(0.8, 0.9), textcoords='axes fraction',
    70. ## arrowprops = dict(facecolor='grey',color='grey'))
    71. ##
    72. ##
    73. ## # Font dict example
    74. ## font_dict = {'family':'serif',
    75. ## 'color':'darkred',
    76. ## 'size':15}
    77. ## # Hard coded text
    78. ## ax2.text(date[10], closep[1],'Text Example', fontdict=font_dict)
    79. ax2v.fill_between(date[-start:],0, volume[-start:], facecolor='#0079a3', alpha=0.4)
    80. ax3.plot(date[-start:], ma1[-start:], linewidth=1)
    81. ax3.plot(date[-start:], ma2[-start:], linewidth=1)
    82. ax3.fill_between(date[-start:], ma2[-start:], ma1[-start:],
    83. where=(ma1[-start:] < ma2[-start:]),
    84. facecolor='r', edgecolor='r', alpha=0.5)
    85. ax3.fill_between(date[-start:], ma2[-start:], ma1[-start:],
    86. where=(ma1[-start:] > ma2[-start:]),
    87. facecolor='g', edgecolor='g', alpha=0.5)
    88. ax3.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    89. ax3.xaxis.set_major_locator(mticker.MaxNLocator(10))
    90. ax3.yaxis.set_major_locator(mticker.MaxNLocator(nbins=4, prune='upper'))
    91. for label in ax3.xaxis.get_ticklabels():
    92. label.set_rotation(45)
    93. plt.setp(ax1.get_xticklabels(), visible=False)
    94. plt.setp(ax2.get_xticklabels(), visible=False)
    95. plt.subplots_adjust(left=0.11, bottom=0.24, right=0.90, top=0.90, wspace=0.2, hspace=0)
    96. plt.show()
    97. graph_data('GOOG')

    会生成:

    太棒了,到目前为止还不错。 接下来,我们可能要删除新y轴上的标签,然后我们也可能不想让交易量占用太多空间。 没问题:

    首先:

    接下来,我们可能要将网格设置为false,使轴域上不会有双网格:

    1. ax2v.grid(False)

    最后,为了处理交易量占用很多空间,我们可以做以下操作:

    所以这设置y轴显示范围从 0 到交易量的最大值的 3 倍。 这意味着,在最高点,交易量最多可占据图形的33%。 所以,增加volume.max的倍数越多,空间就越小/越少。

    1. import matplotlib.pyplot as plt
    2. import matplotlib.dates as mdates
    3. import matplotlib.ticker as mticker
    4. from matplotlib.finance import candlestick_ohlc
    5. from matplotlib import style
    6. import numpy as np
    7. import urllib
    8. import datetime as dt
    9. style.use('fivethirtyeight')
    10. print(plt.style.available)
    11. print(plt.__file__)
    12. MA1 = 10
    13. MA2 = 30
    14. def moving_average(values, window):
    15. weights = np.repeat(1.0, window)/window
    16. smas = np.convolve(values, weights, 'valid')
    17. return smas
    18. def high_minus_low(highs, lows):
    19. return highs-lows
    20. def bytespdate2num(fmt, encoding='utf-8'):
    21. strconverter = mdates.strpdate2num(fmt)
    22. def bytesconverter(b):
    23. s = b.decode(encoding)
    24. return strconverter(s)
    25. return bytesconverter
    26. def graph_data(stock):
    27. fig = plt.figure()
    28. ax1 = plt.subplot2grid((6,1), (0,0), rowspan=1, colspan=1)
    29. plt.title(stock)
    30. plt.ylabel('H-L')
    31. ax2 = plt.subplot2grid((6,1), (1,0), rowspan=4, colspan=1, sharex=ax1)
    32. plt.ylabel('Price')
    33. ax2v = ax2.twinx()
    34. plt.ylabel('MAvgs')
    35. source_code = urllib.request.urlopen(stock_price_url).read().decode()
    36. stock_data = []
    37. split_source = source_code.split('\n')
    38. for line in split_source:
    39. split_line = line.split(',')
    40. if len(split_line) == 6:
    41. if 'values' not in line and 'labels' not in line:
    42. stock_data.append(line)
    43. date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,
    44. delimiter=',',
    45. unpack=True,
    46. converters={0: bytespdate2num('%Y%m%d')})
    47. x = 0
    48. y = len(date)
    49. ohlc = []
    50. while x < y:
    51. append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x]
    52. ohlc.append(append_me)
    53. x+=1
    54. ma1 = moving_average(closep,MA1)
    55. ma2 = moving_average(closep,MA2)
    56. start = len(date[MA2-1:])
    57. h_l = list(map(high_minus_low, highp, lowp))
    58. ax1.plot_date(date[-start:],h_l[-start:],'-')
    59. ax1.yaxis.set_major_locator(mticker.MaxNLocator(nbins=4, prune='lower'))
    60. candlestick_ohlc(ax2, ohlc[-start:], width=0.4, colorup='#77d879', colordown='#db3f3f')
    61. ax2.yaxis.set_major_locator(mticker.MaxNLocator(nbins=7, prune='upper'))
    62. ax2.grid(True)
    63. bbox_props = dict(boxstyle='round',fc='w', ec='k',lw=1)
    64. ax2.annotate(str(closep[-1]), (date[-1], closep[-1]),
    65. xytext = (date[-1]+5, closep[-1]), bbox=bbox_props)
    66. ## # Annotation example with arrow
    67. ## ax2.annotate('Bad News!',(date[11],highp[11]),
    68. ## xytext=(0.8, 0.9), textcoords='axes fraction',
    69. ## arrowprops = dict(facecolor='grey',color='grey'))
    70. ##
    71. ##
    72. ## # Font dict example
    73. ## font_dict = {'family':'serif',
    74. ## 'color':'darkred',
    75. ## 'size':15}
    76. ## # Hard coded text
    77. ## ax2.text(date[10], closep[1],'Text Example', fontdict=font_dict)
    78. ax2v.fill_between(date[-start:],0, volume[-start:], facecolor='#0079a3', alpha=0.4)
    79. ax2v.axes.yaxis.set_ticklabels([])
    80. ax2v.grid(False)
    81. ax2v.set_ylim(0, 3*volume.max())
    82. ax3.plot(date[-start:], ma1[-start:], linewidth=1)
    83. ax3.plot(date[-start:], ma2[-start:], linewidth=1)
    84. ax3.fill_between(date[-start:], ma2[-start:], ma1[-start:],
    85. where=(ma1[-start:] < ma2[-start:]),
    86. facecolor='r', edgecolor='r', alpha=0.5)
    87. ax3.fill_between(date[-start:], ma2[-start:], ma1[-start:],
    88. where=(ma1[-start:] > ma2[-start:]),
    89. facecolor='g', edgecolor='g', alpha=0.5)
    90. ax3.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    91. ax3.xaxis.set_major_locator(mticker.MaxNLocator(10))
    92. ax3.yaxis.set_major_locator(mticker.MaxNLocator(nbins=4, prune='upper'))
    93. for label in ax3.xaxis.get_ticklabels():
    94. label.set_rotation(45)
    95. plt.setp(ax1.get_xticklabels(), visible=False)
    96. plt.setp(ax2.get_xticklabels(), visible=False)
    97. plt.subplots_adjust(left=0.11, bottom=0.24, right=0.90, top=0.90, wspace=0.2, hspace=0)
    98. plt.show()
    99. graph_data('GOOG')

    到这里,我们差不多完成了。 这里唯一的缺陷是一个好的图例。 一些线条是显而易见的,但人们可能会好奇移动均值的参数是什么,我们这里是 10 和 30。 添加自定义图例是下一个教程中涉及的内容。