简体   繁体   中英

How to put labels on each data points in stem plot using matlab

so this is my x and y data:

x = [29.745, 61.77, 42.57,  70.049, 108.51, 93.1,   135.47, 52.79,  77.91,  116.7,  100.71, 146.37, 125.53]
y = [6, 6, 12,  24, 24, 12, 24, 8,  24, 24, 24, 48, 8]

stem(x,y);

so i want to label each data point on my stem plot, this i want output i want: 在此处输入图片说明

i edit it using paint, can matlab do this vertical labeling? just what the image look like? please help.

Yes it can! You just need to provide the rotation property of text annotations with a value of 90 and it works fine.

Example:

clear
clc


x = [29.745, 61.77, 42.57,  70.049, 108.51, 93.1,   135.47, 52.79,  77.91,  116.7,  100.71, 146.37, 125.53]
y = [6, 6, 12,  24, 24, 12, 24, 8,  24, 24, 24, 48, 8]

hStem = stem(x,y);

%// Create labels.
Labels = {'none'; 'true';'false';'mean';'none';'';'true';'hints';'high';'low';'peas';'far';'mid'}

%// Get position of each stem 'bar'. Sorry I don't know how to name them.
X_data = get(hStem, 'XData');
Y_data = get(hStem, 'YData');

%// Assign labels.
for labelID = 1 : numel(X_data)
   text(X_data(labelID), Y_data(labelID) + 3, Labels{labelID}, 'HorizontalAlignment', 'center','rotation',90);
end

Which gives the following:

在此处输入图片说明

The last label is a bit high so you might want to rescale the axes, but you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM