MATLAB 2012B: The use of pcolor in combination with colorbar

30 views (last 30 days)
Hi all,
I am trying to make a nice figure with pcolor in combination with colorbar. Running the code shown below results in a figure with way too many tick labels (multiple and double overlapping) on the x-axis and colorbar. Is there a nice solution for this? I have to make quite a number of these graphs (100+). Any idea is welcome!
Best regards, Simon
The code (please try to run it, the result is quite astonishing):
minu = -6;
maxu = 6;
U = 2*(rand(1,1000)+1);
V = 6*rand(1,1000);
Z = 2*(rand(1,1000)-0.5);
Urange = linspace(min(U),max(U),100);
Vrange = linspace(min(V),max(V),100);
[UI,VI] = meshgrid(Urange,Vrange);
ZI = griddata(U,V,Z,UI,VI,'cubic');
h = pcolor(UI,VI,ZI);
set(h, 'EdgeColor', 'none');
hcb = colorbar;
colormap(hsv(256));
caxis([minu, maxu]);
colormap(flipud(colormap));

Accepted Answer

Image Analyst
Image Analyst on 6 Feb 2013
First of all, I'm not sure why you're using pcolor() instead of imshow() or image(). Do you know that you lose a row and column of your data if you do that? And your data point is actually the place where the lines (which you turned off) cross, not the whole colored pixel/square/tile like you'd think, though it seems that way if you use "flat" shading (if you don't use flat shading, you'll probably be very surprised). So that's why I never use pcolor().
Also, I get 7 colorbar labels and 9 axis marks. You can use set(gca, 'XTick',.... to set up whatever tick marks you want.
Try running my code to see how it looks with imshow(). You'll get all your data (no missing row and column) and you'll get full range of the colormap.
minu = -6;
maxu = 6;
U = 2*(rand(1,1000)+1);
V = 6*rand(1,1000);
Z = 2*(rand(1,1000)-0.5);
Urange = linspace(min(U),max(U),100);
Vrange = linspace(min(V),max(V),100);
[UI,VI] = meshgrid(Urange,Vrange);
ZI = griddata(U,V,Z,UI,VI,'cubic');
h = pcolor(UI,VI,ZI);
set(h, 'EdgeColor', 'none');
hcb = colorbar;
colormap(hsv(256));
caxis([minu, maxu]);
colormap(flipud(colormap));
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Code added by Image Analyst
figure;
imshow(ZI, [], 'InitialMagnification', 800);
colormap(jet(256));
colorbar;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
  4 Comments
Stuart Huston
Stuart Huston on 25 Feb 2013
The advantage of using pcolor() is that the x and y values don't have to be uniformly spaced.
I used to be able to add colorbars after using pcolor() in previous versions, but it doesn't seem to work in R2012a.

Sign in to comment.

More Answers (3)

Andrew Krygier
Andrew Krygier on 7 Mar 2013
The solution has to do with the renderer as discussed here:
If you use the command:
set(gcf, 'renderer', 'zbuffer');
you should get a better result.
There are more suggestions in the link.

Stuart Huston
Stuart Huston on 25 Feb 2013
Try the following snippet in the editor:
generate some random x, y, and z values
nx=15;
ny=15;
x=rand(nx,1);
x=sort(x);
y=rand(ny,1);
y=sort(y);
z=rand(length(x),length(y));
% plot using pcolor()
figure
pcolor(x,y,z')
shading flat
hcb = colorbar('location','EastOutside');
now try changing the color axis
caxis([.2 .8])
On my system this gives a proper colorbar. However, now try changing nx to, say 25. In this case changing the color axis produces the spurious labels.

Nir Dahan
Nir Dahan on 31 Aug 2015
I have noticed that simply defining colorbar location solves the problem!
h = pcolor(UI,VI,ZI);
set(h, 'EdgeColor', 'none');
hcb = colorbar('location','eastoutside');

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!