Home > Software engineering >  Adding Vertical Line styles to a matplotlib legend to a plot made from a dataframe
Adding Vertical Line styles to a matplotlib legend to a plot made from a dataframe

Time:07-21

I am looking to add a label that indicates that the blue vertical dashed lines represent long entry points and that black vertical dashed lines represent short entry points.

The other two lines (benchmark and manual strategy portfolio) came from the dataframe. How do I add a legend for the two vertical line styles?

Here is my existing code and the corresponding graph. The dataframe is a two column dataframe of values that share date indices (the x) and have y values. The blue_x_coords and black_x_coords are the date indices for the vertical lines, as you would expect. Thanks in advance!

    ax = df.plot(title=title, fontsize=12, color=["tab:purple", "tab:red"])
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    for xc in blue_x_coords:
        plt.axvline(x=xc, color="blue", linestyle="dashed", label="Long Entry points")
    for xc in black_x_coords:
        plt.axvline(x=xc, color="black", linestyle="dashed", label="Short Entry points")
    plt.savefig('./images/'   filename)
    plt.clf()

enter image description here

CodePudding user response:

You can just do plt.legend() before plt.show() but here you need to use enter image description here

  • Related