How can I use a custom marker for plot or alternatively add a text item in the legend?

조회 수: 20 (최근 30일)
Dear all,
I would like to add data points to my plot with a letter instead of the default markers that are available in Matlab. So far the only way I could get that working was to use
text(x,y,'A','FontSize',12,'Interpreter','latex')
but sadly this does not produce an entry in the legend. Therefore I'm in need of a solution for either adding the text item to the legend (so with the letter 'A' as icon) or for a way to change the marker of plot(x,y) to the letter 'A'.
By the way, the plot is likely to evolve so it would be good if it's really implemented with the use of legend and/or plot. I thought of adding some text as a "fake legend" but that sounds like a lot of work that would have to be done again every time the legend changes. Moreover, if I find a good solution I intend on using it on other plots in the future.
Many thanks in advance!
Julien.

답변 (1개)

Ameer Hamza
Ameer Hamza 2018년 5월 23일
편집: Ameer Hamza 2018년 5월 23일
One solution is to create an empty line i.e. the line will not appear on the screen but the handle still exists. Then you can give a legend to the empty line and it will appear as if the legend belongs to the text object. For example, try this
plot(1:10, 1:10);
ax = gca;
hold(ax);
text(1:10, 1:10, 'A', 'FontSize',12,'Interpreter','latex')
l = line(); % creating an empty line
l.XData = []; % remove its points from the axes
l.YData = [];
l.Color = [0 0 0];
legend({'axtual plot', 'text markers'});
The result will be like this
  댓글 수: 1
Julien Barrat
Julien Barrat 2018년 5월 23일
Hi and thank you for your answer. I thought about this solution, however I find it confusing to have a line in the legend instead of the letter, especially in the case where you have several plots on the same figure. For example there are instances where I might plot experimental data from different authors, and each dataset would be designated by the first letter of the last name of the author.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by