1

To continue my question here now I'd like to understand how to use ymax, ymin etc. values in new variables made with pgfmathparse or pgfmathsetmacro. So, if I know \pgfkeysvalueof{/pgfplots/ymax} and \pgfkeysvalueof{/pgfplots/ymin} how to set new variable \eh being (ymax - ymin)/10? This code:

\pgfmathsetmacro{\eh}{0.1*\pgfkeysvalueof{/pgfplots/ymax} -0.1*\pgfkeysvalueof{/pgfplots/ymin}}

gives the error message: Missing number, treated as zero. I'd like to use this \eh as a height of rectangle or in other calculations of coordinates.

EDIT:

Added MWE as asked.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{siunitx}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.14}


\begin{document}
\begin{tikzpicture}
\pgfkeys{
    /pgf/number format/precision=1, 
    /pgf/number format/fixed zerofill=true }
   \begin{axis}[
                 xmin=1,
                 xmax=4,
                 grid=both,
                 clip=false
                 ]

        \addplot[thick,red,domain=\pgfkeysvalueof{/pgfplots/xmin}:{\pgfkeysvalueof{/pgfplots/xmax}/1.3}] {x^2};
        \draw [black, thick] (1,12.7) -- (2.5,12.7);
        \draw [blue, thick] (0,\pgfkeysvalueof{/pgfplots/ymax}+0.3*\pgfkeysvalueof{/pgfplots/ymax}-0.3*\pgfkeysvalueof{/pgfplots/ymin}) -- (4,\pgfkeysvalueof{/pgfplots/ymax}+0.3*\pgfkeysvalueof{/pgfplots/ymax}-0.3*\pgfkeysvalueof{/pgfplots/ymin});
    \end{axis}
\end{tikzpicture}


\end{document}

This is based on @koleygr answer. In this particular example ymin=1, ymax=10 and they are not set explicitly by user. Hence blue and black lines should have the same y-coordinate, but they are not. And how to simplify input by setting a new variable with all that staff \pgfkeysvalueof{/pgfplots/ymax}+0.3*\pgfkeysvalueof{/pgfplots/ymax}-0.3*\pgfkeysvalueof{/pgfplots/ymin}?

Ideally I woild like to place objects (lines, rectangles etc.) above the main graph (above y=10 in this particular example) and the distance these objects are shifted from ymax should depend on the size of the main graph, i.e. y-coordinate-of-objects = ymax + 0.3*(ymax - ymin).

Alx
  • 737
  • As you can see in my answer I suppose that your problem was that you did not used a \xdef to store \eh... \eh can be seen only from inside your tikzpicture (that is a separate environment) and is lost outside. I add some code to let you keep precision in your calculations and to be able to print outside of the plot area (I can comment my code if needed) – koleygr Sep 01 '17 at 06:17
  • 1
    It sounds like you should just be using rel axis cs. I mean, the y-coordinate in your last line there should be the same as that of rel axis cs:0,1.3. – Torbjørn T. Sep 03 '17 at 06:57
  • Your MWE works out of the box when using PGFPlots v1.16. Do you agree and if yes, is it ok for you when we close this question as "of-topic" because updating to the recent version solves your problem? – Stefan Pinnow Jun 04 '18 at 17:21
  • Yes, I agree, it works now. – Alx Jun 05 '18 at 03:07
  • 4
    I'm voting to close this question because it is solved by updating PGFPlots to the most recent version (1.16). – Stefan Pinnow Jun 05 '18 at 19:49

1 Answers1

0

This is how you can use it and many things you have to use and know for something like this:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{siunitx}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.14}


\begin{document}
\begin{tikzpicture}
\pgfkeys{
    /pgf/number format/precision=1, 
    /pgf/number format/fixed zerofill=true }
   \begin{axis}[
                 ymin=1,
                 ymax=10,
                 xmin=1,
                 xmax=4,
                 grid=both,
                 clip=false
                 ]
        \pgfmathsetmacro{\eh}{0.3*\pgfkeysvalueof{/pgfplots/ymax} -0.3*\pgfkeysvalueof{/pgfplots/ymin}}
        \xdef\ehg{\num[round-mode=places, round-precision=1]{\eh}}
        \addplot[thick,red,domain=\pgfkeysvalueof{/pgfplots/xmin}:{\pgfkeysvalueof{/pgfplots/xmax}/1.3}] {x^2};
        \draw[-,thick,green] ($(\pgfkeysvalueof{/pgfplots/xmin},\eh)$) node[left] (A) {\ehg}
        --($(\pgfkeysvalueof{/pgfplots/xmax},\eh)$);
    \end{axis}
\end{tikzpicture}

value of macro=\ehg
\end{document}

Result:

enter image description here

koleygr
  • 20,105
  • Thanks for your answer, but ... This doesn't work if don't know (and explicitly set) in advance values of ymin and ymax. Just comment ymin=1 and ymax=10 and this leads to the same error message about missing number. I thought these values become known to PGF after \addplot[...]{x^2}, and we can extract them somehow. – Alx Sep 01 '17 at 07:35
  • Please add a MWE to saw what you want to do. (Of course you have to know the values you want to use...) If you mean to find automatically some values of a tikzpicture it depends on the tikzpicture. – koleygr Sep 01 '17 at 07:43
  • Please comment (or delete) strings ymin=1 and ymax=10 in your answer and this will give an error. Even if we move strings \pgfmathsetmacro ... and \xdef ... after \addplot in your answer this doesn't help also. – Alx Sep 01 '17 at 08:09
  • Please add an example of what you want to do... dont care if working or not... Just let as see your picture. Of course it will not work without the values. I don't have to test it. – koleygr Sep 01 '17 at 08:18