将点垂直线放到轴上

将点垂直线放到轴上

我想要得到两个图,使得每个点都有到 x 轴的垂直线(在一个图上),并且每个点都有到 y 轴的垂直线(在另一个图上)。像这样:

在此处输入图片描述

以下是我目前所掌握的信息:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}


\newcommand{\datapthreeone}{prob3.dat}
\begin{filecontents*}{\datapthreeone}
    x1     y1  averX  averY     Ex     Ey
 2.000,  3.000, -0.714,  1.000,  1.000, -0.714
 1.000,  2.000, -1.714,  0.000,  0.000, -1.714
 1.000,  1.000, -1.714, -1.000, -1.000, -1.714
 2.000,  2.000, -0.714,  0.000,  0.000, -0.714
 4.000,  2.000,  1.286,  0.000,  0.000,  1.286
 4.000,  3.000,  1.286,  1.000,  1.000,  1.286
 5.000,  1.000,  2.286, -1.000, -1.000,  2.286
\end{filecontents*}

\begin{document}
\begin{figure}
\begin{center}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 2, horizontal sep=4em, vertical sep=5em}]
\nextgroupplot[xmin=-2, xmax=2, ymin=-2.5, ymax=2.5, legend style={font=\fontsize{7}{9}\selectfont}, title = (c) PCA Projection Points]
\addplot [only marks, line width = 0.3mm, mark=x, blue, mark options={scale=1.2}]table[x index=4, y index=5, only marks,col sep=comma] {\datapthreeone}; \addlegendentry{\emph{Projected Data}}
\addplot [-latex, dashed, blue, domain=-2:2, samples=2]{0*x + 0};
\addplot[-latex, dashed]coordinates {(-3,0) (2.5,0)};
\addplot[-latex, dashed]coordinates {(0,-2.5) (0,2.5)};

\nextgroupplot[xmin=-2, xmax=2, ymin=-2.5, ymax=2.5, legend style={font=\fontsize{7}{9}\selectfont}, title = (c) PCA Projection Points]
\addplot [only marks, line width = 0.3mm, mark=x, blue, mark options={scale=1.2}]table[x index=4, y index=5, only marks,col sep=comma] {\datapthreeone}; \addlegendentry{\emph{Projected Data}}
\addplot [-latex, dashed, blue, domain=-2:2, samples=2]{0*x + 0};
\addplot[-latex, dashed]coordinates {(-3,0) (2.5,0)};
\addplot[-latex, dashed]coordinates {(0,-2.5) (0,2.5)};

\end{groupplot}

\end{tikzpicture}
\end{center}
\end{figure}

\end{document} 

答案1

这是我在 pgfplots 手册中发现的(http://pgfplots.sourceforge.net/pgfplots.pdf):

您需要在命令中添加ycomb或选项,在这种情况下,它会水平或垂直连接坐标。当然,还有一种更聪明的方法来编写直角代码……xcomb\addplot [..] ...

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest,legend style={font=\fontsize{7}{9}\selectfont}}
\usepackage{graphicx}
\newcommand{\datapthreeone}{prob3.dat}
\begin{filecontents*}{\datapthreeone}
    x1     y1  averX  averY     Ex     Ey
    2.000,  3.000, -0.714,  1.000,  1.000, -0.714
    1.000,  2.000, -1.714,  0.000,  0.000, -1.714
    1.000,  1.000, -1.714, -1.000, -1.000, -1.714
    2.000,  2.000, -0.714,  0.000,  0.000, -0.714
    4.000,  2.000,  1.286,  0.000,  0.000,  1.286
    4.000,  3.000,  1.286,  1.000,  1.000,  1.286
    5.000,  1.000,  2.286, -1.000, -1.000,  2.286
\end{filecontents*}
\begin{document}
    \begin{tikzpicture}

    \begin{axis}[
    title= (c) PCA Projection Points,
    xmin = -2, xmax =2,ymin=-2.5, ymax=2.5]
        \addplot [only marks, line width = 0.3mm, mark=x, blue, mark options={scale=1.2}]table[x index=4, y index=5, only marks,col sep=comma] {\datapthreeone}; \addlegendentry{\emph{Projected Data}} 
        \addplot[ycomb, red] table[x index=4, y index=5,col sep=comma] {\datapthreeone};

        \addplot [-latex, dashed, blue, domain=-2:2, samples=2]{0*x + 0};
        \addplot[-latex, dashed]coordinates {(-3,0) (2.5,0)};
        \addplot[-latex, dashed]coordinates {(0,-2.5) (0,2.5)};

        \draw[red] (0.9,0) -- (0.9,0.2) -- (1,0.2);
        \draw[red] (-0.1,0) -- (-0.1,0.2) -- (0,0.2);
        \draw[red] (-1.1,0) -- (-1.1,0.2) -- (-1,0.2);
        \draw[red] (-0.9,0) -- (-0.9,-0.2) -- (-1,-0.2);
        \draw[red] (1.1,0) -- (1.1,-0.2) -- (1,-0.2);
    \end{axis}
    \end{tikzpicture}
    \begin{tikzpicture}
    \begin{axis}[
    title= (c) PCA Projection Points,
    xmin = -2, xmax =2,ymin=-2.5, ymax=2.5]
        \addplot [only marks, line width = 0.3mm, mark=x, blue, mark options={scale=1.2}]table[x index=4, y index=5, only marks,col sep=comma] {\datapthreeone}; \addlegendentry{\emph{Projected Data}}
        \addplot[xcomb, red] table[x index=4, y index=5,col sep=comma] {\datapthreeone};

        \addplot [-latex, dashed, blue, domain=-2:2, samples=2]{0*x + 0};
        \addplot[-latex, dashed]coordinates {(-3,0) (2.5,0)};
        \addplot[-latex, dashed]coordinates {(0,-2.5) (0,2.5)};
    \end{axis}

    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容