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

18 Ansichten (letzte 30 Tage)
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.

Antworten (1)

Ameer Hamza
Ameer Hamza am 23 Mai 2018
Bearbeitet: Ameer Hamza am 23 Mai 2018
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 Kommentar
Julien Barrat
Julien Barrat am 23 Mai 2018
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.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by