画图
画长方体网格中的数据分布图
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