How to plot 3d version of rose plot?

43 visualizzazioni (ultimi 30 giorni)
Sahil Wani
Sahil Wani il 15 Nov 2024 alle 19:14
Risposto: Abhas il 16 Nov 2024 alle 5:59
I have a data.mat file where the first column represents indices, the second column contains force values in the x-direction, the third column contains force values in the y-direction, and the fourth column contains force values in the z-direction. I need to create a 3D rose plot similar to the one shown in the attached file 3dplot.png.
for 2D i can make the rose plot as shown below:
load('data.mat');
force_x = data(:, 2); % Force in x-direction
force_z = data(:, 4); % Force in z-direction
x = force_x;
z = force_z;
theta_rad = atan2(z, x);
theta_deg = rad2deg(theta_rad);
theta_deg = mod(theta_deg, 360);
disp('Theta');
disp(theta_deg);
figure;
rose(theta_rad);

Risposta accettata

Abhas
Abhas il 16 Nov 2024 alle 5:59
To create a 3D rose plot in MATLAB, you need to calculate spherical coordinates and create a custom visualization. You can follow the below steps to generate a 3D rose plot:
  1. Convert the Cartesian force components into spherical coordinates.
  2. Generate a 2D histogram to represent angular distributions.
  3. Visualize the distribution by drawing patches in 3D space.
  4. Apply colors to the patches based on the normalized count within each bin.
  5. Ensure the color scale aligns with your example (ranging from 0.05 to 0.40).
Below is the MATLAB code to guide you in creating this visualization:
% Load the data
load('data.mat');
% Extract force components
force_x = data(:, 2);
force_y = data(:, 3);
force_z = data(:, 4);
% Calculate spherical coordinates
r = sqrt(force_x.^2 + force_y.^2 + force_z.^2);
theta = atan2(force_y, force_x);
phi = acos(force_z ./ r);
[X,Y,Z] = sphere(50);
figure('Color', 'white');
hold on;
% Number of bins for discretization
n_theta = 20;
n_phi = 10;
% Create histogram bins
theta_edges = linspace(-pi, pi, n_theta+1);
phi_edges = linspace(0, pi, n_phi+1);
N = histcounts2(theta, phi, theta_edges, phi_edges);
N = N / max(N(:));
% Create the 3D rose plot
for i = 1:n_theta
for j = 1:n_phi
if N(i,j) > 0
% Calculate center points of each bin
theta_center = (theta_edges(i) + theta_edges(i+1))/2;
phi_center = (phi_edges(j) + phi_edges(j+1))/2;
% Calculate the vertices for this patch
dtheta = (theta_edges(i+1) - theta_edges(i));
dphi = (phi_edges(j+1) - phi_edges(j));
% Create patch vertices
theta_patch = [theta_center-dtheta/2, theta_center+dtheta/2, ...
theta_center+dtheta/2, theta_center-dtheta/2];
phi_patch = [phi_center-dphi/2, phi_center-dphi/2, ...
phi_center+dphi/2, phi_center+dphi/2];
scale = N(i,j);
[x, y, z] = sph2cart(theta_patch, pi/2-phi_patch, scale);
patch(x, y, z, scale, 'EdgeColor', 'none');
end
end
end
% Customize the plot
colormap(jet);
colorbar;
caxis([0 0.4]); % Match the color scale in your example
axis equal;
grid on;
xlabel('X');
ylabel('Y');
zlabel('Z');
view(45, 30); % Adjust view angle
title('3D Force Distribution Rose Plot');
You may refer to the below MathWorks documentation links to know more about the spherical co-ordinates conversion:
  1. https://www.mathworks.com/help/matlab/ref/atan2.html
  2. https://www.mathworks.com/help/matlab/ref/acos.html
  3. https://www.mathworks.com/help/matlab/ref/histcounts2.html
  4. https://www.mathworks.com/help/matlab/ref/sph2cart.html
I hope this helps!

Più risposte (0)

Categorie

Scopri di più su Data Distribution Plots in Help Center e File Exchange

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by