Math & Sciencepgfplots - Define a extra x tick for the x interaction of a

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
simande
Posts: 3
Joined: Wed Aug 26, 2015 12:13 pm

pgfplots - Define a extra x tick for the x interaction of a

Post by simande »

I have a regression line of some points and need a `extra x tick` at the x interaction of this regression. I could just calculate the value and make the tick by `extra x ticks={x1}` but I have to make lots of different curves and I'd like to have a prototype, so I only have to create some .txt documents and the rest is made automaticly by Tex.

Is it possible by using the `\pgfplotstableregressiona` and `\pgfplotstableregressionb` terms?

This is how it looks like:

Code: Select all

    \documentclass{article}
    \usepackage{pgfplots}
    \pgfplotsset{%
        ,compat=1.12
        ,every axis legend/.append style={%
            ,at={(0.03,0.97)}
            ,anchor=north west}
            }
    \usepackage{pgfplotstable}
    
    \begin{document} 
    \begin{tikzpicture}
        \begin{axis}[%
            ,width=10cm,height=10cm
            ,x tick label style={/pgf/number format/1000 sep=}
            ,xmin=0,xmax=0.6
            ,ymin=0,ymax=22 
            ,xlabel=Weg/mm, 
            ,ylabel=Kraft/N,
            ]
            \addplot+[%
                ,fill = blue
                ,mark = *
                ,mark options={solid}
                ,only marks
                ] table {%
                    x       y
                    0.400   18.096
                    0.400   18.401
                    0.400   17.767
                    0.400   16.992
                    0.400   16.399
                    0.397   15.908
                    0.392   15.440
                    0.387   14.533
                    0.382   13.258
                    0.377   11.939      
                    };
            \addlegendentry{Kraft-Weg-Kurve}
            \addplot [] table [%
                ,y={create col/linear regression}] {%
                    x       y
                    0.400   18.096
                    0.400   18.401
                    0.400   17.767
                    0.400   16.992
                    0.400   16.399
                    0.397   15.908
                    0.392   15.440
                    0.387   14.533
                    0.382   13.258
                    0.377   11.939
                    };  
            \addplot [draw,domain=0.25:0.5] (x,\pgfplotstableregressiona*x+\pgfplotstableregressionb);
            \addlegendentry{Regression {$\pgfmathprintnumber[precision=4, fixed zerofill]{\pgfplotstableregressiona} \cdot \mathrm{t} \pgfmathprintnumber[precision=4, fixed zerofill]{\pgfplotstableregressionb}$}}  
        \end{axis}
    \end{tikzpicture}   
    \end{document}
Thank you very much :)

Image

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

pgfplots - Define a extra x tick for the x interaction of a

Post by cgnieder »

site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

pgfplots - Define a extra x tick for the x interaction of a

Post by cgnieder »

I don't know if this is a recommended solution. There may well be a better one…

You can set

Code: Select all

extra x ticks={\myextratick}
and add

Code: Select all

\pgfmathparse{-1 * \pgfplotstableregressionb / \pgfplotstableregressiona}
\let\myextratick\pgfmathresult
after adding the corresponding table:

Code: Select all

\begin{tikzpicture}
    \begin{axis}[%
        ...
        extra x ticks={\myextratick}
        ]
        \addplot [] table [%
            ,y={create col/linear regression}] {%
                x       y
                0.400   18.096
                0.400   18.401
                0.400   17.767
                0.400   16.992
                0.400   16.399
                0.397   15.908
                0.392   15.440
                0.387   14.533
                0.382   13.258
                0.377   11.939
                };
        \pgfmathparse{-1 * \pgfplotstableregressionb / \pgfplotstableregressiona}
        \let\myextratick\pgfmathresult
        ...
    \end{axis}
\end{tikzpicture}
Regards
site moderator & package author
simande
Posts: 3
Joined: Wed Aug 26, 2015 12:13 pm

Re: pgfplots - Define a extra x tick for the x interaction o

Post by simande »

Thank you, it's working just fine.

btw. is posting the same question on different boards not appreciated? In this case, sorry :roll:
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

pgfplots - Define a extra x tick for the x interaction of a

Post by cgnieder »

simande wrote:btw. is posting the same question on different boards not appreciated?
It depends: some forums really don't like it and forbid it in their board rules. Others don't mind too much. E.g., on golatex it's not allowed (see paragraph 1 at the end). I remember that they have closed threads in the past when they found out it was a crosspost but I believe it's not that strict any more. :)

In any case links to crossposts are very much appreciated or even demanded by the board rules. Here we actually do not mind very much but we'd like to see a link if a post is asked on other platforms, too. See section 4.1 of the board rules.

This way anyone who wants to answer can check if the question already has an answer on the other forum.

Regards
site moderator & package author
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

pgfplots - Define a extra x tick for the x interaction of a

Post by Johannes_B »

On goLaTeX, crossposting is forbidden in theory, but ok if a link is posted and the crosspost is transparent.

Spending time to write an answer, just to find out 10 minutes later, that the very same question was answered elsewhere means just one thing: A helper has wasted some amount of time.

Please also have a look at Gimme pizza.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply