Add spoke labels in glyphplot
Afficher commentaires plus anciens
I am using glyplot to create star plots to visualize several statistical metrics of performance for a model. However, I cannot figure out how to add labels to each individual spoke of the star plot. I have used gca to get all of the axes properties, but have been unable to find any successful way to add spoke labels. I see another user asked this same question a number of years ago, but it was never answered satisfactorily (https://www.mathworks.com/matlabcentral/answers/345558-how-can-i-label-the-individual-spokes-in-a-glyph-plot). Can anyone help?
3 commentaires
To be clear, is your goal to label each line segment or each of the vertices?
Also, you mentioned that it's a start plot which is helpful. I just want to confirm that it is indeed a starplot and not a face plot which is also an option with the glyphplot.
Lastly, do you have a vector of labels or do you just want to label them numerically?
Susanne Craig
le 22 Juin 2020
Réponses (1)
h = glyphplot(X,...) returns the handle to all line and text objects created within the function. For a star plot, h(:,1) and h(:,2) contain handles to the line objects for each star's perimeter and spokes, respectively.
From the line handles, you can get the endpoint coordinates. The endpoint coordinates can be used to compute the midpoint of each line segment.
This demo shows how to label the midpoints of each spoke with numbers 1:n and the midpoint of each perimeter segment with letters 'a' to 'z'. You can adapt the demo to use your own labels.
The demo uses labelpoints() from the file exchange but that function could be replaced with Matlab's text() function if needed.
See the inline comments for important details.
% Load a built-in Matlab dataset
load carsmall
% Produce a glyphplot; save the output handle 'h' !
X = [Acceleration Displacement Horsepower MPG Weight];
h = glyphplot(X,'standardize','column','obslabels',Model,'grid',[2 2],...
'page','scroll');
% Get the handle to axes using the glyphplot output handle
ax = h(1).Parent;
% Hold the axes if needed.
hold(ax, 'on')
% These two lines are not necessary but they will add axis ticks
% which may be helpful when troubleshooting.
ax.XTickMode = 'auto';
ax.YTickMode = 'auto';
% The perimeter labels will be letters a:z (note, this demo only supports
% glyphs that have less than 27 perimeter segments.
perimeterLabels = num2cell(('a':'z')')'; % only supports up to 26 perimeter labels
% Loop through each glyph handle
for i = 1:size(h,1)
% Compute and label the midpoint of each spoke
idx = 1:3:numel(h(i,2).XData);
xCnt = sum([h(i,2).XData(idx);h(i,2).XData(idx+1)],1)/2;
yCnt = sum([h(i,2).YData(idx);h(i,2).YData(idx+1)],1)/2;
labelpoints(xCnt, yCnt, 1:numel(xCnt), 'Center', 'Fontsize', 6, 'axHand', ax); % From file exchange
% Compute and label the midpoint of each perimeter segment
idx = 1:numel(h(i,1).XData)-1;
xCnt = sum([h(i,1).XData(idx);h(i,1).XData(idx+1)],1)/2;
yCnt = sum([h(i,1).YData(idx);h(i,1).YData(idx+1)],1)/2;
labelpoints(xCnt, yCnt, perimeterLabels(1:numel(xCnt)), 'Center', 'Fontsize', 6, 'axHand', ax); % From file exchange
end

To use Matlab's text() function instead of labelpoints,
text(ax, xCnt, yCnt, LABEL_ARRAY, 'Fontsize', 6, ...
'HorizontalAlignment', 'Center', 'VerticalAlignment','Middle');
Catégories
En savoir plus sur Axis Labels dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!