<p>Here is an example using <a href="http://www.mathworks.com/help/matlab/ref/contourf.html" rel="nofollow">contourf</a>. The trick is in sampling the 3D arrays appropriately:</p>

<pre><code>    x=linspace(0,pi,20);
y=linspace(0,pi,10);
t=0:10:120;
[X3d,Y3d,T3d]=meshgrid(x,y,t);
[X2d,T2dx]=meshgrid(x,t);
[Y2d,T2dy]=meshgrid(y,t);

dat3d=7*sin(X3d)+5*cos(Y3d)+T3d/max(T3d(:))+rand(size(X3d));
dat2dx=7*sin(X2d)+5*cos(y(5))+T2dx/max(T2dx(:))+rand(size(X2d)); % at y(5)
dat2dy=7*sin(x(12))+5*cos(Y2d)+T2dy/max(T2dy(:))+rand(size(Y2d));  % at x(12)
%%
figure(1);clf
contourf(X2d,T2dx,dat2dx);
shading flat; colorbar
ylabel('Time (days)')
xlabel('X distance (km)')
%%
figure(2);clf
contourf(Y2d,T2dy,dat2dy);
shading flat; colorbar
ylabel('Time (days)')
xlabel('Y distance (km)')
%%
figure(3);clf
subplot(221)
contourf(squeeze(X3d(5,:,:)),squeeze(T3d(5,:,:)),squeeze(dat3d(5,:,:)));
shading flat; colorbar
ylabel('Time (days)'); xlabel('X distance (km)')
title('Hovmoller at y(5)')
subplot(222)
contourf(squeeze(Y3d(:,12,:)),squeeze(T3d(:,12,:)),squeeze(dat3d(:,12,:)));
shading flat; colorbar
ylabel('Time (days)'); xlabel('Y distance (km)')
title('Hovmoller at x(12)')
subplot(223)
contourf(squeeze(X3d(8,:,:)),squeeze(T3d(8,:,:)),squeeze(dat3d(8,:,:)));
shading flat; colorbar
ylabel('Time (days)'); xlabel('X distance (km)')
title('Hovmoller at y(8)')
subplot(224)
contourf(squeeze(Y3d(:,17,:)),squeeze(T3d(:,17,:)),squeeze(dat3d(:,17,:)));
shading flat; colorbar
ylabel('Time (days)'); xlabel('Y distance (km)')
title('Hovmoller at x(17)')
</code></pre>

CC BY-SA 3.0
2016-09-14T01:06:08.537Z
2016-09-14T01:43:01.747Z
2016-09-14T01:43:01.747Z
arkaia
<p>I haven't programmed in MATLAB in quite a while, but I remember doing some Hovmoller diagrams during my graduate studies. I just looked at my old code, and I used the function <code>contourf</code>, where my input was a 2D matrix of my data (row-wise was radius, column-wise was time).</p>

CC BY-SA 3.0
2016-09-13T19:14:01.847Z
2016-09-13T19:14:01.847Z
jgadoury
<p>Im trying to create a Hovmoller diagram in MATLAB but I'm very new to programming and have no clue what to do. Im trying to do it for vorticity. Below is the code that I have currently. Any input or advice, would be greatly appreciated.</p>

<pre><code>path(path,'/Users/zoleak/Documents/');
lev_name='700';%mb

disp(strcat('calculating mean fields...',lev_name,'mb'))

file1 = '/Users/zoleak/Documents/Datasets/Patricia/ERAi.T.00.2015.1p5.45N45S.nc';
file2 = '/Users/zoleak/Documents/Datasets/Patricia/ERAi.U.00.2015.1p5.45N45S.nc';
file3 =  '/Users/zoleak/Documents/Datasets/Patricia/ERAi.V.00.2015.1p5.45N45S.nc';
file4 =  '/Users/zoleak/Documents/Datasets/Patricia/ERAi.GEO.00.2015.1p5.45N45S.nc';
file5 =  '/Users/zoleak/Documents/Datasets/Patricia/ERAi.VORT.00.2015.1p5.45N45S.nc';
file6 =  '/Users/zoleak/Documents/Datasets/Patricia/ERAi.PV.00.2015.1p5.45N45S.nc';

%Read files in
lat=ncread(file1,'latitude');
lon=ncread(file1,'longitude');
levels=ncread(file1,'level');
u_data=ncread(file2,'u');
v_data=ncread(file3,'v');
vort_data=ncread(file5,'vo');

%Format Files
x=lon(28:121);
y=lat(1:31);
[nlons,nlats,ntimes]=size(vort_data);
u_data=u_data(28:121,1:31,4,:);
v_data=v_data(28:121,1:31,4,:);
vort_data=vort_data(28:121,1:31,4,:);

u_data=squeeze(squeeze(u_data));
v_data=squeeze(squeeze(v_data));
vort_data=squeeze(squeeze(vort_data));

%%

clf;

lat_avg=mean(vort_data(5:15,:,:),1);     

LONLIMS=[-139.5 -60];
LATLIMS=[0 30];
[lon_plot,lat_plot]=meshgrid(x,y);
proj('mercator','lon',LONLIMS,'lat',LATLIMS);
%m_quiver(lon_plot,lat_plot,u_data(:,:)',v_data(:,:)','LineStyle','-','Color',[0 0 0],'AutoScaleFactor',2);
hold on;
contourf(lon_plot,lat_plot,lat_avg(:,:)','LevelList',[-6e-4:1e-5:6e-4],'LineColor','none')
coast('patch',[1 1 1],'linewi',2);
grid('linewi',0.5,'tickdir','out','linest','none');
set(gca,'FontSize',14);
set(gcf,'Color','white');
hcb=colorbar('eastoutside');
title(['5N-20N,700mb Vorticity']);
set(hcb,'YTick',[-1e-4:1e-5:1e-4])
caxis([-.5e-4 .5e-4])
save_name1=strcat('/Users/zoleak/Documents/figures/HOVMOLLER/PAT_VORT');
print(save_name1,'dpdf'-r600);
</code></pre>

<meteorology><software><matlab><visualization>
How to plot a Hovmoller diagram in MATLAB?
NBE
# How to plot a Hovmoller diagram in MATLAB?

(Asked by: username_0 on 2016-06-23)

Im trying to create a Hovmoller diagram in MATLAB but I'm very new to programming and have no clue what to do. Im trying to do it for vorticity. Below is the code that I have currently. Any input or advice, would be greatly appreciated.

```
path(path,'/Users/zoleak/Documents/');
lev_name='700';%mb

disp(strcat('calculating mean fields...',lev_name,'mb'))

file1 = '/Users/zoleak/Documents/Datasets/Patricia/ERAi.T.00.2015.1p5.45N45S.nc';
file2 = '/Users/zoleak/Documents/Datasets/Patricia/ERAi.U.00.2015.1p5.45N45S.nc';
file3 =  '/Users/zoleak/Documents/Datasets/Patricia/ERAi.V.00.2015.1p5.45N45S.nc';
file4 =  '/Users/zoleak/Documents/Datasets/Patricia/ERAi.GEO.00.2015.1p5.45N45S.nc';
file5 =  '/Users/zoleak/Documents/Datasets/Patricia/ERAi.VORT.00.2015.1p5.45N45S.nc';
file6 =  '/Users/zoleak/Documents/Datasets/Patricia/ERAi.PV.00.2015.1p5.45N45S.nc';

%Read files in
lat=ncread(file1,'latitude');
lon=ncread(file1,'longitude');
levels=ncread(file1,'level');
u_data=ncread(file2,'u');
v_data=ncread(file3,'v');
vort_data=ncread(file5,'vo');

%Format Files
x=lon(28:121);
y=lat(1:31);
[nlons,nlats,ntimes]=size(vort_data);
u_data=u_data(28:121,1:31,4,:);
v_data=v_data(28:121,1:31,4,:);
vort_data=vort_data(28:121,1:31,4,:);

u_data=squeeze(squeeze(u_data));
v_data=squeeze(squeeze(v_data));
vort_data=squeeze(squeeze(vort_data));

%%

clf;

lat_avg=mean(vort_data(5:15,:,:),1);     

LONLIMS=[-139.5 -60];
LATLIMS=[0 30];
[lon_plot,lat_plot]=meshgrid(x,y);
proj('mercator','lon',LONLIMS,'lat',LATLIMS);
%m_quiver(lon_plot,lat_plot,u_data(:,:)',v_data(:,:)','LineStyle','-','Color',[0 0 0],'AutoScaleFactor',2);
hold on;
contourf(lon_plot,lat_plot,lat_avg(:,:)','LevelList',[-6e-4:1e-5:6e-4],'LineColor','none')
coast('patch',[1 1 1],'linewi',2);
grid('linewi',0.5,'tickdir','out','linest','none');
set(gca,'FontSize',14);
set(gcf,'Color','white');
hcb=colorbar('eastoutside');
title(['5N-20N,700mb Vorticity']);
set(hcb,'YTick',[-1e-4:1e-5:1e-4])
caxis([-.5e-4 .5e-4])
save_name1=strcat('/Users/zoleak/Documents/figures/HOVMOLLER/PAT_VORT');
print(save_name1,'dpdf'-r600);

```

**Tags:** meteorology, software, matlab, visualization


---

## 2 answers

### Answer by username_1 on 2016-09-14. Score: 2

Here is an example using [contourf](http://www.mathworks.com/help/matlab/ref/contourf.html). The trick is in sampling the 3D arrays appropriately:

```
    x=linspace(0,pi,20);
y=linspace(0,pi,10);
t=0:10:120;
[X3d,Y3d,T3d]=meshgrid(x,y,t);
[X2d,T2dx]=meshgrid(x,t);
[Y2d,T2dy]=meshgrid(y,t);

dat3d=7*sin(X3d)+5*cos(Y3d)+T3d/max(T3d(:))+rand(size(X3d));
dat2dx=7*sin(X2d)+5*cos(y(5))+T2dx/max(T2dx(:))+rand(size(X2d)); % at y(5)
dat2dy=7*sin(x(12))+5*cos(Y2d)+T2dy/max(T2dy(:))+rand(size(Y2d));  % at x(12)
%%
figure(1);clf
contourf(X2d,T2dx,dat2dx);
shading flat; colorbar
ylabel('Time (days)')
xlabel('X distance (km)')
%%
figure(2);clf
contourf(Y2d,T2dy,dat2dy);
shading flat; colorbar
ylabel('Time (days)')
xlabel('Y distance (km)')
%%
figure(3);clf
subplot(221)
contourf(squeeze(X3d(5,:,:)),squeeze(T3d(5,:,:)),squeeze(dat3d(5,:,:)));
shading flat; colorbar
ylabel('Time (days)'); xlabel('X distance (km)')
title('Hovmoller at y(5)')
subplot(222)
contourf(squeeze(Y3d(:,12,:)),squeeze(T3d(:,12,:)),squeeze(dat3d(:,12,:)));
shading flat; colorbar
ylabel('Time (days)'); xlabel('Y distance (km)')
title('Hovmoller at x(12)')
subplot(223)
contourf(squeeze(X3d(8,:,:)),squeeze(T3d(8,:,:)),squeeze(dat3d(8,:,:)));
shading flat; colorbar
ylabel('Time (days)'); xlabel('X distance (km)')
title('Hovmoller at y(8)')
subplot(224)
contourf(squeeze(Y3d(:,17,:)),squeeze(T3d(:,17,:)),squeeze(dat3d(:,17,:)));
shading flat; colorbar
ylabel('Time (days)'); xlabel('Y distance (km)')
title('Hovmoller at x(17)')

```


---

### Answer by username_2 on 2016-09-13. Score: 0

I haven't programmed in MATLAB in quite a while, but I remember doing some Hovmoller diagrams during my graduate studies. I just looked at my old code, and I used the function `contourf`, where my input was a 2D matrix of my data (row-wise was radius, column-wise was time).


---

