0

So I made this graph, and this graph can't show me the tick $\frac{5}{2}\pi$ at 7.85.

I've tried to enlarge the x max range, give extra ticks (and extra tick label) but nothing works. Anyone can help me? Thanks!

\documentclass[10pt,openany,a4paper]{article}
\usepackage[margin=1.5cm]{geometry}
\usepackage{a4wide}
\usepackage{amsfonts,amsmath,amssymb}
\usepackage{array}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{tkz-euclide}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[english]{babel}

\begin{document}

\begin{tikzpicture}
    \begin{axis} [
        axis line style = thick,
        axis x line = middle,
        axis y line = middle,
        xtick = {-3.14, -1.57, 0., ..., 7.85, 9.42},
        ytick = {-1, 0, 1},
        xlabel = {$\theta$},
        ylabel = {$y$},
        xticklabels = {$-\pi$, $-\frac{\pi}{2}$, , $\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$, $\frac{5\pi}{2}$, $3\pi$},
        xmin = -3.5, xmax = 10,
        ymin = -1.5, ymax = 1.5,
        font = \tiny,
    ]

    \addplot[black, thin, samples = 1000, smooth, domain=-3.14:0] {cos(deg(x))};
    \addplot[cyan, thin, samples = 1000, smooth, domain=0:3.140000] {cos(deg(x))};
    \addplot[black, thin, samples = 1000, smooth, domain=3.14:8.5] {cos(deg(x))};
    \end{axis}
\end{tikzpicture}
\end{document}
3
  • Your code generates errors. Please post a working example.
    – user263192
    Commented May 13, 2022 at 17:01
  • Replacing your xtick={...} with xtick={-3.14,-1.57,...,9.42} works for me (which avoids the rounding errors). Commented May 17, 2022 at 15:34
  • @StefanPinnow It worked too on me! Thank you so much
    – Wilory Lu
    Commented May 18, 2022 at 5:59

1 Answer 1

2

By lucky chance, you could get your desired list of ticks if you'd remove some of the intermediate values and use xtick = {-3.14, -1.57, ..., 9.42}.

Why does this work? The above list will be expanded to

-3.14 
-1.57 
-0.00002 
1.56998 
3.13997 
4.70996 
6.27995 
7.84995 
9.41994

As you can see, the floating point precision of pgf is not that good. By pure luck, the last tikz is calculated to be 9.41994, which is lower than the upper boundary set to 9.42.

The list from your question, xtick = {-3.14, -1.57, 0., ..., 7.851, 9.42}, evaluates to

-3.14 
-1.57
0.
1.57 
3.14001 
4.71002 
6.28003 
9.42

Notice the absence of a value around 7.85. This tick is missing because pgf would calculate the next step in the list to be 7.85004, which is above the upper limit of 7.85.

You could avoid the problem by leaving a bit of margin for rounding errors with xtick = {-3.14, -1.57, 0., ..., 7.851, 9.42}.

7
  • @samcarter, although it should be clear what to do in case one would not be lucky with the end of the given xtick range, maybe you should just add that it would be enough the increase the last number a bit, e.g. in this case to 9.43 to also make the last tick (+ label) visible/show up. Commented May 18, 2022 at 14:52
  • @StefanPinnow Isn't that already covered in "You could avoid the problem by leaving a bit of margin for rounding errors with xtick = {-3.14, -1.57, 0., ..., 7.851, 9.42}."? Commented May 18, 2022 at 15:14
  • thanks for your explanation!
    – Wilory Lu
    Commented May 19, 2022 at 6:04
  • @samcarter_is_at_topanswers.xyz, in principle yes, but then I would apply that suggestion/advice as well to the "final" solution. That is I would use/show instead of 9.42 minimum 9.43. Maybe also simply 9.5 or 10. The only requirement would be to be smaller than "the next step". I'd do that, because maybe (I didn't check) you get the shown evaluated tick values by TeXing with pdfLaTeX, but would yield something larger than 9.42 when TeXing with e.g. LuaLaTeX. Commented May 19, 2022 at 11:12
  • Of course your solution is fine as is. My suggestion would just make it a bit more "general". Commented May 19, 2022 at 11:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .