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:
- Convert the Cartesian force components into spherical coordinates.
- Generate a 2D histogram to represent angular distributions.
- Visualize the distribution by drawing patches in 3D space.
- Apply colors to the patches based on the normalized count within each bin.
- 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:
- https://www.mathworks.com/help/matlab/ref/atan2.html
- https://www.mathworks.com/help/matlab/ref/acos.html
- https://www.mathworks.com/help/matlab/ref/histcounts2.html
- https://www.mathworks.com/help/matlab/ref/sph2cart.html
I hope this helps!