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.

How do I make the axis labels use multiples of \pi in PGFPlots? enter image description here

share|improve this question
1  
See the example in pgfplots manual pages 262-263 (manual for version 1.5, of July 29 2011) for a solution. (Assuming you're after the labels on the x-axis.) –  Torbjørn T. Nov 15 '11 at 16:31
    
I think you mean "display one axis". If so, which one? Can you clarify the question a bit? Also, it would help to show the code that produced your figure, not just the figure. –  Alan Munn Nov 15 '11 at 16:31
4  
Does this post help? –  cmhughes Nov 15 '11 at 16:32
    
It is not clear what you are looking for. Please compose a MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Nov 15 '11 at 16:34
    
Did you look at Pgfplots with custom axis markers? I think they solve your question. May be not an automatic solution, but a solution which looks like the one at page 264-265 (section 4.21) of pgfplots manual. –  Ignasi Nov 15 '11 at 16:36

1 Answer 1

up vote 14 down vote accepted

As mentioned in the comments, this is basically the same as Pgfplots with custom axis markers. All you need to do is to specify where you want the xtick={...} and how you want each of them labelled via xticklabels={...}.

Method 1: Explicit Labels:

Explicit labels can be specified using xticklabels. The one complication that comes about is that sometimes the label overlaps with the plot as is the case for -\pi and 2pi in the blue graph. I have not found a elegant way to fix that so I just manually add some spacing to those labels to tweak them as I did the red graph:

enter image description here

Method 2: Scaled Axis Labels:

An alternate is to scale the x-axis labels in terms of multiples of pi, and show that the x axis labels are multiples of pi. This solution is based on Spikes solution, so you should up vote that if you prefer this version. I prefer to label this as part of the axis (brown graph), but others might prefer to display it as in the cyan graph:

enter image description here

xticklabels:

If you want a tick mark, but not a corresponding label you can simply place an empty label as in $$ or just better just use a double comma ,, to skip it being labelled. For instance, if the labels at +\pi and -\pi are not desired, simply replace those labels with spaces (extra spaces here are just to point out where the gap is):

xticklabels={$-2\pi$, $-\frac{3\pi}{2}$,    , $-\frac{\pi}{2}$, 
             $\frac{\pi}{2}$,     , $\frac{3\pi}{2}$, $2\pi$}

xtick:

Note that two methods of specifying where the tick marks go are used in the code. One is to explicitly list them as

xtick={-6.28318, -4.7123889, -3.14159, -1.5708, 1.5708, 3.14159, 4.7123889, 6.28318}

This is used in the first two examples so that the correspondence between the xtick and xticklabels is easier to see. The second two use the more compact method:

xtick={-6.28318, -4.7123889, ..., 6.28318}

Code:

\documentclass[border=2pt,tightpage]{standalone} 
\usepackage{pgfplots}

% Grouping the common style settings here to make the code below easier to read
\pgfkeys{/pgfplots/Axis Style/.style={
    width=13.5cm, height=5cm,
    axis x line=center, 
    axis y line=middle, 
    samples=100,
    ymin=-1.5, ymax=1.5,
    xmin=-7.0, xmax=7.0,
    domain=-2*pi:2*pi
}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    Axis Style,
    xtick={
        -6.28318, -4.7123889, -3.14159, -1.5708,
        1.5708, 3.14159, 4.7123889, 6.28318
    },
    xticklabels={
        $-2\pi$, $-\frac{3\pi}{2}$, $-\pi$, $\frac{\pi}{2}$,
        $\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$
    }
]
\addplot [mark=none, ultra thick, blue] {sin(deg(x))};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
    Axis Style,
    xtick={
        -6.28318, -4.7123889, -3.14159, -1.5708,
        1.5708, 3.14159, 4.7123889, 6.28318
    },
    xticklabels={
        $-2\pi$, $-\frac{3\pi}{2}$, $-\pi\hspace{0.30cm}$, $\frac{\pi}{2}$,
        $\frac{\pi}{2}$, $\pi\hspace{0.10cm}$, $\frac{3\pi}{2}$, $\hspace{0.25cm} 2\pi$
    }
]
\addplot [mark=none, ultra thick, red] {sin(deg(x))};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
    Axis Style,
    xtick={-6.28318, -4.7123889, ..., 6.28318},
    scaled x ticks={real:3.1415},
    xtick scale label code/.code={},
    xlabel={$x \thinspace [\times \pi]$}
]
\addplot [mark=none, ultra thick, brown] {sin(deg(x))};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
    Axis Style,
    xtick={-6.28318, -4.7123889, ..., 6.28318},
    scaled x ticks={real:3.1415},
    xtick scale label code/.code={$[\times \pi]$},
    xlabel={$x$}
]
\addplot [mark=none, ultra thick, cyan] {sin(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
+1 Is there a way to specify xtick using a \foreach loop? This would be useful if one wanted to have different fractions of \pi. –  cmhughes Nov 15 '11 at 20:14
    
@cmhughes: I had attempted something similar and manually add the tick labels but have not come up with an elegant solution. See my related posts: Using macro defined lists in tikz pgfplot, and PGFplots foreach equivalent to TikZ's with multiple variables separated by a slash. –  Peter Grill Nov 15 '11 at 20:23
    
@cmhughes: Also related: Macro to access a specific member of a list –  Peter Grill Nov 15 '11 at 20:25
    
@cmhughes: See alternate solution which does not require you to specify each point explicitly. –  Peter Grill Nov 16 '11 at 1:47

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.