I have been gathering data from hygrometers for several weeks, observing their drift. Now I want to plot the data. This is my first attempt and how the data looks like (I hope this is working, I cannot test for filecontents, because TL2021 won’t let me install it any more, since TL 2022 is out, and I haven’t had the time to upgrade, yet):
\documentclass[pagesize]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{test.csv}
t;30.5005, 0916;30.5005, 0818;30.3054.10;30.3208.02, Ch1;30.3208.02, Ch2;30.3208.02, Ch3
2022-03-31 08:56:09;56;59;60;60;59;58
2022-03-31 09:47:22;45;47;52;54;52;53
2022-03-31 10:14:44;48;52;54;55;54;53
2022-03-31 16:00:55;43;46;50;50;50;50
2022-04-01 00:16:09;41;41;49;49;48;49
2022-04-01 00:18:07;42;42;49;49;48;49
2022-04-01 22:01:30;42;46;52;52;51;51
2022-04-02 16:15:50;40;48;51;52;51;51
2022-04-02 20:17:47;38;45;51;51;51;50
2022-04-04 17:44:12;32;40;46;47;46;46
2022-04-06 21:14:45;40;49;52;52;51;51
2022-04-08 11:01:51;42;51;53;55;54;53
2022-04-08 12:00:06;41;51;54;55;53;54
2022-04-08 12:35:03;38;46;50;52;50;51
2022-04-20 00:29:28;33;41;46;47;45;45
2022-04-20 11:42:43;47;55;56;56;54;54
2022-04-20 12:48:44;40;48;51;52;50;51
2022-04-20 13:44:00;35;42;47;50;48;49
\end{filecontents*}
\usepackage{amsmath}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{units}
\newcommand{\xlabelstrut}{\rule[-0.7\baselineskip]{0pt}{1.8\baselineskip}}
\pgfplotsset{%
compat=1.16,%
scale only axis,%
axis x line* = bottom,%
axis y line* = left,%
every axis/.append style = {line width=0.5pt},%
tick align = center,%
tick style={line width = 0.5pt, color = black},%
tick label style={font=\footnotesize, /pgf/number format/.cd, use comma, 1000 sep={}},%
every axis x label/.style={at={(ticklabel* cs:1.02)},anchor=west,},%
every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south,},%
label style={font=\footnotesize},%
grid = both,%
every axis plot/.append style={%
line join = round,%
},%
%
legend style = {draw = none, font = \footnotesize},%
legend cell align = left,%
legenderechtsauszen/.style = {legend style = {at = {(1.01,0.5)}, anchor = west}},%
diagramm/.style = {%
log ticks with fixed point,%
},%
hygrovgl/.style = {%
diagramm,%
width = 120mm,%
height = 80mm,%
legenderechtsauszen,%
xmin = 1,%
xmax = 19,%
xtick distance = 1,%
ymax = 70,%
ymin = 10,%
minor y tick num = 4,%
xlabel = {sample\xlabelstrut},%
ylabel = {$\varphi$/\%},%
},%
}
\pgfplotstableread[col sep = semicolon]{test.csv}\data
\begin{document}
\begin{minipage}{210mm}
\centering\vspace{8mm}
\begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[hygrovgl]
\pgfplotsinvokeforeach{1,...,6}{%
\pgfplotstablegetcolumnnamebyindex{#1}\of{\data}\to{\colname}%
\addplot+ plot [error bars/.cd, y dir=both, y fixed = {5}] table [x expr = \coordindex + #1/7 + 1, y index = #1]{\data};%
\addlegendentryexpanded{\colname};%
}
\end{axis}
\end{tikzpicture}
\vspace{8mm}
\end{minipage}
\end{document}
I removed cycle lists for this MWE, but apart from that it’s close to what I want.
My first idea was to have date and time on the x axis, but somehow it didn’t work and I hadn’t much time, so I ended up using x expr = \coordindex
, which gave me an easy way of moving the data points slightly apart by adding an offset term to the index, so you can actually see them.
When I finally managed to use the t column instead of \coordindex
, I saw what I already had anticipated: This (obviously) results in a real waste of space, making the x axis a time axis, and shifting the data points to their respective positions in time, which renders data of the same day impossible to read and leaves big gaps during those days during which I didn’t gather data.
So I would like to stick with \coordindex
and read the xticklabels from the data (from here):
\documentclass[pagesize]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{test.csv}
t;30.5005, 0916;30.5005, 0818;30.3054.10;30.3208.02, Ch1;30.3208.02, Ch2;30.3208.02, Ch3
2022-03-31 08:56:09;56;59;60;60;59;58
2022-03-31 09:47:22;45;47;52;54;52;53
2022-03-31 10:14:44;48;52;54;55;54;53
2022-03-31 16:00:55;43;46;50;50;50;50
2022-04-01 00:16:09;41;41;49;49;48;49
2022-04-01 00:18:07;42;42;49;49;48;49
2022-04-01 22:01:30;42;46;52;52;51;51
2022-04-02 16:15:50;40;48;51;52;51;51
2022-04-02 20:17:47;38;45;51;51;51;50
2022-04-04 17:44:12;32;40;46;47;46;46
2022-04-06 21:14:45;40;49;52;52;51;51
2022-04-08 11:01:51;42;51;53;55;54;53
2022-04-08 12:00:06;41;51;54;55;53;54
2022-04-08 12:35:03;38;46;50;52;50;51
2022-04-20 00:29:28;33;41;46;47;45;45
2022-04-20 11:42:43;47;55;56;56;54;54
2022-04-20 12:48:44;40;48;51;52;50;51
2022-04-20 13:44:00;35;42;47;50;48;49
\end{filecontents*}
\usepackage{amsmath}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{units}
\newcommand{\xlabelstrut}{\rule[-0.7\baselineskip]{0pt}{1.8\baselineskip}}
\pgfplotsset{%
compat=1.16,%
scale only axis,%
axis x line* = bottom,%
axis y line* = left,%
every axis/.append style = {line width=0.5pt},%
tick align = center,%
tick style={line width = 0.5pt, color = black},%
tick label style={%
font=\footnotesize,%
% /pgf/number format/.cd, use comma, 1000 sep={}%
},%
every axis x label/.style={at={(ticklabel* cs:1.02)},anchor=west,},%
every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south,},%
label style={font=\footnotesize},%
grid = both,%
every axis plot/.append style={%
line join = round,%
},%
%
legend style = {draw = none, font = \footnotesize},%
legend cell align = left,%
legenderechtsauszen/.style = {legend style = {at = {(1.01,0.5)}, anchor = west}},%
diagramm/.style = {%
log ticks with fixed point,%
},%
hygrovgl/.style = {%
diagramm,%
width = 120mm,%
height = 80mm,%
legenderechtsauszen,%
xmin = 1,%
xmax = 19,%
xtick distance = 1,%
ymax = 70,%
ymin = 10,%
minor y tick num = 4,%
flexible xticklabels from table = {test.csv}{t}{col sep = semicolon},%
xticklabel style = {rotate = 90, anchor = near xticklabel}, % To make sure the text labels are nicely aligned
xtick = data,%
xlabel = {sample\xlabelstrut},%
ylabel = {$\varphi$/\%},%
},%
}
\makeatletter
\pgfplotsset{
/pgfplots/flexible xticklabels from table/.code n args={3}{%
\pgfplotstableread[#3]{#1}\coordinate@table
\pgfplotstablegetcolumn{#2}\of{\coordinate@table}\to\pgfplots@xticklabels
\let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x
}
}
\makeatother
\pgfplotstableread[col sep = semicolon]{test.csv}\data
\begin{document}
\begin{minipage}{210mm}
\centering\vspace{8mm}
\begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[hygrovgl]
\pgfplotsinvokeforeach{1,...,6}{%
\pgfplotstablegetcolumnnamebyindex{#1}\of{\data}\to{\colname}%
\addplot+ plot [error bars/.cd, y dir=both, y fixed = {5}] table [x expr = \coordindex + #1/7 + 1, y index = #1]{\data};%
\addlegendentryexpanded{\colname};%
}
\end{axis}
\end{tikzpicture}
\vspace{8mm}
\end{minipage}
\end{document}
Why do I have to remove /pgf/number format/.cd, use comma, 1000 sep={}
if I want to rotate the xticklabels?
How can I format the xticklabels? Something like xticklabel = \day.\,\month.
doesn’t work, unless I use date coordinates in = x
. I could of course do the formatting in the data, but I would like to keep it in this form, so PGF can handle it as data, if needed.
It seems that xmin
and xmax
are being ignored. Why is the grid shifted to the right, now?
Can I have the xticks (and labels) right in the middle of the x grid lines, so that they label the respective group of data points?
This is what the second MWE from above looks like:
I wonder why the grid has shifted a bit to the right, note detail 1. On the left edge there is a bit of space left to the first vertical line, which isn’t the case in the first MWE (see second image below, too), and on the right edge, the last vertical line has disappeared. The first data point of each sample/timestamp is now right on its vertical grid line, even though I am giving it a horizontal offset. Note the increasing offsets of data points within one sample group. The groups are seperated by the vertical grid lines.
Apart from that: Is there a way, to move the xticks half the distance of the vertical grid lines to the right, as indicated by detail 2?
Here is why I moved away from using the timestamps as data for the x axis (same data as above):
It makes it impossible to see which data point is within the uncertainty of which other point, how the readings are drifting, and so on.