## 3-D histogram in spherical coordinates

on 2 May 2011

### Teja Muppirala (view profile)

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.

## Products

No products are associated with this question.

### Teja Muppirala (view profile)

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.

Andrew Newell

on 2 May 2011

Nice work, Teja!

### Patrick Kalita (view profile)

on 2 May 2011

Have you tried looking at bar3?

Anitha S D

### Anitha S D (view profile)

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.

#### Join the 15-year community celebration.

Play games and win prizes!

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