Plotting speed data in 3D (Cube)

5 visualizaciones (últimos 30 días)
Keith Hay
Keith Hay el 10 de Ag. de 2022
Respondida: Divyam el 9 de Oct. de 2024
I've got data for the speed with which air flows through different locations in a box. I began by a simple code to graph speed as follows.
clc
clear all
D = readtable("CubePlot","VariableNamingRule","preserve");
Df = table2array(D);
for i=1:1:length(Df)
t=Df(i,1);
for j=2:5;
speed=Df(i,j);
plot(t,speed,'.')
drawnow
hold on
end
end
This plots all the air speeds at time t, followed by all the air speeds at time t+1 and so on. Ive got several hundreds of data points so instantaneous comparison with this graph is not reasonable.
How can I represent this data in a cube with different locations displaying their respective speeds with passing time?
  2 comentarios
Karim
Karim el 10 de Ag. de 2022
At first glance you have 2D data? One dimension is time and the other is speed?
To make a bit more clear you could plot the curves, and not only the data points:
D = readtable("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1093160/CubePlot.xlsx","VariableNamingRule","preserve");
Df = table2array(D);
figure
plot(Df(:,1), Df(:,2:end))
grid on
xlabel('t')
ylabel('speed')
However, if you want to seperate them and plot them as 3D curves, you could create a space (see below). However i'm not sure if this makes it easier to interpret.
figure
hold on
for i = 2:6
Space = i * 10 * ones(size(Df(:,1)));
plot3(Df(:,1), Space,Df(:,i))
end
hold off
grid on
view(3)
xlabel('t')
zlabel('speed')
Keith Hay
Keith Hay el 10 de Ag. de 2022
Editada: Keith Hay el 10 de Ag. de 2022
My bad. I shouldve been more clear about what I meant with the cube.
Say I have an the xyz origin axis at the center of a cube such that it divides the cube into 8 different sections as below.
Say I want j = 2:5 in the front facing quadrant, one in the center of (-,-,+), one in the center of (+,-,+), one in the center of (-,-,-) and finally one in the center of (+,-,-). My previous code was graphing all these speeds at the same time. However, I want to change that to displaying the actual numbers in the desired location. As in when t=2, j is 0.46 m/s and that is displayed as a number, in the center of (-,-,+) and so forth.

Iniciar sesión para comentar.

Respuestas (1)

Divyam
Divyam el 9 de Oct. de 2024
The "patch" function can be used to plot the 3D cube with appropriate dimensions and by using the "fill3" function you can add color to the planes identifying different axis.
The "text" function can then be used to display the data at the required positions while looping through the table. For constantly updating the plot in time, use the "drawnow" function. The "pause" function can be used to make the time data readable in real time.
% Read the data from the table
D = readtable("CubePlot", "VariableNamingRule", "preserve");
Df = table2array(D);
% Defining the positions of the points in the cube
positions = [
-0.5, -0.5, 0.5; % Center of (-,-,+)
0.5, -0.5, 0.5; % Center of (+,-,+)
-0.5, -0.5, -0.5; % Center of (-,-,-)
0.5, -0.5, -0.5 % Center of (+,-,-)
];
% Creating a figure for the plot
figure;
hold on;
axis equal;
xlim([-1, 1]);
ylim([-1, 1]);
zlim([-1, 1]);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Air Speed Visualization in a 3D Cube');
% Plotting the cube using patch
vertices = [-1, -1, -1; 1, -1, -1; 1, 1, -1; -1, 1, -1; -1, -1, 1; 1, -1, 1; 1, 1, 1; -1, 1, 1];
faces = [
1, 2, 3, 4; % Bottom
5, 6, 7, 8; % Top
1, 2, 6, 5; % Front
2, 3, 7, 6; % Right
3, 4, 8, 7; % Back
4, 1, 5, 8 % Left
];
patch('Vertices', vertices, 'Faces', faces, 'FaceColor', 'none', 'EdgeColor', 'k');
% Add colored planes for axis identification
fill3([-1 1 1 -1], [-1 -1 1 1], [0 0 0 0], 'r');
fill3([0 0 0 0], [-1 1 1 -1], [-1 -1 1 1], 'g');
fill3([-1 1 1 -1], [0 0 0 0], [-1 -1 1 1], 'b');
% Loop over each time step
for i = 1:length(Df)
% Time t for iteration i
t = Df(i, 1);
% Clear previous text annotations
delete(findall(gcf, 'Type', 'text'));
% Looping over each location and plot the speed
for j = 2:5
speed = Df(i, j);
pos = positions(j-1, :);
% Displaying the speed as text at the location
text(pos(1), pos(2), pos(3), sprintf('%.2f', speed), 'HorizontalAlignment', 'center');
end
% Updating the plot
drawnow;
% Optional: Pause to simulate time progression
pause(0.5);
end
hold off;
A frame from the output figure
For more information regarding the "patch", "fill3", "text", "drawnow" and "pause" functions, refer to the following documentation links:

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by