画长方体网格中的数据分布图@Matlab

画图

画长方体网格中的数据分布图

Matlab代码

代码由Draw 函数draw_temperature_box 函数两部分组成。在我的项目里,数据data的size比较大,10 * 20 * 100 , 中间的3个for循环里面要计算很久。

%% Draw picture
%  Data is composed of three dimensions. Each data here represents the temperature in the rectangular grid.
%  The code here takes drawing the temperature distribution in the cube as an example.
%  If you don't have temperature data, randomly generate one. eg. data = rand(10,20,100)

function y = Draw( data )

figure;
hold on;

% Define the maximum temperature for colormap normalization
max_data = max(data(:));
min_data = min(data(:));

% Iterate through the 3D temperature array and draw each cube
for x = 1:size(data, 1)
    for y = 1:size(data, 2)
        for z = 1:size(data, 3)
            draw_temperature_box(x, y, z, data(x, y, z), max_data);
        end
    end
end

% Configure the view and axis
view(3);
axis equal;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Va Distribution in the 3D Grid');
colorbar('Ticks', linspace(0, 1, 11), 'TickLabels', linspace(min_data, max_data, 11));
function draw_temperature_box(x, y, z, temperature, max_temperature)
    % Normalize temperature to the range [0, 1]
    normalized_temperature = temperature / max_temperature;

    % Convert temperature to RGB color
    cmap = colormap('jet'); % You can choose another colormap, such as 'hot', 'cool', etc.
    color_idx = ceil(normalized_temperature * size(cmap, 1));
    color = cmap(color_idx, :);

    % Draw a cube at the specified position with the specified color
    cube_size = 1; % Adjust this value if you want to change the size of the cubes
    patch('Vertices', [x, y, z; x+cube_size, y, z; x+cube_size, y+cube_size, z; x, y+cube_size, z; ...
                      x, y, z+cube_size; x+cube_size, y, z+cube_size; x+cube_size, y+cube_size, z+cube_size; x, y+cube_size, z+cube_size], ...
          'Faces', [1, 2, 3, 4; 5, 6, 7, 8; 1, 2, 6, 5; 3, 4, 8, 7; 1, 4, 8, 5; 2, 3, 7, 6], ...
          'FaceColor', color, 'EdgeColor', 'k');
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在Matlab,可以通过使用网格函数来绘制长方体的三维网格图。网格函数的语法格式为mesh(x, y, z),其x、y和z分别是包含长方体边界顶点坐标的矩阵。 首先,我们需要定义长方体的边界顶点坐标。假设长方体的边长分别为a、b和c,则边界顶点坐标可以定义为如下矩阵: x = [0, 0, a, a, 0, 0, a, a; % 长方体底面的四个顶点坐标 0, 0, a, a, 0, 0, a, a; 0, 0, 0, 0, c, c, c, c; 0, a, a, 0, 0, a, a, 0]; y = [0, 0, 0, 0, b, b, b, b; % 长方体底面的四个顶点坐标 0, a, a, 0, 0, a, a, 0; 0, 0, b, b, 0, 0, b, b; c, c, c, c, c, c, c, c]; z = [0, b, b, 0, 0, b, b, 0; % 长方体底面的四个顶点坐标 c, c, c, c, c, c, c, c; 0, 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, b, b, b, b]; 接下来,我们可以通过mesh函数来绘制三维网格图: mesh(x, y, z); xlabel('X轴'); % 设置x轴标签 ylabel('Y轴'); % 设置y轴标签 zlabel('Z轴'); % 设置z轴标签 title('长方体的三维网格图'); % 设置图标题 运行这段代码后,我们就可以在Matlab的绘图窗口看到长方体的三维网格图,其x、y和z轴分别代表空间的三个坐标轴,长方体的边界顶点即为网格的拐点。 ### 回答2: 长方体三维网格图在MATLAB可以通过使用meshgrid和mesh函数来创建。首先,我们需要定义长方体的边界范围。假设长方体的边界是x轴范围为[0, 1],y轴范围为[0, 2],z轴范围为[0, 3]。 接下来,我们可以使用meshgrid函数创建网格点的坐标矩阵。通过创建x、y和z向量,然后使用meshgrid函数生成三维网格点的坐标矩阵。 ``` matlab x = linspace(0, 1, 10); % 在x轴上生成10个点,范围为[0, 1] y = linspace(0, 2, 20); % 在y轴上生成20个点,范围为[0, 2] z = linspace(0, 3, 30); % 在z轴上生成30个点,范围为[0, 3] [X, Y, Z] = meshgrid(x, y, z); % 生成网格点的坐标矩阵 ``` 现在,我们可以创建一个柱体的数据,即所有点在长方体内为1,其他点为0。 ``` matlab cylinder_data = zeros(size(X)); % 创建与网格大小相同的全零矩阵 in_cylinder = (X >= 0.2 & X <= 0.5 & Y >= 0.6 & Y <= 1.2 & Z >= 1 & Z <= 2); % 长方体内的点为真,否则为假 cylinder_data(in_cylinder) = 1; % 将长方体内的点值设为1 ``` 最后,我们可以使用mesh函数将网格图显示出来。 ``` matlab figure; mesh(X, Y, Z, cylinder_data); xlabel('X'); ylabel('Y'); zlabel('Z'); title('长方体三维网格图'); ``` 以上步骤生成了一个包含长方体的三维网格图,x轴范围为[0, 1],y轴范围为[0, 2],z轴范围为[0, 3],网格内部的柱体显示为实心,其他部分为空白。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值