plottools.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:finance_news_analysis 作者: pskun 项目源码 文件源码
def plot_date_line_and_vline(
        line_values,
        line_dates,
        vline_dates = [],
        line_label = [],
        line_color = []):
    ''' line_values could be 1d or nd arrays '''
    if len(line_label) == 0:
        if len(line_values.shape) == 1:
            line_label = ['line']
        else:
            line_label_coll = ['line1', 'line2', 'line3', 'line4', 'line5', 'line6', 'line7', 'line8']
            line_label = line_label_coll[0:line_values.shape[1]]

    if len(line_color) == 0:
        if len(line_values.shape) == 1:
            line_color = ['CornflowerBlue']
        else:
            line_color_coll = ['Blue', 'Green', 'Red', 'DarkTurquoise', 'Chocolate', 'CadetBlue', 'IndianRed', 'Orange']
            line_color = line_color_coll[0:line_values.shape[1]]

    line_dtdates = [get_datetime_date(x) for x in line_dates]
    vline_dtdates = [get_datetime_date(x) for x in vline_dates]
    (fig, ax) = plt.subplots()
    if len(line_values.shape) == 1:
        ax.plot_date(line_dtdates, line_values, '-', label = line_label[0], color = line_color[0])
    else:
        for i in xrange(line_values.shape[1]):
            ax.plot_date(line_dtdates, line_values[:, i], '-', label = line_label[i], color = line_color[i])
    for vldate in vline_dtdates:
        ax.axvline(vldate)
    ax.xaxis.set_major_formatter(plt_dates.DateFormatter('%Y-%m-%d'))
    def ydata(y): return '$%1.2f'%y
    ax.fmt_xdata = plt_dates.DateFormatter('%Y-%m-%d')
    ax.fmt_ydata = ydata
    ax.grid(True)
    # show
    fig.autofmt_xdate()
    plt.legend()
    plt.show()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号