Effacer les filtres
Effacer les filtres

displaying correct marker type using text command

7 vues (au cours des 30 derniers jours)
Munish Sikka
Munish Sikka le 31 Mai 2018
Commenté : Munish Sikka le 1 Juin 2018
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

Réponse acceptée

Walter Roberson
Walter Roberson le 31 Mai 2018
"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 commentaires
Walter Roberson
Walter Roberson le 31 Mai 2018
Unfortunately, there is no tex or latex for the symbols -- at least not that is supported by the installed latex packages.
Munish Sikka
Munish Sikka le 1 Juin 2018
Ok, Thanks. If my requirement changes, I will ask a fresh question.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 31 Mai 2018
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 commentaires
Munish Sikka
Munish Sikka le 31 Mai 2018
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.
Walter Roberson
Walter Roberson le 31 Mai 2018
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'})

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by