Match name to axis value in chart
    2 views (last 30 days)
  
       Show older comments
    
 load('matlab_pl.mat')
stem(pl(:,2))
 ff= find(pl(:,2));
  ax = gca;
ax.XTickLabel=pl(ff,1);
hi, i see that the name axis not corrisponding on axis value <>0

and if possibile to avoid overlapping I would like to write the number under its own bar as in the example
0 Comments
Accepted Answer
  Star Strider
      
      
 on 16 Mar 2025
        Apparently, you want to put all the labels in the negative part of the y-axis.    
Try this — 
load('matlab_pl.mat')
stem(pl(:,2))
ff= find(pl(:,2));
ax = gca;
% ax.XTickLabel=pl(ff,1);
ylim('padded')
xval = ff;
tval = pl(ff,1);
yval = pl(ff,2);
lval = zeros(size(yval));                                                                               % Label Positions
lval(yval<0) = yval(yval<0);                                                                            % Label Positions
lval(yval>0) = yval(yval>0)-max(yval(yval>0));                                                          % Label Positions
text(xval, lval-0.02, compose('%d',tval), Horiz='center', Vert='top', FontSize=9, FontWeight='bold')
Make appropriate changes to get the result you want.  
.
0 Comments
More Answers (1)
  Image Analyst
      
      
 on 16 Mar 2025
        How about this:
load('matlab_pl.mat')
x = pl(:, 1);
y = pl(:, 2);
stem(x, y, 'Filled', 'LineWidth', 2);
grid on;
xticklabels("")
bigStemIndexes = find(y ~= 0);
for k = 1 : length(bigStemIndexes)
    xs = x(bigStemIndexes(k));
    ys = y(bigStemIndexes(k));
    if ys > 0
        str = sprintf('%d\n', xs);
    	text(xs, ys, str, ...
    		'HorizontalAlignment','center',...
    		'VerticalAlignment','bottom', 'FontWeight','bold')
    else
        str = sprintf('\n%d', xs);
    	text(xs, ys, str, ...
    		'HorizontalAlignment','center',...
    		'VerticalAlignment','top', 'FontWeight','bold')
    end
end
0 Comments
See Also
Categories
				Find more on Annotations in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

