![3D 平面上的文本](https://linux22.com/image/414083/3D%20%E5%B9%B3%E9%9D%A2%E4%B8%8A%E7%9A%84%E6%96%87%E6%9C%AC.png)
我无法在 3D 中正确地沿平面投影文本。完整代码和图片已附上。xz 平面中 x = 0 $p_Y(y)$ 处的红色文本和 yz 平面中 y = 3 $p_X(x)$ 处的蓝色文本不在平面上,即未投影在平面上。
请指导。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\newcommand\PrintFrac[2][3]{%
\pgfkeys{/pgf/number format/frac,/pgf/number format/frac denom=#1}%
\pgfmathprintnumber{#2}}
\begin{document}
\begin{center}
\begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}
[ scale = 2,
view={55}{30},
ticklabel style = {font=\large},
xlabel={\huge $X$},
ylabel={\huge $Y$},
zlabel={\huge $p_{XY}(x,y)$},
zlabel style={rotate=0},
xmin=0,xmax=3,
xtick={0,1,2,3},
ymin=0,ymax=3,
ytick={0,1,2,3},
zmin=0, zmax=0.9,
ztick={0,0.4,0.8},
axis background/.style={fill=white},
xmajorgrids=true,
ymajorgrids=true,
zmajorgrids=true,
grid style=dashed,
visualization depends on={value \thisrow{m} \as \labela},
]
\addplot3 [ycomb, ultra thick, scatter, mark = text,
mark options={text mark=\PrintFrac{\labela},
text mark as node=true,
text mark style={scale=1.25, yshift=13pt}
}] table {
x y m
1 1 0.33
2 1 0.33
1 2 0.33
};
\addplot3 [ycomb, ultra thick, scatter,color=red, mark = text,
mark options={text mark=\PrintFrac{\labela},
text mark as node=true,
text mark style={scale=1.25, yshift=13pt}
}] table {
x y m
0 1 0.67
0 2 0.33
};
\addplot3 [ycomb,ultra thick, scatter,color=blue, mark = text,
mark options={text mark=\PrintFrac{\labela},
text mark as node=true,
text mark style={scale=1.25, yshift=13pt}
}] table {
x y m
1 3 0.67
2 3 0.33
};
\node at (0,1.5,0.7) [red] {$p_Y(y)$};
\node at (2,3,0.7) [blue] {$p_X(x)$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
答案1
原则上这相当简单。加载3d
库并将键添加canvas is yz plane at x=0,
到节点和text mark style
。但问题是 pgfplots 会进行额外的比例变换,而 Ti钾Z 看不到。所以我们需要将节点比例因子(您认为是)更改1.25
为较小的值。我还必须从scale=2
您的图中删除,以便设置一些width
,这样overfull hbox
警告就会消失。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{3d}
\newcommand\PrintFrac[2][3]{%
\pgfkeys{/pgf/number format/frac,/pgf/number format/frac denom=#1}%
\pgfmathprintnumber{#2}}
\begin{document}
\begin{center}
\begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[
width=0.95\textwidth,
view={55}{30},
ticklabel style = {font=\large},
xlabel={\large $X$},
ylabel={\large $Y$},
zlabel={\large $p_{XY}(x,y)$},
zlabel style={rotate=0},
xmin=0,xmax=3,
xtick={0,1,2,3},
ymin=0,ymax=3,
ytick={0,1,2,3},
zmin=0, zmax=0.9,
ztick={0,0.4,0.8},
axis background/.style={fill=white},
xmajorgrids=true,
ymajorgrids=true,
zmajorgrids=true,
grid style=dashed,
visualization depends on={value \thisrow{m} \as \labela},
]
\addplot3 [ycomb, ultra thick, scatter, mark = text,
mark options={text mark=\PrintFrac{\labela},
text mark as node=true,
text mark style={scale=1.25, yshift=13pt}
}] table {
x y m
1 1 0.33
2 1 0.33
1 2 0.33
};
\addplot3 [ycomb, ultra thick, scatter,color=red, mark = text,
mark options={text mark=\PrintFrac{\labela},
text mark as node=true,
text mark style={canvas is yz plane at x=0,
scale=0.2, yshift=13pt}
}] table {
x y m
0 1 0.67
0 2 0.33
};
\addplot3 [ycomb,ultra thick, scatter,color=blue, mark = text,
mark options={text mark=\PrintFrac{\labela},
text mark as node=true,
text mark style={canvas is xz plane at y=0,
scale=0.2, yshift=13pt}
}] table {
x y m
1 3 0.67
2 3 0.33
};
\begin{scope}[canvas is yz plane at x=0]
\node at (1.5,0.7) [red,transform shape,scale=0.2] {$p_Y(y)$};
\end{scope}
\begin{scope}[canvas is xz plane at y=3]
\node at (2,0.7) [blue,transform shape,scale=0.2] {$p_X(x)$};
\end{scope}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}