Rice University logo
 
Top blue bar image
 

Archive for August, 2012


Two images generated using latex packages tikz and pgfplots

August 26th, 2012 by wy1

The following images were generated by tikz and pgfplots scripts in latex.

  

The Latex source file is:

\documentclass{article}

\usepackage{tikz, pgfplots}
\usepgfplotslibrary{external}
\tikzexternalize[prefix=figures/]

\begin{document}

\thispagestyle{empty}

\tikzsetnextfilename{L1_ball_fig} % general PDF image L1_ball_fig.pdf
\begin{tikzpicture}[scale=0.5]
   \begin{axis}[hide axis, view={125}{25}, axis equal=true]

        % 3 axes
        \addplot3[thin,dotted,->] coordinates {(-2.4,0,0) (3.0,0,0)};
        \addplot3[thin,dotted,->] coordinates {(0,-2.4,0) (0,3.0,0)};
        \addplot3[thin,dotted,->] coordinates {(0,0,-2.4) (0,0,3.0)};

        % L1 ball boundaries
        \addplot3[thin,domain=0:4/3, samples y=0] (x,0,4/3-x);
        \addplot3[thin,domain=0:4/3, samples y=0] (x,0,x-4/3);

        \addplot3[domain=0:4/3, samples y=0] (0,x,4/3-x);
        \addplot3[domain=0:4/3, samples y=0] (0,x,x-4/3);

        \addplot3[domain=-4/3:0, samples y=0] (x,0,4/3+x);
        \addplot3[dashed, domain=-4/3:0, samples y=0] (x,0,-x-4/3);

        \addplot3[domain=-4/3:0, samples y=0] (0,x,4/3+x);
        \addplot3[dashed, domain=-4/3:0, samples y=0] (0,x,-x-4/3);

        % horizontal lines
        \addplot3[domain=0:4/3, samples y=0] (4/3-x,x,0);
        \addplot3[domain=0:4/3, samples y=0] (x-4/3,x,0);
        \addplot3[dashed, domain=-4/3:0, samples y=0] (-4/3-x,x,0);
        \addplot3[domain=-4/3:0, samples y=0] (4/3+x,x,0);

        % Ax = b
        \addplot3[red,domain=-1.6:3.0, samples y=0] ({(2-x)*1/3},{-(2-x)*1/3},x);

    	% intersection points
    	\addplot3[only marks, mark size=2pt, blue] coordinates{(0,0,2)} node [left] {$\mathbf{x}^o$};
    	\addplot3[only marks, mark size=2pt] coordinates{(2/3,-2/3,0)};
   \end{axis}
\end{tikzpicture}

\tikzsetnextfilename{L05_ball_fig} % general PDF image L05_ball_fig.pdf
\begin{tikzpicture}[scale=0.5]
%   \begin{axis}[xmin=-2, xmax=3, ymin=-2, ymax=3, zmin=-2, zmax=3, hide axis, view={135}{35}, plot box ratio=1 1 2]
   \begin{axis}[hide axis, view={125}{25}, axis equal=true]

        % 3 axes
        \addplot3[thin,dotted,->] coordinates {(-2.2,0,0) (2.8,0,0)};
        \addplot3[thin,dotted,->] coordinates {(0,-2.2,0) (0,2.8,0)};
        \addplot3[thin,dotted,->] coordinates {(0,0,-2.2) (0,0,2.8)};

        % Ax = b
        \addplot3[red,domain=-1.6:3.0, samples y=0] ({(2-x)*1/3},{-(2-x)*1/3},x);

        % L_1/2 ball boundaries
        \addplot3[domain=0:4/2, samples y=0] (x,0,{(sqrt(4/2)-sqrt(x))^2});
        \addplot3[domain=0:4/2, samples y=0] (x,0,-{(sqrt(4/2)-sqrt(x))^2});

        \addplot3[domain=0:4/2, samples y=0] (0,x,{(sqrt(4/2)-sqrt(x))^2});
        \addplot3[domain=0:4/2, samples y=0] (0,x,-{(sqrt(4/2)-sqrt(x))^2});

        \addplot3[domain=-4/2:0, samples y=0] (x,0,{(sqrt(4/2)-sqrt(-x))^2});
        \addplot3[dashed, domain=-4/2:0, samples y=0] (x,0,-{(sqrt(4/2)-sqrt(-x))^2});

        \addplot3[domain=-4/2:0, samples y=0] (0,x,{(sqrt(4/2)-sqrt(-x))^2});
        \addplot3[dashed, domain=-4/2:0, samples y=0] (0,x,-{(sqrt(4/2)-sqrt(-x))^2});

        % connection lines
        \addplot3[domain=0:4/2, samples y=0] ({(sqrt(4/2)-sqrt(x))^2},x,0);
        \addplot3[domain=0:4/2, samples y=0] (-{(sqrt(4/2)-sqrt(x))^2},x,0);
        \addplot3[dashed, domain=-4/2:0, samples y=0] (-{(sqrt(4/2)-sqrt(-x))^2},x,0);
        \addplot3[domain=-4/2:0, samples y=0] ({(sqrt(4/2)-sqrt(-x))^2},x,0);

    	% intersection points
    	\addplot3[only marks, mark size=2pt, blue] coordinates{(0,0,2)} node [left] {$\mathbf{x}^o$};

   \end{axis}
\end{tikzpicture}

\end{document}

To typeset this latex, I used pdflatex with the switch -shell-escape (or -enable-write18 in MikTex). I also used the tools in this blog to crop the white space around the plots.

How to crop a pdf file (and convert it to an eps file)

August 26th, 2012 by wy1

We are going to remove the margin (white space) around a figure in input.pdf. We do this in Linux (I do not find free tools in Windows yet). We need

  1. input.pdf a single-page image file
  2. pdfcrop executable (get from http://sourceforge.net/projects/pdfcrop/)
  3. pdf2eps script (get from http://tex.stackexchange.com/questions/20883/how-to-convert-pdf-to-eps)

I saved a copy of items 2 and 3 here. Of course, you need to put them in the search path and apply “chmod a+x” to them.

Then run the following

  $ pdfcrop --verbose input.pdf output.pdf
  $ pdf2eps 1 output

The second line will generate output.eps.

If we need to impose the same bounding box on more than one files, read the output of

  $ pdfcrop --verbose input1.pdf output1.pdf
  $ pdfcrop --verbose input2.pdf output2.pdf
  ...
  $ pdfcrop --verbose inputn.pdf outputn.pdf

Figure out the largest bounding box <left> <top> <right> <bottom>, and run

  $ pdfcrop --bbox "<left> <top> <right> <bottom>" input1.pdf output1.pdf
  $ pdfcrop --bbox "<left> <top> <right> <bottom>" input2.pdf output2.pdf
  ...
  $ pdfcrop --bbox "<left> <top> <right> <bottom>" inputn.pdf outputn.pdf

Produce eps/pdf files from Matlab figures (almost) exactly as what you see on screen

August 15th, 2012 by wy1

[Download m-file]

function myprint(name, h)
% This code will produce eps and pdf files out of a MATLAB figure with no
% change. What you see will be what you get.
%
% Example:
%
%   h = figure(1);
%   ...(your plotting code) ...
%   myprint('filename', h);
%
% Your will get filename.eps and filename.pdf.
% -----------------
% Wotao Yin (2011).

if ~ischar(name)
    error('Input 1 must be char');
end

if ~exist('h','var') || isempty(h)
    h = gcf;
end

ppos = get(h,'Position'); ppos(1:2)=0; psize = ppos(3:4);
dpi = get(0,'ScreenPixelsPerInch');
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperSize', psize/dpi);
set(gcf, 'PaperPosition', ppos/dpi);

print('-painters','-depsc2','-r0',name);
print('-painters','-dpdf','-r0',name);

end