3D 散点图中的标记不以 3D 形式显示

3D 散点图中的标记不以 3D 形式显示

我正在尝试绘制如下所示的 3D 散点图,但标记是圆圈,无论我选择哪种视图,它们始终只出现在 2D 中。我希望它们在 XY 平面上是平的。请告诉我该怎么做。我尝试了“设置图层”,但这会将我的整个图形向下推,推到下一节的文本后面。我认为红线也是这样,没有给我正确的 3D 视角。

梅威瑟:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{math, decorations.pathreplacing,angles,quotes,bending, arrows.meta}
\pgfplotsset{compat=1.15}
\pgfplotstableread{
X Y Z m
2.2 14 0 0
2.7 23 0 0
3 13 0 0
3.55 22 0 0
4 15 0 0
4.5 20 0 0
4.75 28 0 0
5.5 23 0 0
}\datatable

\makeatletter
        \pgfdeclareplotmark{dot}
        {%
            \fill circle [x radius=0.02, y radius=0.08];
        }%
\makeatother

\begin{document}

\section{table using raw data in 3D}

The below diagram tries to replicate in 3D, the Figure 12.3 found in \cite{devore} , page 472 \\

% https://tex.stackexchange.com/questions/11251/trend-line-or-line-of-best-fit-in-pgfplots
\begin{tikzpicture}[scale=1.5]
\begin{axis}
    [   
    view={140}{50},
    xmin=1,xmax=6, ymin=5,ymax=40, zmin=0, zmax=10,
    % ytick=\empty,xtick=\empty,ztick=\empty,
    clip=false, axis lines = middle
    ]
    \addplot3[only marks, fill=cyan,mark=*] table {\datatable};
    \addplot3[thick, red] table[y={create col/linear regression={y=Y}}] {\datatable}; % compute a linear regression from the input table

    \def\X{2.7}
    \def\Y{23}
    \draw [-{Latex[length=4mm, width=2mm]}] (\X,\Y+10,12.5) node[right]{$(x_1,y_1)$} ..controls (0,5) .. (\X,\Y,0);
    \draw [-{Latex[length=4mm, width=2mm]}] (9,30,20) node[left, align=right]{\scriptsize True Regression Line\\ \scriptsize $y = \beta_0 + \beta_1 x$} .. controls (5,2.5) .. (5,22.7,0); 
    \draw [decorate, decoration={brace,amplitude=3pt}, xshift=0.5mm] (\X,\Y-0.1,0) to (\X,17,0) node[left, xshift=5mm, yshift=-1mm]{\scriptsize 1}; % brace 

    \draw [thick,dash pattern={on 7pt off 2pt on 1pt off 3pt}] (1,17.1) to (\X,17.1);
    \draw [thick,dash pattern={on 7pt off 2pt on 1pt off 3pt}] (\X,17.1) -- (\X,5);
    \node[above] at (\X,4) {$x_1$};
    \node[right, align=left,yshift=0.5mm] at (1,17.1) {$E(Y|x_1)=\mu_{Y.x_1}$};

\end{axis}
\end{tikzpicture}




  \begin{thebibliography}{1}
  \bibitem{devore} Jay. L Devore {\em Probability and Statistics for Engineering and the Sciences} 8th Edition.
  \end{thebibliography}


\end{document}

输出:
在此处输入图片描述

答案1

更新:没有硬编码值的版本。​​(请注意,我使用了一个并非完全无害的命令:\globaldefs。替代方案会更长。我相信这里使用\globaldefs就可以了。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{3d,calc,decorations.pathreplacing,arrows.meta}
% small fix for canvas is xy plane at z % https://tex.stackexchange.com/a/48776/121799
\makeatletter
\tikzoption{canvas is xy plane at z}[]{%
    \def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
    \def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
    \def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
    \tikz@canvas@is@plane}
\makeatother
\pgfplotsset{compat=1.15}
\pgfplotstableread{
X Y Z m
2.2 14 0 0
2.7 23 0 0
3 13 0 0
3.55 22 0 0
4 15 0 0
4.5 20 0 0
4.75 28 0 0
5.5 23 0 0
}\datatable
\pgfplotstableread{
X Y Z m
2.2 0 0 0
2.7 0 0 0
3 13 0 0
3.55 0 0 0
4 15 0 0
4.5 0 0 0
4.75 0 0 0
5.5 0 0 0
}\datatabletwo


\pgfdeclareplotmark{fcirc}{%          
          \begin{scope}[expand style={local frame}{\MyLocalFrame},local frame]
          \begin{scope}[canvas is xy plane at z=0,transform shape]
            \fill circle(0.1);
          \end{scope}   
          \end{scope}
}%
% based on https://tex.stackexchange.com/a/64237/121799
\tikzset{expand style/.code n args={2}{\tikzset{#1/.style/.expanded={#2}}}}
\newcommand{\GetLocalFrame}{
    \path let \p1=($(1,0,0)-(0,0,0)$), \p2=($(0,1,0)-(0,0,0)$),
    \p3=($(0,0,1)-(0,0,0)$) in \pgfextra{
    \pgfmathsetmacro{\ratio}{veclen(\x1,\y1)/veclen(\x2,\y2)}
    \xdef\MyLocalFrame{   
                x   =  {   (\x1,\y1)    },
                y   =  {    (\ratio*\x2,\ratio*\y2)     },
                z   =   {     (\x3,\y3)     }
            }
    }; }

\begin{document}
\begin{tikzpicture}[scale=1.5]
\begin{axis}
    [   
    view={140}{50},
    xmin=1,xmax=6, ymin=5,ymax=40, zmin=0, zmax=10,
    % ytick=\empty,xtick=\empty,ztick=\empty,
    clip=false, axis lines = middle
    ]
    % read out the transformation done by pgfplots
    \GetLocalFrame
    \begin{scope}[transform shape]
    \addplot3[only marks, fill=cyan,mark=fcirc] 
    table {\datatable};
    \end{scope}
    \addplot3[thick, red] table[y={create col/linear regression={y=Y}}] {\datatable}; % compute a linear regression from the input table

    \def\X{2.7}
    \def\Y{23}
    \draw [-{Latex[length=4mm, width=2mm]}] (\X,\Y+10,12.5) node[right]{$(x_1,y_1)$} ..controls (0,5) .. (\X,\Y,0);
    \draw [-{Latex[length=4mm, width=2mm]}] (9,30,20) node[left, align=right]{\scriptsize True Regression Line\\ \scriptsize $y = \beta_0 + \beta_1 x$} .. controls (5,2.5) .. (5,22.7,0); 
    \draw [decorate, decoration={brace,amplitude=3pt}, xshift=0.5mm] (\X,\Y-0.1,0) to (\X,17,0) node[left, xshift=5mm, yshift=-1mm]{\scriptsize 1}; % brace 

    \draw [thick,dash pattern={on 7pt off 2pt on 1pt off 3pt}] (1,17.1) to (\X,17.1);
    \draw [thick,dash pattern={on 7pt off 2pt on 1pt off 3pt}] (\X,17.1) -- (\X,5);
    \node[above] at (\X,4) {$x_1$};
    \node[right, align=left,yshift=0.5mm] at (1,17.1) {$E(Y|x_1)=\mu_{Y.x_1}$};

\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

旧答案:这不是答案,而只是为了向您展示进行投影后会得到什么。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{3d,calc}
% small fix for canvas is xy plane at z % https://tex.stackexchange.com/a/48776/121799
\makeatletter
\tikzoption{canvas is xy plane at z}[]{%
    \def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
    \def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
    \def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
    \tikz@canvas@is@plane}
\makeatother
\pgfplotsset{compat=1.15}
\pgfplotstableread{
X Y Z m
2.2 14 0 0
2.7 23 0 0
3 13 0 0
3.55 22 0 0
4 15 0 0
4.5 20 0 0
4.75 28 0 0
5.5 23 0 0
}\datatable
\pgfplotstableread{
X Y Z m
2.2 0 0 0
2.7 0 0 0
3 13 0 0
3.55 0 0 0
4 15 0 0
4.5 0 0 0
4.75 0 0 0
5.5 0 0 0
}\datatabletwo

%\makeatletter
        \pgfdeclareplotmark{dot}
        {%
            \fill circle [x radius=0.02, y radius=0.08];
        }%
%\makeatother

        \pgfdeclareplotmark{fcirc}
        {%
          \begin{scope}[x={(-21.20514pt,-9.26361pt)},
          y={(2.54181pt,-1.57715pt)},z={(0.0pt,6.04706pt)}]
          \begin{scope}[canvas is xy plane at z=0,transform shape]
            \fill circle(0.1);
          \end{scope}   
          \end{scope}
        }%


\begin{document}

\section{table using raw data in 3D}

The below diagram tries to replicate in 3D, the Figure 12.3 found in \cite{devore} , page 472 \\

% https://tex.stackexchange.com/questions/11251/trend-line-or-line-of-best-fit-in-pgfplots
\begin{tikzpicture}[scale=1.5]
\begin{axis}
    [   
    view={140}{50},
    xmin=1,xmax=6, ymin=5,ymax=40, zmin=0, zmax=10,
    % ytick=\empty,xtick=\empty,ztick=\empty,
    clip=false, axis lines = middle
    ]
    % read out the transformation done by pgfplots
    \path let \p1=($(1,0,0)-(0,0,0)$), \p2=($(0,1,0)-(0,0,0)$),
    \p3=($(0,0,1)-(0,0,0)$) in \pgfextra{\typeout{
    \x1,\y1;\x2,\y2;\x3,\y3}}; 
    \begin{scope}[transform shape]
    \addplot3[only marks, fill=cyan,mark=fcirc] 
    table {\datatable};
    \end{scope}
    \addplot3[thick, red] table[y={create col/linear regression={y=Y}}] {\datatable}; % compute a linear regression from the input table

    \def\X{2.7}
    \def\Y{23}
%     \draw [-{Latex[length=4mm, width=2mm]}] (\X,\Y+10,12.5) node[right]{$(x_1,y_1)$} ..controls (0,5) .. (\X,\Y,0);
%     \draw [-{Latex[length=4mm, width=2mm]}] (9,30,20) node[left, align=right]{\scriptsize True Regression Line\\ \scriptsize $y = \beta_0 + \beta_1 x$} .. controls (5,2.5) .. (5,22.7,0); 
%     \draw [decorate, decoration={brace,amplitude=3pt}, xshift=0.5mm] (\X,\Y-0.1,0) to (\X,17,0) node[left, xshift=5mm, yshift=-1mm]{\scriptsize 1}; % brace 
% 
%     \draw [thick,dash pattern={on 7pt off 2pt on 1pt off 3pt}] (1,17.1) to (\X,17.1);
%     \draw [thick,dash pattern={on 7pt off 2pt on 1pt off 3pt}] (\X,17.1) -- (\X,5);
%     \node[above] at (\X,4) {$x_1$};
%     \node[right, align=left,yshift=0.5mm] at (1,17.1) {$E(Y|x_1)=\mu_{Y.x_1}$};

\end{axis}
\end{tikzpicture}




  \begin{thebibliography}{1}
  \bibitem{devore} Jay. L Devore {\em Probability and Statistics for Engineering and the Sciences} 8th Edition.
  \end{thebibliography}


\end{document}

在此处输入图片描述

现在,圆已正确投影到 xy 平面上,但不幸的是,由于 x 和 y 刻度看起来非常不同,它们看起来像椭圆。这就是您想要的吗?

另一方面,如果你想要消除椭圆失真,你可以通过以下方法来实现:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{3d,calc,decorations.pathreplacing,arrows.meta}
% small fix for canvas is xy plane at z % https://tex.stackexchange.com/a/48776/121799
\makeatletter
\tikzoption{canvas is xy plane at z}[]{%
    \def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
    \def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
    \def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
    \tikz@canvas@is@plane}
\makeatother
\pgfplotsset{compat=1.15}
\pgfplotstableread{
X Y Z m
2.2 14 0 0
2.7 23 0 0
3 13 0 0
3.55 22 0 0
4 15 0 0
4.5 20 0 0
4.75 28 0 0
5.5 23 0 0
}\datatable
\pgfplotstableread{
X Y Z m
2.2 0 0 0
2.7 0 0 0
3 13 0 0
3.55 0 0 0
4 15 0 0
4.5 0 0 0
4.75 0 0 0
5.5 0 0 0
}\datatabletwo

%\makeatletter
        \pgfdeclareplotmark{dot}
        {%
            \fill circle [x radius=0.02, y radius=0.08];
        }%
%\makeatother

        \pgfdeclareplotmark{fcirc}
        {%
          \begin{scope}[x={(-21.20514pt,-9.26361pt)},
          y={(7.73369*2.54181pt,-7.73369*1.57715pt)},z={(0.0pt,6.04706pt)}]
          \begin{scope}[canvas is xy plane at z=0,transform shape]
            \fill circle(0.1);
          \end{scope}   
          \end{scope}
        }%


\begin{document}

\section{table using raw data in 3D}

The below diagram tries to replicate in 3D, the Figure 12.3 found in \cite{devore} , page 472 \\

% https://tex.stackexchange.com/questions/11251/trend-line-or-line-of-best-fit-in-pgfplots
\begin{tikzpicture}[scale=1.5]
\begin{axis}
    [   
    view={140}{50},
    xmin=1,xmax=6, ymin=5,ymax=40, zmin=0, zmax=10,
    % ytick=\empty,xtick=\empty,ztick=\empty,
    clip=false, axis lines = middle
    ]
    % read out the transformation done by pgfplots
    \path let \p1=($(1,0,0)-(0,0,0)$), \p2=($(0,1,0)-(0,0,0)$),
    \p3=($(0,0,1)-(0,0,0)$) in \pgfextra{
    \pgfmathsetmacro{\ratio}{veclen(\x1,\y1)/veclen(\x2,\y2)}
    \typeout{
    \x1,\y1;\x2,\y2;\x3,\y3;\ratio}}; 
    \begin{scope}[transform shape]
    \addplot3[only marks, fill=cyan,mark=fcirc] 
    table {\datatable};
    \end{scope}
    \addplot3[thick, red] table[y={create col/linear regression={y=Y}}] {\datatable}; % compute a linear regression from the input table

    \def\X{2.7}
    \def\Y{23}
    \draw [-{Latex[length=4mm, width=2mm]}] (\X,\Y+10,12.5) node[right]{$(x_1,y_1)$} ..controls (0,5) .. (\X,\Y,0);
    \draw [-{Latex[length=4mm, width=2mm]}] (9,30,20) node[left, align=right]{\scriptsize True Regression Line\\ \scriptsize $y = \beta_0 + \beta_1 x$} .. controls (5,2.5) .. (5,22.7,0); 
    \draw [decorate, decoration={brace,amplitude=3pt}, xshift=0.5mm] (\X,\Y-0.1,0) to (\X,17,0) node[left, xshift=5mm, yshift=-1mm]{\scriptsize 1}; % brace 

    \draw [thick,dash pattern={on 7pt off 2pt on 1pt off 3pt}] (1,17.1) to (\X,17.1);
    \draw [thick,dash pattern={on 7pt off 2pt on 1pt off 3pt}] (\X,17.1) -- (\X,5);
    \node[above] at (\X,4) {$x_1$};
    \node[right, align=left,yshift=0.5mm] at (1,17.1) {$E(Y|x_1)=\mu_{Y.x_1}$};

\end{axis}
\end{tikzpicture}




  \begin{thebibliography}{1}
  \bibitem{devore} Jay. L Devore {\em Probability and Statistics for Engineering and the Sciences} 8th Edition.
  \end{thebibliography}


\end{document}

在此处输入图片描述

不幸的是,由于 pgfplots 的工作方式,您需要运行它,找出转换和“作弊规模” \ratio,然后将其插入到定义中,fcircle以防您进行任何更改。

相关内容