How do I add the numbers on top of the bar graph?

조회 수: 3 (최근 30일)
Belle Dionido
Belle Dionido 2022년 4월 29일
편집: Belle Dionido 2022년 4월 29일
file = input("Please enter the name of the data file : ",'s');
[num,txt,raw] = xlsread(file);
amino_names = txt(3:end,1);
atomic_weights = [15.994; 12.01; 14; 32.065; 1.01];
amino_weights = num * atomic_weights;
amino_weights = round(amino_weights,2);
disp("**** The report is ready ****");
T = table(amino_names,amino_weights);
T.Properties.VariableNames = {'Amino Acid','Molecular Weight'};
disp(T)
bar(amino_weights)
ylabel("Molecular Weights")
title("Molecular weights for different amino acids")
xticks(1:length(amino_names))
xticklabels(amino_names)
xtickangle(90)
% How do I add the numbers on top of the bar graph?
% Thank you!

답변 (2개)

KSSV
KSSV 2022년 4월 29일
A = rand(3,1) ;
h = bar(A) ;
text(h.XData,h.YData+0.05,num2str(A)) ;
  댓글 수: 2
Belle Dionido
Belle Dionido 2022년 4월 29일
What are the h.XData, h.YData+0.05?
KSSV
KSSV 2022년 4월 29일
REad about text, it needs positions (x,y) to place the text you want. h.XData, h.YData are those positions which can be obtained from the plot. You can try it yourself.

댓글을 달려면 로그인하십시오.


Belle Dionido
Belle Dionido 2022년 4월 29일
%I replaced xticks(1:length(amino_names)) with:
text(1:length(amino_names),amino_weights,num2str(amino_weights, '%0.2f'),'HorizontalAlignment','center','VerticalAlignment','bottom');

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by