图形的边框基本上是图形的边界,其中有刻度线等东西。为了改变边框的颜色,你可以做一些类似这样的事情:

    在这里,我们引用了我们的边框字典,表示我们要调整左边框,然后我们使用方法将颜色设置为'c',它是青色。

    1. ax1.spines['right'].set_visible(False)
    2. ax1.spines['top'].set_visible(False)

    这是非常类似的代码,删除了右边框和上边框。

    很难看到我们修改了左边框的颜色,所以让我们通过修改线宽来使它变得很明显:

    1. ax1.tick_params(axis='x', colors='#f06215')

    现在我们的日期是橙色了! 接下来,让我们来看看我们如何绘制一条水平线。 你当然可以将你创建的一组新数据绘制成一条水平线,但你不需要这样做。 你可以:

    所以在这里,我们的整个代码是:

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. import urllib
    4. import datetime as dt
    5. import matplotlib.dates as mdates
    6. def bytespdate2num(fmt, encoding='utf-8'):
    7. strconverter = mdates.strpdate2num(fmt)
    8. def bytesconverter(b):
    9. s = b.decode(encoding)
    10. return strconverter(s)
    11. return bytesconverter
    12. def graph_data(stock):
    13. fig = plt.figure()
    14. ax1 = plt.subplot2grid((1,1), (0,0))
    15. stock_data = []
    16. split_source = source_code.split('\n')
    17. for line in split_source:
    18. split_line = line.split(',')
    19. if len(split_line) == 6:
    20. if 'values' not in line and 'labels' not in line:
    21. stock_data.append(line)
    22. date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,
    23. delimiter=',',
    24. unpack=True,
    25. converters={0: bytespdate2num('%Y%m%d')})
    26. ax1.plot_date(date, closep,'-', label='Price')
    27. ax1.plot([],[],linewidth=5, label='loss', color='r',alpha=0.5)
    28. ax1.plot([],[],linewidth=5, label='gain', color='g',alpha=0.5)
    29. ax1.axhline(closep[0], color='k', linewidth=5)
    30. ax1.fill_between(date, closep, closep[0],where=(closep > closep[0]), facecolor='g', alpha=0.5)
    31. ax1.fill_between(date, closep, closep[0],where=(closep < closep[0]), facecolor='r', alpha=0.5)
    32. for label in ax1.xaxis.get_ticklabels():
    33. #ax1.xaxis.label.set_color('c')
    34. #ax1.yaxis.label.set_color('r')
    35. ax1.set_yticks([0,25,50,75])
    36. ax1.spines['left'].set_color('c')
    37. ax1.spines['right'].set_visible(False)
    38. ax1.spines['top'].set_visible(False)
    39. ax1.spines['left'].set_linewidth(5)
    40. ax1.tick_params(axis='x', colors='#f06215')
    41. plt.xlabel('Date')
    42. plt.ylabel('Price')
    43. plt.title(stock)
    44. plt.legend()
    45. plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
    46. plt.show()