MATLAB代码实现多组柱状图
时间: 2023-08-31 12:12:40 浏览: 42
好的,以下是MATLAB代码实现多组柱状图的示例:
```matlab
% 创建数据
data = [10 20 30; 40 50 60; 70 80 90];
labels = {'Group 1', 'Group 2', 'Group 3'};
categories = {'Category 1', 'Category 2', 'Category 3'};
% 绘制柱状图
b = bar(data);
% 设置颜色
b(1).FaceColor = 'red';
b(2).FaceColor = 'blue';
b(3).FaceColor = 'green';
% 添加标签和标题
xlabel('Categories');
ylabel('Data');
set(gca, 'XTickLabels', categories);
legend(labels);
title('Multiple Grouped Bar Chart');
```
这段代码可以绘制三组数据在同一个柱状图中,并且按照不同的颜色进行区分。通过修改`data`、`labels`和`categories`变量的值,可以绘制不同的柱状图。
相关问题
Bilinear Interpolation Adaptive Histogram Equalization matlab代码实现
以下是Bilinear Interpolation Adaptive Histogram Equalization的Matlab代码实现:
```matlab
function [outImg] = bilinearAdapthisteq(inImg, numTiles, clipLimit)
% Bilinear Interpolation Adaptive Histogram Equalization
% inImg: input image
% numTiles: number of tiles (default: 8x8)
% clipLimit: clipping limit (default: 0.01)
% outImg: output image
if nargin < 2
numTiles = [8 8];
end
if nargin < 3
clipLimit = 0.01;
end
inImg = double(inImg);
[h, w] = size(inImg);
tileH = floor(h / numTiles(1));
tileW = floor(w / numTiles(2));
outImg = zeros(h, w);
for i = 1:numTiles(1)
rowStart = (i - 1) * tileH + 1;
rowEnd = min(rowStart + tileH - 1, h);
rowSize = rowEnd - rowStart + 1;
for j = 1:numTiles(2)
colStart = (j - 1) * tileW + 1;
colEnd = min(colStart + tileW - 1, w);
colSize = colEnd - colStart + 1;
tile = inImg(rowStart:rowEnd, colStart:colEnd);
% histogram equalization
tileHist = histcounts(tile(:), 256);
tileHistCum = cumsum(tileHist) / sum(tileHist);
tileHistNew = max(0, floor(tileHistCum(tile(:)) * 255));
% clip histogram
tileHistNewClip = tileHistNew;
tileHistNewClip(tileHistNewClip > clipLimit * rowSize * colSize) = clipLimit * rowSize * colSize;
% bilinear interpolation
tileBilinear = interp2((0:255)', repmat([0 255], 256, 1), [tileHistNewClip; tileHistNewClip], tile, 'linear');
tileBilinear(isnan(tileBilinear)) = 0;
outImg(rowStart:rowEnd, colStart:colEnd) = tileBilinear;
end
end
outImg = uint8(outImg);
```
该函数的输入为原始图像、瓦片数量和剪切限制。输出为进行Bilinear Interpolation Adaptive Histogram Equalization后的图像。在实现中,将原始图像分成多个瓦片,对每个瓦片进行直方图均衡化。然后,通过双线性插值将直方图映射应用于每个像素。最后,将所有瓦片重新组合为一幅图像。
matlab柱状图
在MATLAB中绘制柱状图可以使用 `bar` 函数。
以下是一个简单的示例代码:
```matlab
x = [1, 2, 3, 4, 5]; % 横坐标数据
y = [10, 20, 30, 25, 15]; % 纵坐标数据
bar(x, y); % 绘制柱状图
xlabel('X'); % 设置 x 轴标签
ylabel('Y'); % 设置 y 轴标签
title('Bar Plot'); % 设置图表标题
```
运行该代码,将生成一个简单的柱状图。
如果需要添加更多的数据系列,可以使用 `hold on` 和 `hold off` 函数来实现。例如:
```matlab
x = [1, 2, 3, 4, 5]; % 横坐标数据
y1 = [10, 20, 30, 25, 15]; % 第一组纵坐标数据
y2 = [20, 25, 15, 30, 10]; % 第二组纵坐标数据
bar(x, y1); % 绘制第一组柱状图
hold on; % 保持绘图
bar(x, y2); % 绘制第二组柱状图
hold off; % 取消保持绘图
xlabel('X'); % 设置 x 轴标签
ylabel('Y'); % 设置 y 轴标签
title('Bar Plot'); % 设置图表标题
legend('Group 1', 'Group 2'); % 添加图例
```
运行该代码,将生成两组数据的柱状图,并添加了图例。