MATLAB: Manually enter legend details

manual entry in legend

I have 9 curves on a plot. 3 different colors x 3 (solid lines, dotted lines etc). However Instead of crowding my legend with 9 entries, I'd like to depict each color – their meaning (example red – 30-70Hz, blue – 20-50hz etc) and separately the meaning of dotted lines (training set), solid lines (test set) etc. How do I do this in Matlab ?

Best Answer

  • You could put the information in a different area not using legend()
  • You could plot 6 additional lines with nan or inf as their coordinates, and using the bare style (e.g., just red with no dotting, or just solid with color not one of the three). Capture the handles of those and pass those to legend() so that only those lines would be legend()'d. With the nan or inf coordinates the lines will not be displayed, but they will exist enough for legend to be able to work with them. You would not legend() any of the real plot lines, because if you did that then legend() would want to display the combination style for them, color with spacing both.
Related Question