MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Anitha S D on 2 May 2011

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.

*No products are associated with this question.*

Answer by Teja Muppirala on 2 May 2011

Accepted answer

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.

Answer by Patrick Kalita on 2 May 2011

Have you tried looking at bar3?

Anitha S D on 3 May 2011

Thanks a lot Teja. It was of really great help. I tried with my histogram data and with slight modifications it started giving me expected results. Thanks once again for your valuable time spend on this.

Hi Patrick, I tried with bar3, but it will not give me is spherical coordinate system even if we convert in to cartesian and try. Thanks.

## 0 Comments