我正在尝试制作一个图表来展示主成分分析的性质。我有以下代码,其中我想在主成分向量下旋转花括号以指定其长度。
我目前拥有的代码如下:
\begin{tikzpicture}[thick,scale=0.9, every node/.style={scale=0.9}]
\draw[line width = 0.5mm, ->] (0, 0) -- (5, 0) node[right]{$z(x_{0})$};
\draw[line width = 0.5mm, ->] (0, 0) -- (0, 5) node[above]{$z(x_{1})$};
\draw[line width = 0.5mm, ->] (2.75, 2.75) -- (1.6, 3.8) node[above right]{$\overrightarrow{\text{pc}}_{1}$};
\draw[line width = 0.5mm, ->] (2.75, 2.75) -- (5, 5) node[above left]{$\overrightarrow{\text{pc}}_{0}$};
\clip[rotate=45] (4, 0) ellipse (2.5 and 1.0);
\foreach \p in {1,...,100}
{ \fill[black, rotate = 45] (4 + 2.5*rand,1*rand) circle (0.05);
}
\end{tikzpicture}
其结果如下:
我想要的是以下内容:
有办法吗?我对 Tikz 还很陌生,所以任何指点都很棒!
非常感谢!
答案1
欢迎来到 TeX.SE!这可以通过装饰来实现brace
。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[thick,scale=0.9, every node/.style={scale=0.9}]
\draw[line width = 0.5mm, ->] (0, 0) -- (5, 0) node[right]{$z(x_{0})$};
\draw[line width = 0.5mm, ->] (0, 0) -- (0, 5) node[above]{$z(x_{1})$};
\draw[line width = 0.5mm, ->] (2.75, 2.75) -- (1.6, 3.8) node[above right]{$\overrightarrow{\text{pc}}_{1}$};
\draw[line width = 0.5mm, ->] (2.75, 2.75) -- (5, 5) node[above left]{$\overrightarrow{\text{pc}}_{0}$};
\draw[thick,decorate,decoration={brace,raise=3pt}] (2.75, 2.75) -- (1.6, 3.8)
node[midway,below left=2pt]{$\lambda$};
\draw[thick,decorate,decoration={brace,raise=3pt,mirror}] (2.75, 2.75) -- (5, 5)
node[midway,below right=2pt]{$\lambda$};
\clip[rotate=45] (4, 0) ellipse (2.5 and 1.0);
\foreach \p in {1,...,100}
{ \fill[black, rotate = 45] (4 + 2.5*rand,1*rand) circle (0.05);
}
\end{tikzpicture}
\end{document}
如果您想缩短屏幕截图中的括号,请使用例如
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[thick,scale=0.9, every node/.style={scale=0.9}]
\draw[line width = 0.5mm, ->] (0, 0) -- (5, 0) node[right]{$z(x_{0})$};
\draw[line width = 0.5mm, ->] (0, 0) -- (0, 5) node[above]{$z(x_{1})$};
\draw[line width = 0.5mm, ->] (2.75, 2.75) -- (1.6, 3.8) node[above right]{$\overrightarrow{\text{pc}}_{1}$}
coordinate[pos=0.9] (aux1);
\draw[line width = 0.5mm, ->] (2.75, 2.75) -- (5, 5) node[above left]{$\overrightarrow{\text{pc}}_{0}$}
coordinate[pos=0.95] (aux2);
\draw[thick,decorate,decoration={brace,raise=3pt}] (2.75, 2.75) -- (aux1)
node[midway,below left=2pt]{$\lambda$};
\draw[thick,decorate,decoration={brace,raise=3pt,mirror}] (2.75, 2.75) -- (aux2)
node[midway,below right=2pt]{$\lambda$};
\clip[rotate=45] (4, 0) ellipse (2.5 and 1.0);
\foreach \p in {1,...,100}
{ \fill[black, rotate = 45] (4 + 2.5*rand,1*rand) circle (0.05);
}
\end{tikzpicture}
\end{document}
我可能会在周围\lambda
和支架上添加一个小光环,以防止情节混乱。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{amsmath}
\usepackage[outline]{contour}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[thick,scale=0.9, every node/.style={scale=0.9}]
\draw[line width = 0.5mm, ->] (0, 0) -- (5, 0) node[right]{$z(x_{0})$};
\draw[line width = 0.5mm, ->] (0, 0) -- (0, 5) node[above]{$z(x_{1})$};
\draw[line width = 0.5mm, ->] (2.75, 2.75) -- (1.6, 3.8) node[above right]{$\overrightarrow{\text{pc}}_{1}$}
coordinate[pos=0.9] (aux1);
\draw[line width = 0.5mm, ->] (2.75, 2.75) -- (5, 5) node[above left]{$\overrightarrow{\text{pc}}_{0}$}
coordinate[pos=0.95] (aux2);
\begin{scope}
\pgfmathsetseed{23}
\clip[rotate=45] (4, 0) ellipse (2.5 and 1.0);
\foreach \p in {1,...,100}
{ \fill[black, rotate = 45] (4 + 2.5*rand,1*rand) circle (0.05);
}
\end{scope}
\draw[thick,white,double=black,decorate,decoration={brace,raise=3pt}] (2.75, 2.75) -- (aux1)
node[midway,below left=2pt,black]{\contour{white}{$\lambda$}};
\draw[thick,white,double=black,decorate,decoration={brace,raise=3pt,mirror}] (2.75, 2.75) -- (aux2)
node[midway,below right=2pt,black]{\contour{white}{$\lambda$}};
\end{tikzpicture}
\end{document}
但这当然只是一种选择。
答案2
{
可以使用标准数学:
\documentclass{article}
\usepackage{amsmath}
\usepackage{lmodern}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick,scale=0.9, every node/.style={scale=0.9}]
\draw[line width = 0.5mm, ->] (0, 0) -- (5, 0) node[right]{$z(x_{0})$};
\draw[line width = 0.5mm, ->] (0, 0) -- (0, 5) node[above]{$z(x_{1})$};
\draw[line width = 0.5mm, ->] (2.75, 2.75) -- (1.6, 3.8) node[above right]{$\overrightarrow{\text{pc}}_{1}$};
\draw[line width = 0.5mm, ->] (2.75, 2.75) -- (5, 5) node[above left]{$\overrightarrow{\text{pc}}_{0}$};
\node[rotate=-45] at (4.1,3.5) {\fontsize{30pt}{30pt}\selectfont$\Bigg\}$};
\node[rotate=229] at (1.8,3) {\fontsize{30pt}{30pt}\selectfont$\big\}$};
\clip[rotate=45] (4, 0) ellipse (2.5 and 1.0);
\foreach \p in {1,...,100}
{ \fill[black, rotate = 45] (4 + 2.5*rand,1*rand) circle (0.05);
}
\end{tikzpicture}
\end{document}