0

I want to create a custom scatter legend. I want it to show a single point in the legend, so I am using scatterpoints=1. However it still plots 2-points and a line between them.

from matplotlib.patches import Patch
from matplotlib.lines import Line2D
markers = ['^', 's']
legend_elements=[Line2D([0], [0], marker=m, label=str(t)) for m,t in zip(markers,[1,2])]
plt.legend(handles=legend_elements, scatterpoints=1)
plt.show()

This is the result I get.

enter image description here

I suspect that the problem is caused because I use Line2D, but I can't find another object to create the legend.

I am following instructions from: https://matplotlib.org/gallery/text_labels_and_annotations/custom_legends.html

Environment

  • CentOS 7
  • matplotlib 1.5.0
  • Python 2.7.11 :: Anaconda 2.4.1 (64-bit)
rph
  • 901
  • 1
  • 10
  • 26
  • @Mr.T I am using a much older version of matiplotlib, 1.5. :/ – rph Nov 25 '18 at 09:15
  • Have you tried `numpoints=1` instead of scatterpoints? Not sure what works with 1.5/2.7 though. – Mr. T Nov 25 '18 at 09:26
  • Oh yeah, that works. I had to add `linestyle=''` to the Line2D and `numpoints=1` to `plt.legend`. – rph Nov 25 '18 at 09:32

0 Answers0