二十二、自定义填充、修剪和清除

欢迎阅读另一个 Matplotlib 教程! 在本教程中,我们将清除图表,然后再做一些自定义。

我们当前的代码是:

  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. ax2 = plt.subplot2grid((6,1), (1,0), rowspan=4, colspan=1)
  31. plt.xlabel('Date')
  32. plt.ylabel('Price')
  33. ax3 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1)
  34. stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
  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,h_l,'-')
  59. candlestick_ohlc(ax2, ohlc, width=0.4, colorup='#77d879', colordown='#db3f3f')
  60. for label in ax2.xaxis.get_ticklabels():
  61. label.set_rotation(45)
  62. ax2.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
  63. ax2.xaxis.set_major_locator(mticker.MaxNLocator(10))
  64. ax2.grid(True)
  65. bbox_props = dict(boxstyle='round',fc='w', ec='k',lw=1)
  66. ax2.annotate(str(closep[-1]), (date[-1], closep[-1]),
  67. xytext = (date[-1]+4, closep[-1]), bbox=bbox_props)
  68. ## # Annotation example with arrow
  69. ## ax2.annotate('Bad News!',(date[11],highp[11]),
  70. ## xytext=(0.8, 0.9), textcoords='axes fraction',
  71. ## arrowprops = dict(facecolor='grey',color='grey'))
  72. ##
  73. ##
  74. ## # Font dict example
  75. ## font_dict = {'family':'serif',
  76. ## 'color':'darkred',
  77. ## 'size':15}
  78. ## # Hard coded text
  79. ## ax2.text(date[10], closep[1],'Text Example', fontdict=font_dict)
  80. ax3.plot(date[-start:], ma1[-start:])
  81. ax3.plot(date[-start:], ma2[-start:])
  82. plt.subplots_adjust(left=0.11, bottom=0.24, right=0.90, top=0.90, wspace=0.2, hspace=0)
  83. plt.show()
  84. graph_data('EBAY')

现在我认为向我们的移动均值添加自定义填充是一个很好的主意。 移动均值通常用于说明价格趋势。 这个想法是,你可以计算一个快速和一个慢速的移动均值。 一般来说,移动均值用于使价格变得『平滑』。 他们总是『滞后』于价格,但是我们的想法是计算不同的速度。 移动均值越大就越『慢』。 所以这个想法是,如果『较快』的移动均值超过『较慢』的均值,那么价格就会上升,这是一件好事。 如果较快的 MA 从较慢的 MA 下方穿过,则这是下降趋势并且通常被视为坏事。 我的想法是在快速和慢速 MA 之间填充,『上升』趋势为绿色,然后下降趋势为红色。 方法如下:

  1. ax3.fill_between(date[-start:], ma2[-start:], ma1[-start:],
  2. where=(ma1[-start:] < ma2[-start:]),
  3. facecolor='r', edgecolor='r', alpha=0.5)
  4. ax3.fill_between(date[-start:], ma2[-start:], ma1[-start:],
  5. where=(ma1[-start:] > ma2[-start:]),
  6. facecolor='g', edgecolor='g', alpha=0.5)

下面,我们会碰到一些我们可解决的问题:

  1. ax3.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
  2. ax3.xaxis.set_major_locator(mticker.MaxNLocator(10))
  3. for label in ax3.xaxis.get_ticklabels():
  4. label.set_rotation(45)
  5. plt.setp(ax1.get_xticklabels(), visible=False)
  6. plt.setp(ax2.get_xticklabels(), visible=False)

这里,我们剪切和粘贴ax2日期格式,然后我们将x刻度标签设置为false,去掉它们!

我们还可以通过在轴域定义中执行以下操作,为每个轴域提供自定义标签:

  1. fig = plt.figure()
  2. ax1 = plt.subplot2grid((6,1), (0,0), rowspan=1, colspan=1)
  3. plt.title(stock)
  4. ax2 = plt.subplot2grid((6,1), (1,0), rowspan=4, colspan=1)
  5. plt.xlabel('Date')
  6. plt.ylabel('Price')
  7. ax3 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1)

接下来,我们可以看到,我们y刻度有许多数字,经常互相覆盖。 我们也看到轴之间互相重叠。 我们可以这样:

  1. ax1.yaxis.set_major_locator(mticker.MaxNLocator(nbins=5, prune='lower'))

所以,这里发生的是,我们通过首先将nbins设置为 5 来修改我们的y轴对象。这意味着我们显示的标签最多为 5 个。然后我们还可以『修剪』标签,因此,在我们这里, 我们修剪底部标签,这会使它消失,所以现在不会有任何文本重叠。 我们仍然可能打算修剪ax2的顶部标签,但这里是我们目前为止的源代码:

当前的源码:

  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)
  32. plt.ylabel('Price')
  33. ax3 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1)
  34. plt.ylabel('MAvgs')
  35. stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
  36. source_code = urllib.request.urlopen(stock_price_url).read().decode()
  37. stock_data = []
  38. split_source = source_code.split('\n')
  39. for line in split_source:
  40. split_line = line.split(',')
  41. if len(split_line) == 6:
  42. if 'values' not in line and 'labels' not in line:
  43. stock_data.append(line)
  44. date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,
  45. delimiter=',',
  46. unpack=True,
  47. converters={0: bytespdate2num('%Y%m%d')})
  48. x = 0
  49. y = len(date)
  50. ohlc = []
  51. while x < y:
  52. append_me = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x]
  53. ohlc.append(append_me)
  54. x+=1
  55. ma1 = moving_average(closep,MA1)
  56. ma2 = moving_average(closep,MA2)
  57. start = len(date[MA2-1:])
  58. h_l = list(map(high_minus_low, highp, lowp))
  59. ax1.plot_date(date,h_l,'-')
  60. ax1.yaxis.set_major_locator(mticker.MaxNLocator(nbins=5, prune='lower'))
  61. candlestick_ohlc(ax2, ohlc, width=0.4, colorup='#77d879', colordown='#db3f3f')
  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]+4, 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. ax3.plot(date[-start:], ma1[-start:], linewidth=1)
  79. ax3.plot(date[-start:], ma2[-start:], linewidth=1)
  80. ax3.fill_between(date[-start:], ma2[-start:], ma1[-start:],
  81. where=(ma1[-start:] < ma2[-start:]),
  82. facecolor='r', edgecolor='r', alpha=0.5)
  83. ax3.fill_between(date[-start:], ma2[-start:], ma1[-start:],
  84. where=(ma1[-start:] > ma2[-start:]),
  85. facecolor='g', edgecolor='g', alpha=0.5)
  86. ax3.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
  87. ax3.xaxis.set_major_locator(mticker.MaxNLocator(10))
  88. for label in ax3.xaxis.get_ticklabels():
  89. label.set_rotation(45)
  90. plt.setp(ax1.get_xticklabels(), visible=False)
  91. plt.setp(ax2.get_xticklabels(), visible=False)
  92. plt.subplots_adjust(left=0.11, bottom=0.24, right=0.90, top=0.90, wspace=0.2, hspace=0)
  93. plt.show()
  94. graph_data('EBAY')

二十二、自定义填充、修剪和清除 - 图1

看起来好了一些,但是仍然有一些东西需要清除。