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 have been searching for hours now on how to fix these small problems, and I am finally admitting defeat and humbly ask for help. Lately I have been going over some of my badly drawn figures, trying to clean the up and whatnot.

    \documentclass[10pt,a4paper]{article} 
\usepackage{tikz,tkz-euclide,pgfplots}
\usetkzobj{all}
\begin{document}

\begin{tikzpicture}[x=0.02cm,y=0.02cm,scale=0.8]
  \tkzInit[ymin=-40,ymax=390,xmin=-40,xmax=540]
  \tkzClip
\draw[help lines,ultra thin,dashed,gray!50!white] (-50,-50) grid (550,400);
\coordinate [label=below right:{50}] (x_1) at (50,0);
\coordinate [label=below right:{}] (B) at (0,390);
\coordinate [label=above right:{\large $y$}] (U) at (0,350);
\coordinate [label=above left:{\large $x$}] (A) at (540,0);
\coordinate [label=below right:{50}] (x_1) at (50,0);
\coordinate [label=below right:{100}] (x_2) at (100,0);
\coordinate [label=below right:{150}] (x_3) at (150,0);
\coordinate [label=below right:{200}] (x_4) at (200,0);
\coordinate [label=below right:{250}] (x_5) at (250,0);
\coordinate [label=below right:{300}] (x_6) at (300,0);
\coordinate [label=below right:{350}] (x_7) at (350,0);
\coordinate [label=below right:{400}] (x_8) at (400,0);
\coordinate [label=below right:{450}] (x_9) at (450,0);
\coordinate [label=below right:{500}] (x_10) at (500,0);
\coordinate [label=below right:{0}] (O) at (0,0);
\coordinate [label=above left:{50}] (y_1) at (0,50);
\coordinate [label=above left:{100}] (y_2) at (0,100);
\coordinate [label=above left:{150}] (y_3) at (0,150);
\coordinate [label=above left:{200}] (y_4) at (0,200);
\coordinate [label=above left:{250}] (y_5) at (0,250);
\coordinate [label=above left:{300}] (y_6) at (0,300);
\coordinate [label=above left:{350}] (y_7) at (0,350);
\coordinate [label=above left:{\bf Kostnader per m{\aa}ned (kroner)}] (R) at (400,350);
\coordinate [label=above left:{\bf Ringetid (minutter)}] (K) at (540,20);
\tkzDefPoint(0,-50){C} 
\tkzDefPoint(-50,0){D}
\tkzDefPoint(0,87.50){S}
\tkzDefPoint(500,337.50){T}
\tkzDrawSegment[thick,-stealth](D,A)
\tkzDrawSegment[thick,-stealth](C,B)
\tkzDrawSegment[ultra thick,red](S,T)
\end{tikzpicture}

\vspace*{2ex}

\begin{tikzpicture}
\begin{axis}[
    scale only axis,
    grid=major,
    grid style={dashed, gray!40},
    axis lines=middle,
    inner axis line style={-stealth},
    xlabel={\begin{tabular}{@{}r@{}}\bf Ringetid (minutter)\\ \large $x$\end{tabular}},
    ylabel={\large $y$ \ \ \normalsize \bf Kostnader per m{\aa}ned (kroner)},
    ytick={0,50,...,350},
    xtick={0,50,...,500},
    x tick label style={below right},
    y tick label style={above left,},
    ymin=-50,
    ymax=380,
    xmin=-50,
    xmax=550,
]
\addplot[color=red,very thick] coordinates { (0, 75) (500, 340)};
\end{axis}
\end{tikzpicture}

\end{document}

The first image as a rather messy code, but the output is exactly as I want it. I tried cleaning up the code, but ran into heaps of problems.

My main issue how to keep the ratio between the x- and y-axis, is there any way to do this?

(Now I also had some problems with the x and y labels, and the thickness of the axis, but I guess that can wait for another answer...). Any other hints on improvements would be great. =)

share|improve this question
1  
You may be aware of this, but don't use \bf, \it etc. Rather use \bfseries or \textbf{...}. See Does it matter if I use \textit or \it, \bfseries or \bf, etc –  Torbjørn T. Jun 24 '12 at 14:50

2 Answers 2

up vote 19 down vote accepted

There are three options:

  • You can specify axis equal (or equivalently unit vector ratio=1 1 1), which will make the axis units the same while keeping the dimensions of the plot (set by width and height) constant, by enlarging the axis limits.
  • You can specify axis equal image (or equivalently unit vector ratio*=1 1 1), which will make the axis units the same without keeping the dimensions of the plot. The axis limits will not be changed.
  • You specify the unit vector lengths explicitly yourself, using something like x=0.015cm, y=0.015cm. That way, the plot dimensions will depend on the axis limits. In my experience, this can be interesting if you have several axes with bar plots, with different numbers of entries. By specifying a length for the unit vectors, a ybar axis with 20 entries will be twice as wide as one with 10 entries, while keeping the distance between the bars constant.

In this case, I would go with the second option, since you probably care more about the axis limits than the precise dimensions of the plot.

To get the 0 tick labels, you'll have to use a bit of a workaround, described in Need to do some changes to pgfplots axis.

For the multi-line x label, you don't need to use a tabular: You can simply insert a \\ and set the alignment of the node using xlabel style={align=right}.

\documentclass[10pt,a4paper]{article} 
\usepackage{pgfplots,relsize}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
    unit vector ratio*=1 1 1,
    width=11cm,
    grid=major,
    grid style={dashed, gray!30},
    axis lines=middle, enlargelimits=false,
    inner axis line style={-stealth},
    xlabel={
        \textbf{Ringetid (minutter)}\\\large $x$
    },
    xlabel style={
        anchor=south east,
        align=right
    },
    ylabel={\large $y$ \ \ \normalsize \bf Kostnader per måned (kroner)},
    ytick={0,50,...,350},
    xtick={0,50,...,500},
    x tick label style={below right},
    y tick label style={above left,},
    ymin=-50,
    ymax=380,
    xmin=-50,
    xmax=550,
    after end axis/.code={
        \path (axis cs:0,0) 
        node [anchor=north west,yshift=-0.075cm] {0}
        node [anchor=south east,xshift=-0.075cm] {0};
    }
]
\addplot[color=red,very thick] coordinates {
            (0, 75)
            (500, 340)
        };
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thanks a bunch as always Jake <3. Reason for not asking about the zeros was of course because I knew you had solved that problem earlier. I actually tried using \\ but for some reason this did not work. Is there any reason why you use both "axis equal" and "unit vector ratio*=1 1 1" in your answer? =) –  N3buchadnezzar Jun 24 '12 at 16:45
    
@N3buchadnezzar: The \\ only works if you also set something for align (like left, right, or center). And no, no reason for leaving the axis equal in there apart from carelessness =) –  Jake Jun 24 '12 at 16:57
    
There is somewhat of a problem with your solution when the length of the axis vary greatly. Take a look here for instance pastebin.com/mE2YxFv8 Now I do not want the yxis and the xaxis to have the same length, I just want the ratio between them to be the same. =) From Chat The solution seems to be "axis equal image". –  N3buchadnezzar Jun 29 '12 at 20:52
    
@N3buchadnezzar: You can use unit vector ratio*=1 1 1 (with the asterisk, as used in my answer) to the same effect as axis equal image). –  Jake Jul 1 '12 at 19:32

I'm not an expert of pgfplots but for tkz-euclide I can suggest some improvements. You can use some specific macros : \tkzgrid, \tkzLabelX, \tkzLabelY, \tkzDrawX and \tkzDrawY.

You don't need [x=0.02cm,y=0.02cm]but you can use scale=0.8.

\documentclass[10pt,a4paper]{article} 
\usepackage{tikz,tkz-euclide}
\usetkzobj{all}
\begin{document}

\begin{tikzpicture}
  \tkzInit[ymin=0,ymax=390,xstep=50,ystep=50,xmin=0,xmax=540]
  \tkzGrid
  \tkzLabelX[orig=false,label options={below right}] 
  \tkzLabelY[orig=false]
  \tkzDrawX[below left =22 pt,label={\textbf{Ringetid (minutter)}}]  
  \tkzDrawY[right=12 pt,label={\textbf{Kostnader per m{\aa}ned (kroner)}}]  
  \tkzDefPoint(0,87.50){S}
  \tkzDefPoint(500,337.50){T}
  \tkzDrawSegment[ultra thick,red](S,T)
\end{tikzpicture}

\end{document} 

enter image description here

share|improve this answer

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.