displaying correct marker type using text command

Hi, I am trying to plot 12 different markers for a long series of monthly reocord and wish to display the legend using 'text' command so I don't have a separate line of legend for each month. Instead the marker used for a month stays constant across all years. However, am not able to display a 'square' in text command despite using marker 's' in the plot and similarly for other markers. Please help. I tried using interpreter as latex and a '$' sign but got nothing. Here is a sample reproducible ex. I tried on Matlab 2015a.
Thanks in advance.
% sample code starts
temp=rand(14);
color_vector=zeros(14,3);
color_vector(13,:)=[1 0 0];
color_vector(14,:)=[1 0 0];
MarkerSet='so^d*+hvp><xso' % repeats after 12th entry
Present_year=2018
x_1=1.1;
x_2=1.5;
x_3=1.8;
y_top=2;
next_row_rel_loc=0.2;
font_val=10;
hold on
for ii = 1 : 3
plot(temp(ii,1:3),'marker',MarkerSet(ii),'markersize',10,'markeredgecolor',color_vector(ii,:),'LineStyle','none','linewidth',1)
end %ii
ylim([-2 2])
text(x_1,y_top, 'Month','FontSize',font_val) %Curr Year Prev Year')
text(x_2,y_top, num2str(Present_year),'FontSize',font_val)
text(x_3, y_top, 'Prev. Yr','FontSize',font_val)
text(x_1,y_top-next_row_rel_loc, 'Jan','FontSize',font_val)
text(x_2, y_top-next_row_rel_loc, '\color{red} s','FontSize',font_val)
text(x_3, y_top-next_row_rel_loc, '\color{black} s','FontSize',font_val)
text(x_1,y_top-next_row_rel_loc*2, 'Feb','FontSize',font_val)
text(x_2, y_top-next_row_rel_loc*2, '\color{red} o','FontSize',font_val)
text(x_3, y_top-next_row_rel_loc*2, '\color{black} o','FontSize',font_val)%,'interpreter', 'latex'
%end of sample code

 Accepted Answer

"wish to display the legend using 'text' command so I don't have a separate line of legend for each month"
That is not the approach to use. Instead, construct a series of line() objects with x and y both nan, and with the appropriate color and symbol choice for the markers. Record the handle of each of those line objects into a single vector. Then legend() passing that vector of handles as the first parameter, and appropriate legend entries in the other parameter(s).
Because of the nan x and y data, the line objects will not appear on the display, but they will exist and legend() can display entries for them.

3 Comments

thanks for posting the sample. I used one of your older responses on the website and for now have created this code (in matlab 2017)
If someone could suggest a way to extend this code to display a red color 'symbol' signifying present yr and a black color 'symbol' on same row signifying prev. yr points that would be great:
hold on
for ii = 1 : 3
plot(temp(ii,1:3),'marker',MarkerSet(ii),'markersize',10,'markeredgecolor',color_vector(ii,:),'LineStyle','none','linewidth',1)
end %ii
ylim([-2 2])
LH(1) = plot(nan, nan, 'blacks');
L{1} = 'Jan';
LH(2) = plot(nan, nan, 'blacko');
L{2} = 'Feb';
LH(3) = plot(nan, nan, 'black^');
L{3} = 'Mar';
% .... so on for other months followed by
lgd = legend(LH, L);
legend_title_text=sprintf('\\color{red}%d \\color{black}and \\color{black}Prev Yrs',Present_year);
title(lgd, legend_title_text)
Unfortunately, there is no tex or latex for the symbols -- at least not that is supported by the installed latex packages.
Ok, Thanks. If my requirement changes, I will ask a fresh question.

Sign in to comment.

More Answers (1)

What exactly is your question?
I am not able to display a 'square' in text command despite using marker 's' in the plot
Correct. There is no square character mentioned in doc text. This means, that you cannot use a TeX or LaTeX command to draw a square.
text(0.5, 0.5, '\square', 'InterPreter', 'TeX')
Warning: Error updating Text.
Character vector must have valid interpreter syntax:
\square
If you want a square, use the plot command and a marker instead of simulating it by a text. What is the problem with using legend?

3 Comments

\square requires LaTex AMS package, which is not included in MATLAB's LaTex. There is also \Square which is wasyssym package, also not included.
Hi Jan, I understand but as mentioned have a long series of points, legend gives each point a separate row whereas I am trying to categorize things into current year and prev year. Can I do this using legend? please guide.
Example:
plot(rand(1,20), 'b*-');
hold on
plot(rand(1,20).^2, 'b^-');
plot(randn(1,20), 'k*-');
plot(randn(1,20).^2, 'k^-');
L(1) = plot(nan, nan, 'b-');
L(2) = plot(nan, nan, 'k-');
legend(L, {'2016', '2017'})

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!