# Need to do some changes to pgfplots axis

I am still in the habit of learning TikZ, barely started using pgfplots to create my images

Although I am still not sure what the best way to do so is, I need to create the image below. My problems is mainly the axis (and the points, but I guess I should refrain from asking too many questions at once.)

Here is the image I am trying to reconstruct. And below is my attempt.

And here is my code

\documentclass[10pt,a4paper]{article}
\usepackage[hmargin=3cm,vmargin=2cm]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{figure}[h!tbp]
\centering
\begin{tikzpicture}
\begin{axis}[
scale only axis,
grid=major,
axis lines=middle,
inner axis line style={=>},
xlabel={\large $x$},
ylabel={\large $y$},
ytick={-5,-4,...,5},
xtick={-4,-3,...,4},
ymin=-5,
ymax=6,
xmin=-5,
xmax=5,
]
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

My question is - How do I shift the ticks, so they are not directly below, but shifted to the right?. (Same as the image I am trying to reconstruct) - Are there any easy way to add a dashed background, quite faint? I know a dashed background is not advised, but I think It works if it is quite faint, and I need my plot to be as close as possible to the original image =)

-
For the intersections with the axes, take a look at tex.stackexchange.com/questions/21740/…. – Jake Dec 22 '11 at 13:37

You can change pretty much everything in PGFplots using styles, many of which can be accessed using normal keys (instead of .../.styles).

For shifting the x tick labels, I would use xticklabel style={anchor=north west} and yticklabel style={anchor=south east}. In order to align the numbers nicely, you can also set inner xsep=0pt for the x tick labels and inner ysep=0pt for the y tick labels.

You'll notice that the 0 is missing from the labels because you use axis lines=middle. Unfortunately, there's no easy way to reactivate these. We'll have to add them back in "by hand". You can specify code that is to be executed at the end of the axis in the after end axis/.code={...} key. The path

\path (axis cs:0,0)
node [anchor=north west,yshift=-0.075cm] {0}
node [anchor=south east,xshift=-0.075cm] {0};

will add two zeros at the positions where you'd expect them. The yshift and xshift are half the tick mark lengths. The tick mark lengths are 0.15cm by default (which is specified in the manual), and the shift by half that amount is what PGFplots applies automatically (which can be seen by looking at the source code).

The grid appearance can be set using grid style.

\documentclass[10pt,a4paper]{article}
\usepackage[hmargin=3cm,vmargin=2cm]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{figure}[h!tbp]
\centering
\begin{tikzpicture}
\begin{axis}[
scale only axis,
grid=major,
grid style={dashed, gray!50},
axis lines=middle,
inner axis line style={=>},
xlabel={\large $x$},
ylabel={\large $y$},
yticklabel style={inner ysep=0pt, anchor=south east},
ytick={-5,-4,...,5},
xticklabel style={inner xsep=0pt, anchor=north west},
xtick={-4,-3,...,4},
ymin=-5,
ymax=6,
xmin=-5,
xmax=5,
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};
}
]
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
-
Exellent answer, I feel this is a too small question to ask. So I will post is as a comment. I tried plotting a very similar image pastebin.com/JxzfWEzT Problem is that the function stops at x=5 and not x=7. Could you explain why? =) – N3buchadnezzar Dec 22 '11 at 14:49
@N3buchadnezzar It seems as if your example at pastebin is configured to display ticks + limits up to x=7 --- but the sampling range is independent of the axis limits. Use domain=4:7 to sample the function from 4 to 7. – Christian Feuersänger Dec 22 '11 at 20:38
@Jake: It seems (see this question) that before end behaves better than after end. Do you know why? What is the loss in replace after by before? – Colas Apr 8 '14 at 18:11

While Jake's method is clearly preferable for this particular graph, I prefer to manually specify the xticklabels and move only the ones that need to be moved by adding some \hspace on the appropriate side:

xticklabels={$-4$,$-3$,$-2\hspace{0.9em}$,$-1$,$$,1,\hspace{0.3em}2,3,4} For the yticklabels you can use \raisebox to move them up and down: yticklabels={-5,{\raisebox{-2.5ex}{-4}},-3,-2,-1,$$,$1$,$2$,$3$,$4$,$5$},

which yields:

This method comes in handy with trigonometric functions as some of the labels need to shift to the right and others need to shift to the left. See for example axis with trigonometric labels in pgfplots.

• Used an empty label $$to not display the 0 tick label for both the x and y tick labels. An alternative is to leave that entry empty: -1, ,1. • Added samples=50 to get a smoother graph. ## Code: \documentclass[border=3pt]{standalone} \usepackage{tikz} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis}[ scale only axis, grid=major, axis lines=middle, inner axis line style={=>}, xlabel={\large x}, ylabel={\large y}, ytick={-5,-4,...,5}, yticklabels={-5,{\raisebox{-2.5ex}{-4}},-3,-2,-1,$$,$1$,$2$,$3$,$4$,$5$},
xtick={-4,-3,...,4},
xticklabels={$-4$,$-3$,$-2\hspace{0.9em}$,$-1$,,$1$,$\hspace{0.3em}2$,$3$,$4$},
ymin=-5,
ymax=6,
xmin=-5,
xmax=5,
samples=50
]