带有网格和通过原点的轴线的散点图

带有网格和通过原点的轴线的散点图

我想制作一个带有误差线的散点图(使用 pgfplots 或 tikz),它具有两个特征;我需要两个轴在 处相交(0,0),并且背景中有一个网格。

我尝试用谷歌搜索过很多次,但似乎无法将这两者结合起来。tikz 和 pgfplots 还不熟悉。提前谢谢大家!

答案1

要使轴线通过原点,请设置axis lines=middle。要获取网格,请设置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}

相关内容