addplot3 标记问题和视图理解

addplot3 标记问题和视图理解

3D 视图图片

我已将讲义中的三幅图xelatex脚本和三幅图 jpg 添加到其中。问题是我无法以addplot3分数形式显示标记。图 2 和图 3 中的 2D 图也是如此。如果您看到 3D 图 1,我想在一个位置显示 x 和 y 的原点,我无法调整视图。

最后,我无法在 xz 平面和 yz 平面上显示文本。我知道它的视图调整,但无法做到。

如果有人能在这方面帮助我,请告知。数据文件很简单,不包含在代码中,但可以从图表中理解。

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\usetikzlibrary{3d}
\usepackage{amsmath}
\usepackage{filecontents}

\begin{filecontents}{s.dat}
x   y   m 
1   1   0.33
2   1   0.33 
1   2   0.33 
\end{filecontents}

\begin{filecontents}{st.dat}
x   y   m
0   1   0.67
0   2   0.33 
\end{filecontents}

\begin{filecontents}{sth.dat}
x   y   m
1   4   0.67
2   4   0.33
\end{filecontents}

\begin{filecontents}{data.dat}
 n   xn 
1   0.67
2   0.33 
\end{filecontents}

\begin{filecontents}{Second.dat}
 n   xn 
1   0.67
2   0.33 
\end{filecontents}

\begin{document}

\begin{tikzpicture}

\begin{axis}
[   scale = 2,
    view={45}{45},
    ticklabel style = {font=\large},
    xlabel={\huge $X$}, 
    ylabel={\huge $Y$},
    zlabel={\huge $p_{XY}(x,y)$},
    zlabel style={rotate=0},
    xmin=0,xmax=5,
    xtick={0,1,2,3,4},
    ymin=0,ymax=4,
    ytick={0,1,2,3,4},
    zmin=0, zmax=0.9,
    ztick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9},
    axis background/.style={fill=white},
    xmajorgrids=true,
    ymajorgrids=true,
    zmajorgrids=true,
    grid style=dashed,
    visualization depends on={value \thisrow{m} \as \labela},
]
    \addplot3 [ycomb,scatter, mark = text,
              mark options={text mark=\labela,
                            text mark as node=true,
                            text mark style={scale=1.25, yshift=13pt}
                            }] table {s.dat};

\addplot3 [ycomb,scatter,color=red, mark = text,
              mark options={text mark={\labela},
                            text mark as node=true,
                            text mark style={scale=1.25, yshift=13pt}
                            }] table {st.dat};
\addplot3 [ycomb,scatter,color=orange, mark = text,
              mark options={text mark={\labela},
                            text mark as node=true,
                            text mark style={scale=1.25, yshift=13pt}
                            }] table {sth.dat};
\end{axis}

\end{tikzpicture}

\newpage
\begin{tikzpicture}
\begin{axis}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    scale=2,
    ticklabel style = {font=\huge, color=blue},
    axis x line=middle,
    axis y line=middle,
    every axis x label={at={(current axis.right of origin)},anchor=north west},
    every axis y label={at={(current axis.above origin)},anchor= north west},
    every axis plot post/.style={mark options={fill=black},font=\huge},   
    xmin=0,
    xmax=4, 
    xtick={0, 1, 2, 3},    
    xticklabels={$0$, $1$, $2$, $3$},
    % extra x tick style={ xticklabel style={yshift=0.5ex, anchor=south} },
    xlabel={\huge $\boldsymbol{x}$},
    ylabel={\huge $\boldsymbol{p_X(x)}$},
    ytick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7},   
    ymin=0,
    ymax=0.7,
    visualization depends on={value \thisrow{xn} \as \labela},
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\addplot+[ycomb,black,very thick,
      mark=text,
      text mark=\labela,
      text mark as node,
      text mark style={
        xshift=-9pt,
        yshift=20pt,
        font=\huge,
        }] table [x={n}, y={xn}] {data.dat};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    scale=2,
    ticklabel style = {font=\huge},
    axis x line=middle,
    axis y line=middle,
    every axis x label={at={(current axis.right of origin)},anchor=north west},
    every axis y label={at={(current axis.above origin)},anchor= north west},
    every axis plot post/.style={mark options={fill=black}},   
    xmin=0,
    xmax=4, 
    xtick={0, 1, 2, 3},    
    xticklabels={$0$, $1$, $2$, $3$},
    % extra x tick style={ xticklabel style={yshift=0.5ex, anchor=south} },
    xlabel={\huge $\boldsymbol{y}$},
    ylabel={\huge $\boldsymbol{p_Y(y)}$},
    ytick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7},   
    ymin=0,
    ymax=0.7,
    visualization depends on={value \thisrow{xn} \as \labela},
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\addplot+[ycomb,black, very thick, mark=text,
      text mark=\labela,
      text mark as node,
      text mark style={
        xshift=-9pt,
        yshift=20pt,
        font=\huge}] table [x={n}, y={xn}] {Second.dat};
\end{axis}
\end{tikzpicture}
\end{document}


\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{60}{110}
\begin{tikzpicture}[tdplot_main_coords,>=stealth,declare function={%
pfft(\x)=pi+0.3*sin(deg(\x));}]
 \draw[->] (0,0,0) coordinate (O) -- (5,0,0) coordinate(X) node[pos=1.1]{$x$};
 \draw[->] (O) -- (0,5,0) node[pos=1.1]{$y$};
 \draw[->] (O) -- (0,0,5) node[pos=1.1]{$z$};
 \draw[thick] plot[variable=\x,domain=0.8*pi:1.1*pi,smooth] (2.2,\x,{pfft(2*\x)}) 
 coordinate (T1)--
 plot[variable=\x,domain=1.1*pi:0.8*pi,smooth] (0.8,\x,{pfft(2*\x)}) coordinate
 (T3)-- cycle;
 \draw (2.2,0.8*pi,0) coordinate (B4)-- (2.2,1.1*pi,0) coordinate (B1)
 -- (0.8,1.1*pi,0) coordinate (B2) -- (0.8,0.8*pi,0) coordinate (B3) -- cycle;
 \path (2.2,0.8*pi,{pfft(2*0.8*pi)}) coordinate (T4)
  (0.8,1.1*pi,{pfft(2*1.1*pi)}) coordinate (T2);
 \foreach \X in {1,...,4}
 {\draw (B\X) -- (T\X);}
 \path[opacity=0.3,left color=blue,right color=blue,middle color=blue!20,shading
  angle=72]
  plot[variable=\x,domain=0:1.1*pi,smooth] (3,\x,{pfft(2*\x)}) --
  plot[variable=\x,domain=1.1*pi:0,smooth] (0,\x,{pfft(2*\x)}) -- cycle;
 \path[opacity=0.3,left color=blue,right color=blue,middle color=blue!20,shading
  angle=72]
  plot[variable=\x,domain=1.1*pi:2.2*pi,smooth] (3,\x,{pfft(2*\x)}) --
  plot[variable=\x,domain=2.2*pi:1.1*pi,smooth] (0,\x,{pfft(2*\x)}) -- cycle;
 \draw plot[variable=\x,domain=0:2.2*pi,smooth] (3,\x,{pfft(2*\x)}) --
 plot[variable=\x,domain=2.2*pi:0,smooth] (0,\x,{pfft(2*\x)}) -- cycle;
\end{tikzpicture}
\end{document}

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{patterns}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{110}
\begin{tikzpicture}[scale=3, tdplot_main_coords,line cap=round,>=stealth,
    declare function={f(\x,\y)=3+0.5*sin(30*\x)*cos(30*\y);}]
 \draw[->] (0,0,0) coordinate (O) -- (6,0,0) node[pos=1.05]{$x$};
 \node at (0.05,-0.15,0) [left,rotate=45]  {$x+\delta_1 x$};
 \node at (1,-0.15,0) [left,rotate=45]  {$\delta_1$};
 \node at (1.5,-0.15,0) [left,rotate=45]  {$x$};
 \draw[->] (O) -- (0,6,0) node[pos=1.05]{$y$};
 \node at (-0.25,2,0) [left,rotate=0]  {$y$};
 \node at (-0.25,3.3,0) [left,rotate=-5]  {$y+\delta_2 y$};
 \node at (-0.25,2.5,0) [left,rotate=0]  {$\delta_2$};
 \draw[->] (O) -- (0,0,5) node[pos=1.05]{$z$};
 \draw [pattern=vertical lines, pattern color=gray] (1,2,0) -- (2,2,0)   -- (2,3,0) -- (1,3,0) -- cycle;
 \foreach \X in {1,2} {\foreach \Y in {2,3} 
  {\draw[dashed] (\X,\Y,0) -- (\X,\Y,{f(\X,\Y)});}}    
 \foreach \X in {1,2} {
  \draw[dashed] (\X,2,0) -- (\X,0,0);}
 \foreach \Y in {2,3} {
  \draw[dashed] (1,\Y,0) -- (0,\Y,0);}
 \foreach \X in {0,...,4}
  {\draw plot[variable=\x,domain=0:5,smooth] (\X,\x,{f(\X,\x)});
  }
 \foreach \Y in {0,...,5}
  {\draw plot[variable=\x,domain=0:4,smooth] (\x,\Y,{f(\x,\Y)});
  }
 \draw[gray,thick,pattern=vertical lines, pattern color=gray] plot[variable=\x,domain=1:2,smooth]  (\x,2,{f(\x,2)})
    -- plot[variable=\y,domain=2:3]  (2,\y,{f(2,\y)})
    -- plot[variable=\x,domain=2:1]  (\x,3,{f(\x,3)})
    -- plot[variable=\y,domain=3:2]  (1,\y,{f(1,\y)})
    -- cycle;
 \draw (4,0,{f(4,0)}) -- (4,0,0);
 \draw (0,5,{f(0,5)}) -- (0,5,0);
 \draw (4,5,{f(4,5)}) -- (4,5,0);
 \draw (0,5,0) -- (4,5,0);
 \draw (4,0,0) -- (4,5,0);
 \draw (3,5,{f(3,5)}) -- (3,5,0);
 \draw (2,5,{f(2,5)}) -- (2,5,0);
 \draw (1,5,{f(1,5)}) -- (1,5,0);
 \draw [<-] (0.75,1,{f(0.75,1)}) -- (-3,1,3) node [pos=1.125] {$f_{XY}(x,y)$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

感谢您提供可编译的代码!至于“将标记作为分数”问题,您可以使用

\pgfkeys{/pgf/number format/frac,/pgf/number format/frac denom=3}

连同\pgfmathprintnumber。为了您的方便,我将其做成了宏。

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\usetikzlibrary{3d}
\usepackage{amsmath}
\usepackage{filecontents}

\begin{filecontents}{s.dat}
x   y   m 
1   1   0.33
2   1   0.33 
1   2   0.33 
\end{filecontents}

\begin{filecontents}{st.dat}
x   y   m
0   1   0.67
0   2   0.33 
\end{filecontents}

\begin{filecontents}{sth.dat}
x   y   m
1   4   0.67
2   4   0.33
\end{filecontents}

\begin{filecontents}{data.dat}
 n   xn 
1   0.67
2   0.33 
\end{filecontents}

\begin{filecontents}{Second.dat}
 n   xn 
1   0.67
2   0.33 
\end{filecontents}
\newcommand\PrintFrac[2][3]{%
\pgfkeys{/pgf/number format/frac,/pgf/number format/frac denom=#1}%
\pgfmathprintnumber{#2}}

\begin{document}

\begin{tikzpicture}

\begin{axis}
[   scale = 2,
    view={45}{45},
    ticklabel style = {font=\large},
    xlabel={\huge $X$}, 
    ylabel={\huge $Y$},
    zlabel={\huge $p_{XY}(x,y)$},
    zlabel style={rotate=0},
    xmin=0,xmax=5,
    xtick={0,1,2,3,4},
    ymin=0,ymax=4,
    ytick={0,1,2,3,4},
    zmin=0, zmax=0.9,
    ztick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9},
    axis background/.style={fill=white},
    xmajorgrids=true,
    ymajorgrids=true,
    zmajorgrids=true,
    grid style=dashed,
    visualization depends on={value \thisrow{m} \as \labela},
]


    \addplot3 [ycomb,scatter, mark = text,
              mark options={text mark=\PrintFrac{\labela},
                            text mark as node=true,
                            text mark style={scale=1.25, anchor=south}
                            }] table {s.dat};

\addplot3 [ycomb,scatter,color=red, mark = text,
              mark options={text mark=\PrintFrac{\labela},
                            text mark as node=true,
                            text mark style={scale=1.25,  anchor=south}
                            }] table {st.dat};
\addplot3 [ycomb,scatter,color=orange, mark = text,
              mark options={text mark=\PrintFrac{\labela},
                            text mark as node=true,
                            text mark style={scale=1.25, anchor=south}
                            }] table {sth.dat};
\end{axis}

\end{tikzpicture}

\newpage
\begin{tikzpicture}
\begin{axis}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    scale=2,
    ticklabel style = {font=\huge, color=blue},
    axis x line=middle,
    axis y line=middle,
    every axis x label={at={(current axis.right of origin)},anchor=north west},
    every axis y label={at={(current axis.above origin)},anchor= north west},
    every axis plot post/.style={mark options={fill=black},font=\huge},   
    xmin=0,
    xmax=4, 
    xtick={0, 1, 2, 3},    
    xticklabels={$0$, $1$, $2$, $3$},
    % extra x tick style={ xticklabel style={yshift=0.5ex, anchor=south} },
    xlabel={\huge $\boldsymbol{x}$},
    ylabel={\huge $\boldsymbol{p_X(x)}$},
    ytick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7},   
    ymin=0,
    ymax=0.7,
    visualization depends on={value \thisrow{xn} \as \labela},
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\addplot+[ycomb,black,very thick,
      mark=text,
      text mark=\PrintFrac{\labela},
      text mark as node,
      text mark style={anchor=south,
        font=\huge,
        }] table [x={n}, y={xn}] {data.dat};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}
[%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    scale=2,
    ticklabel style = {font=\huge},
    axis x line=middle,
    axis y line=middle,
    every axis x label={at={(current axis.right of origin)},anchor=north west},
    every axis y label={at={(current axis.above origin)},anchor= north west},
    every axis plot post/.style={mark options={fill=black}},   
    xmin=0,
    xmax=4, 
    xtick={0, 1, 2, 3},    
    xticklabels={$0$, $1$, $2$, $3$},
    % extra x tick style={ xticklabel style={yshift=0.5ex, anchor=south} },
    xlabel={\huge $\boldsymbol{y}$},
    ylabel={\huge $\boldsymbol{p_Y(y)}$},
    ytick={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7},   
    ymin=0,
    ymax=0.7,
    visualization depends on={value \thisrow{xn} \as \labela},
]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\addplot+[ycomb,black, very thick, mark=text,
      text mark=\PrintFrac{\labela},
      text mark as node,
      text mark style={anchor=south,
        font=\huge}] table [x={n}, y={xn}] {Second.dat};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

在此处输入图片描述

此外,您可以只调整这些节点的锚点,而不必进行所有硬编码的移位,请参见上面的代码。

但是,我不明白问题的第二部分。你指的是哪些文本?

相关内容