Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to do a 2D surface plot, and the way to do this in pgfplots seems to be making a 3D surface plot, and then adjusting the view. And this works fine, until I try to adjust the aspect ratio. The 2D options don't work, because it is a 3D plot afterall, and the plot box ratio ceases to work once I adjust the view. I adopted an example from the pgfplots manual (section 4.5.2, all y values multiplied by two):

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[view={0}{90},plot box ratio={1}{2}{1}]
% We have `surfdata.dat' with
% ---------
% 0 0 0.8
% 1 0 0.56
% 2 0 0.5
% 3 0 0.75
%
% 0 2 0.6
% 1 2 0.3
% 2 2 0.21
% 3 2 0.3
%
% 0 4 0.68
% 1 4 0.22
% 2 4 0.25
% 3 4 0.4
%
% 0 6 0.7
% 1 6 0.5
% 2 6 0.58
% 3 6 0.9
% -> yields a 4x4 matrix:   
    \addplot3[surf] file {surfdata.dat};
\end{axis}
\end{tikzpicture}
\end{document}

My output is on the left, my expected output on the right (obviously I don't want all the ticks distorted).

enter image description here

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

As Jake pointed out, the problem of plot box ratio is that it is overridden by width and height (I suppose it is only useful for 3d axes).

The option of use here is unit vector ratio=<x> <y> <z> (without braces):

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{surfdata.dat}
 0 0 0.8
 1 0 0.56
 2 0 0.5
 3 0 0.75

 0 2 0.6
 1 2 0.3
 2 2 0.21
 3 2 0.3

 0 4 0.68
 1 4 0.22
 2 4 0.25
 3 4 0.4

 0 6 0.7
 1 6 0.5
 2 6 0.58
 3 6 0.9
\end{filecontents}

\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\begin{axis}[view={0}{90},unit vector ratio*=1 2 1]
% We have `surfdata.dat' with
% ---------
% 0 0 0.8
% 1 0 0.56
% 2 0 0.5
% 3 0 0.75
%
% 0 2 0.6
% 1 2 0.3
% 2 2 0.21
% 3 2 0.3
%
% 0 4 0.68
% 1 4 0.22
% 2 4 0.25
% 3 4 0.4
%
% 0 6 0.7
% 1 6 0.5
% 2 6 0.58
% 3 6 0.9
% -> yields a 4x4 matrix:   
    \addplot3[surf] file {surfdata.dat};
\end{axis}
\end{tikzpicture}
\end{document}

Note that unit vector ratio (without the asterisk *) will enlarge axis limits in order to satisfy width and height. The version with star will shrink the size to match the restriction (which is what you want here).

Note that the only difference between my approach with unit vector ratio* and x={},y={},z={} as suggested by Jake is that mine will still use (one of) width/height whereas Jake's explicitly controls the axis dimensions by means of the unit vectors. Both are completely equivalent, however. In your context, it is merely a matter of taste, I suppose.

Here is the result with unit vector ratio*=1 1 1 (which is the same as axis equal image):

enter image description here

share|improve this answer
add comment

(The filecontents environment writes its content to a file.)

You can add yscale=2 to the axis options. This is an option from TikZ, but works in this case as well.

pgfplots provides the option y post scale, which can be used as well. This is documented in section 4.9 Scaling of the pgfplots manual (page 196 of the manual dated July 29 2011).

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{surfdata.dat}
 0 0 0.8
 1 0 0.56
 2 0 0.5
 3 0 0.75

 0 2 0.6
 1 2 0.3
 2 2 0.21
 3 2 0.3

 0 4 0.68
 1 4 0.22
 2 4 0.25
 3 4 0.4

 0 6 0.7
 1 6 0.5
 2 6 0.58
 3 6 0.9
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[view={0}{90},yscale=2]
    \addplot3[surf] file {surfdata.dat};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[view={0}{90},%
             y post scale=2,
             %ytick={2,4,6}  % uncomment to have the same yticks as the other
             ]
    \addplot3[surf] file {surfdata.dat};
\end{axis}
\end{tikzpicture}
\end{document}

yscale on the left, y post scale on the right.

enter image description here

share|improve this answer
    
Thanks! Is this documented somewhere, I didn't find it in the manual during a quick flick through? –  Psirus Dec 22 '11 at 12:28
    
@Psirus It's actually a TikZ option. I didn't really think about that, I just tried and found that it works ... Searching the manual a little reveals a similar PGFplots option: y post scale. Perhaps that is better to use in the sense that it is from PGFplots, but it changes the ticks on the y axis. –  Torbjørn T. Dec 22 '11 at 12:34
    
This option was the only possible solution where I was plotting very large coordinates: using unit vector ratio*=2 1 turns out to be the same as x=(2,0), y=(0,1), and this overrides the automatic downscaling that makes sure the resulting plot won't be miles wide/tall. –  derabbink Jan 11 at 9:45
add comment

The plot box ratio are overridden by the height and width settings of the plot (see section 4.10.3 of the manual). So one way of getting the desired ratio is to provide height and width values with the correct ratio (make sure you also set scale only axis so only the axis area is taken into account when calculating the width and height).

However, for plots like these where you want the dimensions to be "square", I would explicitly supply the x, y, and z vectors:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{surfdata.dat}
 0 0 0.8
 1 0 0.56
 2 0 0.5
 3 0 0.75

 0 2 0.6
 1 2 0.3
 2 2 0.21
 3 2 0.3

 0 4 0.68
 1 4 0.22
 2 4 0.25
 3 4 0.4

 0 6 0.7
 1 6 0.5
 2 6 0.58
 3 6 0.9
 \end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[x={(1cm,0)}, y={(0,1cm)}, z={(0,0)}]
    \addplot3[surf] file {surfdata.dat};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.