matlab discrete colorbar

| categories: matlab, octave | View Comments

You shouldn't use the default "smooth" gradients of imagesc. The aesthetic side set aside, imagesc gives the wrong impression of an infinite resolution. While we are at it, let's get rid of the default nonesense y-axis opposite direction, and enable masking (whoaa. crazy.).

verbose_disp is my version of disp/sprintf, combining the goods of both of them. You can safely remove these lines, or make up your own version of "verbosity" function.

  1: % purpose : interface for imagesc for producing good 
  2: % heat maps
  3: % syntax : myimagesc(x,y,w,minval,maxval,bin,[mask],[flag_verbose])
  4: % x,y = vectors, representing the range of x and y axis.
  5: % w = typically, a  field which depends on both x and y.
  6: % minval, maxval = first and last values of w  which are color coded.
  7: % values of w which go beyond minval and maxval will be represented by the
  8: % same colorcoding like minval and maxval
  9: % bin - an interval of values of w which has a unique color coding
 10: % mask - binary matrix for pixels that myimagesc grays out
 11: % flag_verbose - work in verbose mode
 12: % 
 13: % dependencies : verbose_disp
 14: % 
 15: % see also: imagesc
 16: 
 17: 
 18: % Copyright 2012 Avi Gozolchiani (http://tiny.cc/avigoz)
 19: % This program is free software: you can redistribute it and/or modify
 20: % it under the terms of the GNU General Public License as published by
 21: % the Free Software Foundation, either version 3 of the License, or
 22: % (at your option) any later version.
 23: %
 24: % This program is distributed in the hope that it will be useful,
 25: % but WITHOUT ANY WARRANTY; without even the implied warranty of
 26: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 27: % GNU General Public License for more details.
 28: %
 29: % You should have received a copy of the GNU General Public License
 30: % along with this program.  If not, see <http://www.gnu.org/licenses/>.
 31: 
 32: % $Log$
 33: function h=myimagesc(x,y,w,minval,maxval,bin,varargin)
 34: %test case
 35: % bin=1e4;minval=-8e4;maxval=8e4;
 36: % x=0.1:0.1:100;
 37: % y=0:200;
 38: % [xx,yy]=ndgrid(x,y);
 39: % w=sin(xx).*xx.^2.*exp(0.01*yy);
 40: % w(:,195:198)=-70000;
 41: % w(15:18,:)=70000;
 42: % myimagesc(x,y,w,minval,maxval,bin)
 43: accur=1e-6; % to avoid roundoff problems when interpolating the colormap
 44: %% check the inputs
 45: if(~isempty(nargchk(6,8,nargin)))
 46:   error('esyntax : myimagesc(x,y,w,minval,maxval,bin,[mask],[flag_verbose])');
 47: end
 48: nxx=length(x);
 49: nyy=length(y);
 50: [nxw,nyw]=size(w);
 51: if(nxx~=nxw || nyy~=nyw)
 52:     error('dimension mismatch');
 53: end % if(nxx~=nxw || nyy~=nyw)
 54: if(minval>=maxval)
 55:     error('minval>=maxval');
 56: end % if(minval>=maxval)
 57: if(bin>=(maxval-minval)/2.0)
 58:     error('color range spans less than two colors');
 59: end % if(bin>=(maxval-minval)/2.0)
 60: mask=zeros(nxx,nyy);
 61: flag_mask=false;
 62: if(nargin>6)
 63:     flag_mask=true;
 64:     mask=varargin{1};
 65:     if(~all([nxx,nyy]==size(mask)))
 66:         error('mask dimension does not match the other matrices');
 67:     end % if(~all([nxx,nyy]==size(mask)))
 68: end % if(nargin>6)
 69: %% parameters
 70: flag_verbos=false;
 71: if(nargin==8)
 72:     flag_verbos=varargin{2};
 73: end % if(nargin==8)
 74: accur=1e-5;
 75: %% prepare colormap
 76: verbose_disp(flag_verbos,'myimagesc : prepare colormap');
 77: colormap('default');
 78: cmap=colormap;
 79: n_origbins=size(cmap,1);
 80: W_bins=minval:bin:maxval;
 81: W_bins_ext=(minval-bin/2):bin:(maxval+(1+accur)*bin/2);
 82: cscal=(minval:(maxval-minval)/(n_origbins-1):maxval)';
 83: newcmap=interp1(cscal,cmap,W_bins);
 84: newcmap=fix((newcmap-0.5)/accur-sign(newcmap))*accur+0.5; %take care that we don't get out of the [0,1] range
 85: colormap(newcmap);
 86: %% plot the heat map
 87: verbose_disp(flag_verbos,'myimagesc : plot the heat map');
 88: h=imagesc(x,y,w',W_bins_ext([1 length(W_bins_ext)]));hh=colorbar;grid on;
 89: set(hh,'ytick',W_bins);
 90: %% plot the mask, if necessary
 91: verbose_disp(flag_verbos,'myimagesc : plot the mask, if necessary');
 92: if(flag_mask)
 93:     % see
 94:     % http://blogs.mathworks.com/steve/2009/02/18/image-overlay-using-transparency/
 95:     % for details
 96:     gray_lev=0.5*ones(size(w')); % gray level for masking
 97:     hold on;
 98:     gray=cat(3,gray_lev,gray_lev,gray_lev);
 99:     hh=imagesc(x,y,gray);
100:     hold off;
101:     set(hh,'alphadata',~mask');
102: end % if(flag_mask)
103: %% invert the y-axis
104: verbose_disp(flag_verbos,'myimagesc : invert the y-axis');
105: set(gca,'YDir','normal');

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

matlab subplots packed densely together

| categories: xmgr, matlab, octave, gmt | View Comments

During significant portion of my grad-school I had to travel a lot. I therefore couldn't rely on hooking to the university matlab license, so I searched for free alternatives. While you could use a lot of good projects, such as octave to crunch your numbers, it seems to me that making beautiful figures is not so easy.

I got used to plotting with the excellent packages of GMT . I even wrote some little octave wrapper functions around GMT, since it's easy to get intimidated by their too elaborate man pages. You could still try them out, though GMT has been working lately on an official matlab API for you (they do have mature API for c++/Fortran). I didn't try it myself, yet. Another option is good old xmgr . Both of them produce great imagery, but they have their limitations at times.

The one point where matlab excels is better set of default parameters. You don't have to worry so much about the line thicknesses, page width etc., as much as in the other options mentioned above. The cons side, obviously, is when you don't want the defaults. Easy things like packing your subplots close are not so easy in matlab. It's of course, nevertheless, still possible. Mainly with axis/plot handles.

Here's how you do it : first let's create our figs:

1: nsubs=3;
2: for isub=1:nsubs
3:  subplot(nsubs,1,isub);
4:  plot(rands(3,100)');
5:  set(gca,'fontsize',16);
6: end % for i=1:nsubs
7: savefigs('nopack_subplots','save demo of packed graphs',[]);

that's the result:

Figure 1: before

now , lets pack them:

 1: packing_const=0.06
 2: for isub=1:nsubs
 3:  h=subplot(nsubs,1,isub);p = get(h, 'pos');
 4:  if(isub<nsubs)
 5:   set(gca,'fontsize',16,'XTickLabelMode', 'Manual','XTickLabel', []);
 6:  else %  if(isub<nsubs)
 7:   set(gca,'fontsize',16);
 8:  end %  if(isub<nsubs) ... else ...
 9:  set(h,'pos',[p(1) p(2) p(3) p(4)+packing_const]);
10: end % for isub=1:nsubs
11: savefigs('pack_subplots','save demo of packed graphs',[]);

that's our "after" exhibit :

Figure 2: after

The "savefigs" function is non standard. Its aim is to save images in fig/eps/png formats at once, and generate README file and a mat file on the fly, with consistent names.

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

ncdump -h for matlab

| categories: matlab, octave | View Comments

I launch "ncdump -h" many times during my workflow. It gives you all the meta-data you need for netcdf files, without the hassle of opening a more serious program like ferret . I figured out that I need the same for mat files. You will need octave to make it work…

1: #!/usr/local/bin/octave -q
2: whos('-file',argv(){1})

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

« Previous Page