I am trying to create a spherical histogram, but my data points are latitude and longitude values so I need to be able to color a given cell region of a sphere based on its position, rather than just randomly going through indices. I have received a similar answer to my problem of certain azimuth and elevations being skipped here: Coloring sections of sphere, some regions end up with unassigned colors, however this has a blank sliver for one azimuth section and also the ordering is not as ordered as expected.
Here is the previous solution code for reference:
n = 20;
[x,y,z] = sphere(n);
r = 1;
surf(r.*x,r.*y,r.*z,'FaceColor', 'white', 'FaceAlpha',0); %// Base sphere
hold on;
for countAz = 1:1:n
for countE = 1:1:n
% Linear index for a 2x2 patch of the matrices
idx = countAz + [0,1,n+(1:2)] + (countE-1)*n;
% Pull out the coordinates, reshape for surf
x2 = x(idx); x2 = reshape(x2,2,2);
y2 = y(idx); y2 = reshape(y2,2,2);
z2 = z(idx); z2 = reshape(z2,2,2);
random_color = rand(1,3);
surf(r*x2,r*y2,r*z2,'FaceColor',random_color, 'FaceAlpha',1);
end
end
Using the solution code from the linked post and holding the countAz to be constant and only the elevation to change, results in this:
And vice versa, holding the CountE constant and only allowing azimuth to change:
It seems only allowing the azimuth to change and holding elevation constant instead has the opposite effect and goes through to color every elevation level of a given azmiuth index, and when holding the azimuth constant, there is a spiral effect even though theoretically the azimuth should not be changing.
Is there a way of going through the elevation and azimuth ranges in a more systematic way that doesn't result in cells being left blank? Being able to loop through one elevation range for every cell bounded by two azimuth values, and then continuing on down to the next elevation level is what I have in mind, but currently the index system just seems to not follow the expected order causing the spiraling or changing the elevation when it should be constant, making it hard to assign inputted latitude and longitude values to corresponding azimuth and elevation values for each inner for loop.