Help with axes labels on histogram plot

조회 수: 24 (최근 30일)
Nadeau Hahne
Nadeau Hahne 2023년 11월 6일
답변: Steven Lord 2023년 11월 6일
Hello,
I am simply trying to add labels to this histogram under the the middle of the columns and I am unable to figure out how to add for the other two columns.
histogram(F)
title('Distribution of Traction Forces')
xticks(0.0135)
xticklabels("0.149")
yticks(1:5)
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 11월 6일
"... I am unable to figure out how to add for the other two columns."
Supply the values for the other two columns as well, along with the first one.

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

채택된 답변

Image Analyst
Image Analyst 2023년 11월 6일
You're manually telling it to do just one tick label. Let it do it automatically and you'll get them. Get rid of the calls to xticks and xticklabels.
F = [.149, .194, .298*ones(1, 5), 3*.149];
histogram(F)
title('Distribution of Traction Forces')
xlabel('Value');
ylabel('Count');

추가 답변 (1개)

Steven Lord
Steven Lord 2023년 11월 6일
If you want the labels to be only in the center of the bars, use the BinEdges property of the histogram to compute where the ticks should be. I'll make a histogram with 17 bins (so the math doesn't work out quite so nicely.)
x = randn(1, 1e5);
n = 17;
h = histogram(x, n);
This has n+1 (in this case, 18) edges.
E = h.BinEdges
E = 1×18
-4.5000 -3.9500 -3.4000 -2.8500 -2.3000 -1.7500 -1.2000 -0.6500 -0.1000 0.4500 1.0000 1.5500 2.1000 2.6500 3.2000 3.7500 4.3000 4.8500
Use diff to compute the width of each bin (or for uniform bins, like this histogram has, use the BinWidth property.)
W = h.BinWidth % or
W = 0.5500
W = diff(E)
W = 1×17
0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500 0.5500
Each bin center is the left edge plus half the bin width. But the last bin edge isn't involved at this step; we don't need the "center" that's past the right edge of the histogram.
C = E(1:end-1) + W/2
C = 1×17
-4.2250 -3.6750 -3.1250 -2.5750 -2.0250 -1.4750 -0.9250 -0.3750 0.1750 0.7250 1.2750 1.8250 2.3750 2.9250 3.4750 4.0250 4.5750
Now set those ticks.
xticks(C)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by