2

I want to make a scatter plot with error bars (using pgfplots or tikz) with two characteristics; I need the two axes to meet at (0,0), and a grid in the background.

I tried to google this a number of times, and I can't seem to combine these two. New to tikz and pgfplots. Thanks a lot in advance!

Jake
  • 232,450
Sheheryar Zaidi
  • 207
  • 2
  • 6

1 Answers1

4

To make the axis lines go through the origin, set axis lines=middle. To get a grid, set grid=both.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines=middle, % Axis lines go through the origin
    grid=both, % Activate grid for both dimensions
    after end axis/.code={ % To get labels for the origin
        \path (axis cs:0,0) 
            node [anchor=north west,yshift=-0.075cm] {0}
            node [anchor=south east,xshift=-0.075cm] {0};
    }
]
\addplot [
    only marks, % So there are no connecting lines between the points
    error bars/x dir=both, error bars/y dir=both, % Activate error bars in both dimensions
    error bars/x explicit, error bars/y explicit, % Error values will be provided for each coordinate
] table [
    x error=error_x, % Columns to use for the error values
    y error=error_y
]{
x       y       error_x error_y
1       2.1     0.3     0.4
-2.5    -1.3    0.5     0.2
5       -1.9    0.8     1.1
};
\end{axis}
\end{tikzpicture}
\end{document}

Jake
  • 232,450
  • Is there any way I can get a zero at the origin? I could use tikz coordinates to make a node but I doubt thats the easiest way. Any suggestions? – Sheheryar Zaidi Sep 12 '14 at 07:25
  • Also, can I somehow specify the domain and range? As in how much of the x and y axis are shown. – Sheheryar Zaidi Sep 12 '14 at 07:30
  • To label the origin, you can use the approach from http://tex.stackexchange.com/questions/39098/need-to-do-some-changes-to-pgfplots-axis/39099. I've edited my answer. To change the domain and range, set xmin=<...>, xmax=<...>, ymin=<...>, ymax=<...> (but that's really something you could have looked up in the manual). – Jake Sep 12 '14 at 07:33