在 pgfplots 中旋转文本

在 pgfplots 中旋转文本

有什么更好的方法可以实现以下结果?我想添加旋转的标签,但我必须有线条\addplot[black] coordinates {...};才能\draw (axis cs:...工作并生成旋转的标签。有一个明显的问题是三角形的线条被追踪了两次。

\documentclass{article}

\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{center}
\begin{tikzpicture} 

%45-45-90
\begin{axis}[name=plot1,
x=4cm,y=4cm,clip=false,
hide x axis, hide y axis]
\addplot[black] coordinates {(0,0)(1,0)(1,1)(0,0)};
\addplot[black] coordinates {(0.9,0)(0.9,0.1)(1,0.1)};
\draw (axis cs:0,0)--(axis cs:1,0) node[anchor=north, pos=0.5]{Adjacent};
\draw (axis cs:1,0)--(axis cs:1,1) node[anchor=north, rotate=90, pos=0.5]{Opposite};
\draw (axis cs:0,0)--(axis cs:1,1) node[anchor=south, rotate=45, pos=0.5]{Hypotenuse};
\node at (axis cs:0.185,0.077) {\(\theta\)};
\end{axis}

%30-60-90
\begin{axis}[name=plot2,at={($(plot1.east)+(6em,0)$)},anchor=west,
x=4cm,y=4cm,clip=false,
hide x axis, hide y axis]
\addplot[black] coordinates {(0,0)(1,0)(1,0.577)(0,0)};
\addplot[black] coordinates {(0.9,0)(0.9,0.1)(1,0.1)};
\draw (axis cs:0,0)--(axis cs:1,0) node[anchor=north, pos=0.5]{Adjacent};
\draw (axis cs:1,0)--(axis cs:1,0.577) node[anchor=north, rotate=90, pos=0.5]{Opposite};
\draw (axis cs:0,0)--(axis cs:1,0.577) node[anchor=south, rotate=30, pos=0.5]{Hypotenuse};
\node at (axis cs:0.193,0.052) {\(\theta\)};
\end{axis}

\end{tikzpicture}
\end{center}

\end{document}

姓名

答案1

环境axis不知道其大小,当\addplot不使用时,并且,,,xmin必须明确给出,例如:xmaxyminymax

xmin=0, xmax=1,
ymin=0, ymax=1,

但由于节点的存在,边界框仍然不正确。

\path通过使用而不是\draw来放置节点可以避免线路重复。

但我认为不用会更容易axis,例如:

\documentclass{article}

\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=newest}

\begin{document}

\begin{center}
%45-45-90
\begin{tikzpicture}[
  x=4cm, y=4cm,
]
  \draw[thin]
    (.9, 0) -- (.9, .1) -- (1, .1)
    (.25, 0) arc(0:45:.25)
    (22.5:.18) node {$\theta$}
  ;
  \draw[semithick]
    (0, 0) -- node[anchor=north] {Adjacent}
    (1, 0) -- node[anchor=north, sloped] {Opposite}
    (1, 1) -- node[anchor=south, sloped] {Hypothenuse}
    cycle
  ;

%30-60-90
\begin{scope}[shift={($(current bounding box.east |- 0, 0) + (6em, 0)$)}]
  \draw[thin]
    (.9, 0) -- (.9, .1) -- (1, .1)
    (.25, 0) arc(0:30:.25)
    (15:.18) node {$\theta$}
  ;
  \draw[semithick]
    (0, 0) -- node[anchor=north] {Adjacent}
    (1, 0) -- node[anchor=north, sloped] {Opposite}
    (1, {tan(30)}) -- node[anchor=south, sloped] {Hypothenuse}
    (0, 0) -- cycle
  ;
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}

结果

提示:

  • 0.577TikZ 可以计算,例如,可以代替,{tan(30)}作为坐标的一部分给出。

  • sloped比用角度指定更简单rotate,请参阅 John Kormylo 的评论

  • 连接的线段不应使用\draw单线分隔。这样线连接会很差。应在单个\draw命令中给出这些线,并使用封闭路径cycle

    \draw (0, 0) -- (1, 0) -- (1, 1) -- cycle;
    

    然后线连接会好得多,并且也可以通过选项进行配置line join

  • 额外的(0, 0)before-- cycle应该不是必要的,但似乎与转移范围有关。如果没有,节点会错误地放置在右侧。

带有垂直居中图画的版本

\documentclass{article}

\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=newest}

\begin{document}

\begin{center}
%45-45-90
\begin{tikzpicture}[
  x=4cm, y=4cm,
  baseline=(current bounding box.center),
]
  \draw[thin]
    (.9, 0) -- (.9, .1) -- (1, .1)
    (.25, 0) arc(0:45:.25)
    (22.5:.18) node {$\theta$}
  ;
  \draw[semithick]
    (0, 0) -- node[anchor=north] {Adjacent}
    (1, 0) -- node[anchor=north, sloped] {Opposite}
    (1, 1) -- node[anchor=south, sloped] {Hypothenuse}
    cycle
  ;
\end{tikzpicture}%
\hspace{6em}%
%30-60-90
\begin{tikzpicture}[
  x=4cm, y=4cm,
  baseline=(current bounding box.center),
]
  \draw[thin]
    (.9, 0) -- (.9, .1) -- (1, .1)
    (.25, 0) arc(0:30:.25)
    (15:.18) node {$\theta$}
  ;
  \draw[semithick]
    (0, 0) -- node[anchor=north] {Adjacent}
    (1, 0) -- node[anchor=north, sloped] {Opposite}
    (1, {tan(30)}) -- node[anchor=south, sloped] {Hypothenuse}
    cycle
  ;
\end{tikzpicture}
\end{center}
\end{document}

结果垂直居中

答案2

pgfplots用于绘图。当您需要绘制某些内容时,请使用适当的工具。这是tikzGonzalo 建议的。没有同质性损失,您无需加载,tikz因为它会自行加载pgfplots。代码更简单。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{angles,quotes,calc}
\begin{document}
     \begin{tikzpicture}
        \coordinate (A) at (0,0);
        \coordinate (B) at (3,3);
        \coordinate (C) at (3,0);
        \draw (A) -- node[above,sloped]{Hypotenuse} (B)
                    -- node[below,rotate=180,sloped] {Opposite} (C)
                    -- node[below] {Adjacent} (A);
        \path pic[draw, angle radius=5mm,"$\theta$",angle eccentricity=1.5] {angle = C--A--B};
        \draw ($(C)!3mm!(B)$) -| ($(C)!3mm!(A)$);
     \end{tikzpicture}
\end{document} 

在此处输入图片描述

相关内容