clc;clear; %确定图片的位置和大小,[x y width height] figure('position',[150,100,750,650]); %准备数据 Z=[70,75,80,85;70,75,80,85;70,75,80,85;70,75,80,85]; %设置柱子颜色,颜色为RGB三原色,每个值在0~1之间即可 color_matrix = [100,255,150;50,152,65;129,211,176;108,99,117]/255; %画柱状图 h=bar3(Z,0.8); axis([-inf inf -inf inf 60 100]) for n=1:numel(h) cdata=get(h(n),'zdata'); set(h(n),'cdata',cdata,'facecolor',color_matrix(n,:)); end %设置小标签 set(gca,'yticklabel',{'A1','A2','A3','A4'},'Fontname','Times New Roman','FontSize',11); set(gca,'xticklabel',{'M_1','M_2','M_3','M_4'},'Fontname','Times New Roman','FontSize',11); %修改每个轴的标签 ylabel('标签Y','Fontname','宋体','FontSize',14); xlabel('标签X','Fontname','宋体','FontSize',14); zlabel('标签Z','Fontname','宋体','FontSize',14); legend({'M_1','M_2','M_3','M_4'}); 帮我把这段代码改成只有一行或一列的三维柱状图
时间: 2023-08-20 10:53:39 浏览: 127
position.m图像特征提取与选择-Matlab代码质心位置
要将代码改成只有一行或一列的三维柱状图,你可以使用`meshgrid`函数生成对应的X和Y坐标,并将Z转置成一行或一列。下面是修改后的代码:
```matlab
clc;clear;
%确定图片的位置和大小,[x y width height]
figure('position',[150,100,750,650]);
%准备数据
Z = [70,75,80,85; 70,75,80,85; 70,75,80,85; 70,75,80,85];
%设置柱子颜色,颜色为RGB三原色,每个值在0~1之间即可
color_matrix = [100,255,150; 50,152,65; 129,211,176; 108,99,117]/255;
%生成X和Y坐标
[x, y] = meshgrid(1:size(Z,2), 1:size(Z,1));
%转置Z成一行或一列
Z = Z(:)';
%画柱状图
h = bar3(x, y, Z);
axis([-inf inf -inf inf 60 100])
for n = 1:numel(h)
cdata = get(h(n), 'zdata');
set(h(n), 'cdata', cdata, 'facecolor', color_matrix(n,:));
end
%设置小标签
set(gca,'yticklabel', {'A1','A2','A3','A4'}, 'Fontname', 'Times New Roman', 'FontSize', 11);
set(gca,'xticklabel', {'M_1','M_2','M_3','M_4'}, 'Fontname', 'Times New Roman', 'FontSize', 11);
%修改每个轴的标签
ylabel('标签Y', 'Fontname', '宋体', 'FontSize', 14);
xlabel('标签X', 'Fontname', '宋体', 'FontSize', 14);
zlabel('标签Z', 'Fontname', '宋体', 'FontSize', 14);
legend({'M_1','M_2','M_3','M_4'});
```
这样修改后的代码将会生成只有一行或一列的三维柱状图。
阅读全文