2

What is a better way to achieve the following result? I want to add the rotated labels, but I have to have the \addplot[black] coordinates {...}; line in order for \draw (axis cs:... to work and produce the rotated labels. There is the obvious problem that the lines of the triangles are being traced twice.

\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}

name

  • 2
    Why are you using pgfplots for this? The natural choice here is pure TikZ. – Gonzalo Medina Sep 8 '15 at 13:23
  • I have a number of other pgfplots diagrams in my document, and I want to use it throughout for the sake of homogeneity. – Arcturus Sep 8 '15 at 13:26
  • @Eridan pgfplots is built on TikZ and actually loads TikZ (so you can remove \usepackage{tikz} in your code). There is no homogeneity issue. – percusse Sep 8 '15 at 13:32
  • Thanks. I should also add that pgfplots is the only graphic-producing program I learned. I don't know how to use straight-up tikz. – Arcturus Sep 8 '15 at 13:36
  • \draw and \node are plain tikz, although (axis cs:...) is special to pgfplots. – John Kormylo Sep 8 '15 at 14:13
2

The axis environment does not know its size, when \addplot is not used, and xmin, xmax, ymin, ymax must be given explicitly, e.g.:

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

But the bounding box is still not correct because of the nodes.

The doubling the lines can be avoided by using \path instead of \draw to place the nodes.

But I think it is easier to go without axis, for example:

\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}

Result

Hints:

  • TikZ can calculate, e.g. instead of 0.577, {tan(30)} can be given as part of the coordinate.

  • sloped is simpler than specifying rotate with angle, see John Kormylo's comment.

  • Connected line segments should not be separated by \draw with a single line. Then the line connections are quite bad. The lines should be given in a single \draw command and closed paths should be use cycle:

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

    Then the line joins are much better and can also be configured via option line join.

  • The additional (0, 0) before -- cycle should not be necessary, but there seems to be a bug in connection with the shifted scope. Without, the node is misplaced to the right.

Version with vertically centered drawings

\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}

Result vertically centered

3

pgfplots is meant for plotting. When you have to draw something, use the proper tool. Here it is tikz as suggested by Gonzalo. There is no loss of homogeneity and you don't have to load tikz as it is loaded by pgfplots itself. The code is simpler.

\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} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.