How to make a colored markers legend from scratch

Following the example given in the legend guide, you can use a Line2D object instead of a marker object.

The only difference to the example given in the guide is you want to set linestyle="None"

import matplotlib.lines as mlines
import matplotlib.pyplot as plt

blue_star = mlines.Line2D([], [], color="blue", marker="*", linestyle="None",
                          markersize=10, label="Blue stars")
red_square = mlines.Line2D([], [], color="red", marker="s", linestyle="None",
                          markersize=10, label="Red squares")
purple_triangle = mlines.Line2D([], [], color="purple", marker="^", linestyle="None",
                          markersize=10, label="Purple triangles")

plt.legend(handles=[blue_star, red_square, purple_triangle])

plt.show()

enter image description here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)