I'm creating a dynamic LaTeX (LuaLaTeX) script, which is creating a plot out of a given CSV file. Settings like xmin, xmax etc. depend on that CSV file.
That CSV file contains the storage utilization of a hard drive, together with date time stamps.
The domain of the plot reaches from two years ago to one year in the future. The black plot shows the CSV data, the thick red like is the linear regression line, expanded in both directions (with the "shorten"-key).
The linear regression is created with this code:
\addplot [line width=10pt, opacity=.3, red, shorten >= -10cm, shorten <= -10cm] table [
x index=5,
y={create col/linear regression={
x=JulianDayMod,
y="size",
}}
] {\loadedtable};
Since the CSV has date-formats, I needed to convert it beforehand with this script: Linear regression with dates on x axis in pgfplots
Now I need the y value of the linear regression at xmax. Do you know how to get it?
(its also important to know that xmax is a date as well, like "2022-11-23")
It should be displayed in the legend where I've put x
as placeholder.
I already know that you can access the coefficents of the linear regression with \pgfplotstableregressiona
and \pgfplotstableregressionb
.
And that I can access xmax with \pgfkeysgetvalue{/pgfplots/xmax}{\xmax}
But I couldn't find any solution online for this…
Best Answer
After some trying I found the solution!
(I already had the correct idea in mind but this didn't work in the first run...)
Recap: A linear function has this function:
a ยท x + b
And
pgfplotstable
lets you accessa
with\pgfplotstableregressiona
andb
with\pgfplotstableregressionb
of the linear regression plot.
So I used this code to access the value: (xmin was previously defined)
Note:
That code
\luaexec{ tex.sprint ( string.format ( "\%.2f" , \var ) ) }
rounds my value to two decimal places. It only works with LuaLaTeX and you need to add\usepackage{luacode}
to your code.Note 2:
You can print the function of the linear regression with this:
Note 3:
I added a MWE example here: (you need LuaLaTeX in order to run it)
Result: