I have a chart with 4 elements: blue square,blue triangle, red square and red triangle, I’m tryng to add these 4 items to the legend. when I add a polygon patch to the legend, it is displayed as a rectangle, even though the patch is of a triangle How can I get it to work?
JavaScript
x
3
1
legend_elements = [patches.RegularPolygon((4, 4), 3, 0.5, np.pi / 4,label="Triangle")]
2
plt.legend(handles=legend_elements)
3
Advertisement
Answer
You could use line markers to indicate the polygons:
JavaScript
1
10
10
1
from matplotlib import pyplot as plt
2
from matplotlib.lines import Line2D
3
4
handles = [Line2D([0], [0], linestyle='none', mfc='blue', mec='blue', marker='s', label='blue square'),
5
Line2D([0], [0], linestyle='none', mfc='blue', mec='blue', marker='^', label='blue triangle'),
6
Line2D([0], [0], linestyle='none', mfc='red', mec='red', marker='s', label='red square'),
7
Line2D([0], [0], linestyle='none', mfc='red', mec='red', marker='^', label='red triangle')]
8
plt.legend(handles=handles)
9
plt.show()
10