GMT's mysterious page dimension

| categories: matlab, octave | View Comments

GMT has been around for some time now. Their "world domination map" is a nice demonstration of its scripting capabilities and quality rendering. They only suffer from one disease. Well actually two.

  1. GMT doesn't allow gradual step-by-step fine tuning. While in matlab/octave you would use "plot(x,y)" just to see what happens, and afterwards play with the axis limits until you are satisfied, and so on - in GMT you have to worry about axis and the physical figure width and height from the very beginning. Quite a barrier I would say. That's where octgmt might come handy. It's an interface between octave and GMT that will create for you an initial script.
  2. When you try to use the package, you are likely to just produce blank pages. That's because of the heavy dependence on some default printing page size, which is hidden from the user. Your plot should just fit in this mysterious default. This was actually discussed in their mailgroup . Anyhow, you probably want to try the flag "–PAPER_MEDIA=Custom_550x580" or "–PS_MEDIA=Custom_550x580" (depending on the GMT version).

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

org-mode source

Read and Post Comments

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

create a document from your figures

| categories: latex, workflow | View Comments

A part of the scientific workflow is creating images and categorizing them into directories. In our little parties, we scientist show these images to each other and brag about our ability to create more. It is therefore very useful to have bundles of these in pdf or html files (depending on the kind of party).

Here's how to create a pdf (using LaTeX) :

1: #!/usr/bin/perl -nw 
2: ## syntax : ls fig_patterns | latexfigs.pl > latexfile
3: chomp();
4: print "\\begin\{figure\}\n\\centering\n\\includegraphics\[scale=1.2,angle=0\]\{$_\}\n";
5: s/_/\\_/g;
6: print "\\caption\{$_\}\n\\end\{figure\}\n\\clearpage\n";

and Here's how to create a html :

1: #!/usr/bin/perl -nw 
2: ## syntax : ls fig_patterns | htmlfigs.pl > htmlfile
3: chomp();
4: print "<IMG src=\"$_\" width=650><BR>\n";
5: print "$_<BR><BR>\n";

After some time, you may want to make a section in your book/paper from each directory.

here's the LaTeX version :

1: #!/usr/bin/perl -w 
2: ## syntax : anchor_latex.pl "tag" "text" >> file.latex
3: $tag=shift or die "syntax error: anchor_latex.pl \"tag\" \"text\">>file.latex\n";
4: $text=shift or die "syntax error: anchor_latex.pl \"tag\" \"text\">>file.latex\n";
5: print "\\section{$text}\\label{sec:$tag}\n";

and here's the html :

1: #!/usr/bin/perl -w 
2: ## syntax : anchor_html.pl "tag" "text" >> file.html
3: $tag=shift or die "syntax error: anchor_html.pl \"tag\" \"text\">>file.html\n";
4: $text=shift or die "syntax error: anchor_html.pl \"tag\" \"text\">>file.html\n";
5: print "<a id=\"$tag\"><h2>$text</h2></a>\n";

you can include a template for a latexfile in your home directory :

 1: \documentclass[A4paper]{article}
 2: \usepackage{graphicx}
 3: \usepackage{cite}
 4: \usepackage{placeins} % floatbarrier definition
 5: \usepackage[caption=false]{subfig}
 6: \usepackage{fullpage}
 7: \newcommand{\unit}[1]{\ensuremath{\, \mathrm{#1}}}
 8: \begin{document}
 9: TEXT
10: \end{document}

and substitute your created latex code into the TEXT part, using perl again :

 1: #!/usr/bin/perl -w 
 2: # syntax : merge_latex_tmpl.pl tmpfile > merged_file.tex
 3: $tmplfilename=$ENV{'LATEXTMPL'};
 4: $filename=shift // die "syntax error";
 5: open TMPL,"<$tmplfilename" // die "could not find the template file";
 6: open FILE,"<$filename" // die "could not find the file $filename";
 7: $uniq_content = join("", <FILE>); 
 8: while(<TMPL>){
 9:     if(/TEXT/){
10:         print $uniq_content;
11:     }else{
12:         print;
13:     }
14: }

where LATEXTMPL is an environment variable, telling your script the location of your template. I like templates, and I clutter quite a bit as hidden files in my home directory. Do you do it differently ?

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 -- Next Page »