3-D histogram in spherical coordinates
7 views (last 30 days)
Show older comments
I would like to plot a 3D histogram from data in spherical coordinate system. I have (theta, phi, h) each of which is of size 36* 9. Is there any way to plot 3D histogram in spherical coordinate system. My theta varies from 0 :2*pi:36 and phi varies from 0:pi/2:9. Using surf, I am getting it plotted as points only.
0 Comments
Accepted Answer
Teja Muppirala
on 2 May 2011
Just for fun, and because it makes for a great example of visualizing spherical data,
here is a script to display a spherical coordinate histogram (here the histogram data is H):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Set up the figure and data
colordef(figure,'black');
theta_vec = linspace(0,2*pi,36);
phi_vec = linspace(0,pi/2,9);
[theta,phi] = meshgrid(theta_vec,phi_vec);
% H is your histogram data
%H = 1+(theta.*(sin(phi))); %Another example
H = 100*(rand(size(phi)));
Hmax = max(H(:));
r = 0.03*Hmax; %Box size
polar(nan,max(max(H.*cos(phi))));
hold all;
%%Make the Histogram
for kk = 1:numel(theta_vec);
for jj = 1:numel(phi_vec);
X=r*([0 1 1 0 0 0;1 1 0 0 1 1;1 1 0 0 1 1;0 1 1 0 0 0]-0.5);
Y=r*([0 0 1 1 0 0;0 1 1 0 0 0;0 1 1 0 1 1;0 0 1 1 1 1]-0.5);
Z=[0 0 0 0 0 1;0 0 0 0 0 1;1 1 1 1 0 1;1 1 1 1 0 1]*H(jj,kk);
h= patch(X,Y,Z,0*X+H(jj,kk),'edgecolor','none');
rotate(h,[0 0 1],45,[0 0 0]);
rotate(h,[0 1 0],90 - 180/pi*phi_vec(jj),[0 0 0]);
rotate(h,[0 0 1],180/pi*theta_vec(kk),[0 0 0]);
end;
end;
%%Adjust the plot
[Xs,Ys,Zs] = sphere(size(theta,2)+1);
hs = surf(Hmax*Xs,Hmax*Ys,Hmax*Zs);
set(hs,'facecolor','none','edgecolor','w','edgealpha',0.2)
camlight;
set(gca,{'xtick' 'ytick' 'ztick' 'vis' 'clim'},{[] [] [] 'on' [0 Hmax]});
axis equal vis3d;
box on;
view(3);
colorbar
drawnow;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
There is some room for improvement here, but feel free to use/modify this if you'd like.
More Answers (1)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!