hist函数生成的纵轴变为百分比MATLAB并显示value值
时间: 2023-12-24 14:02:07 浏览: 369
MATLAB.rar_MATLAB内置函数_matlab 内置函数_matlab函数大全
你可以使用MATLAB中的histogram函数来生成纵轴为百分比的直方图,并用text函数在每个直方图柱状体上显示对应的value值。具体步骤如下:
1. 使用histogram函数生成直方图,并设置'Normalization'为'probability'以将纵轴变为百分比。例如:
```
data = randn(1000,1);
histogram(data,'Normalization','probability');
```
2. 获取直方图中每个柱状体的高度和位置信息。例如:
```
h = histogram(data,'Normalization','probability');
heights = h.Values;
centers = h.BinEdges(1:end-1) + diff(h.BinEdges)/2;
```
3. 使用text函数在每个柱状体上显示对应的value值。例如:
```
for i = 1:numel(heights)
text(centers(i), heights(i), sprintf('%.2f%%', heights(i)*100), ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
end
```
完整代码如下:
```
data = randn(1000,1);
h = histogram(data,'Normalization','probability');
heights = h.Values;
centers = h.BinEdges(1:end-1) + diff(h.BinEdges)/2;
for i = 1:numel(heights)
text(centers(i), heights(i), sprintf('%.2f%%', heights(i)*100), ...
'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
end
```
阅读全文