フィルターのクリア

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

31 ビュー (過去 30 日間)
Simon
Simon 2013 年 2 月 6 日
回答済み: Nir Dahan 2015 年 8 月 31 日
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));

採用された回答

Image Analyst
Image Analyst 2013 年 2 月 6 日
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 件のコメント
Simon
Simon 2013 年 2 月 6 日
Great, thank you!
Stuart Huston
Stuart Huston 2013 年 2 月 25 日
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.

サインインしてコメントする。

その他の回答 (3 件)

Andrew Krygier
Andrew Krygier 2013 年 3 月 7 日
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 2013 年 2 月 25 日
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 2015 年 8 月 31 日
I have noticed that simply defining colorbar location solves the problem!
h = pcolor(UI,VI,ZI);
set(h, 'EdgeColor', 'none');
hcb = colorbar('location','eastoutside');

カテゴリ

Help Center および File ExchangeBlue についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by