1

I'm making a lot of plots with pgfplots/groupplots/tikz from csv files, using \addplot[...] table[...]{file.csv} or \nextgroupplot[...] table[...]{file.csv}

Some files have both positive and negative x-coordinates, some have all positive.

I'd like to set xmin=0 if all the coordinates are positive, and have xmin determined automatically if the coordinates are negative - so xmin is always 0 or lower, and all data is always shown.

Is there a way to determine this automatically, without just looking at the data and adding xmin=0 to those that don't cross the axis? Something like if(automatically_determined_xmin>0){xmin=0}, that would preferably go in \begin{groupplot}[this bit] to avoid repeating it in every \nextgroupplot[one of these]

MWE:

\documentclass{article}[11pt]
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.groupplots}
\begin{document}

\begin{figure}[htbp]
    \centering
    \begin{tikzpicture}
        \begin{groupplot}[
                group style={
                    group size=2 by 1
                    },
                axis y line=middle,
                axis x line=bottom,
                x=3cm,
                xtick distance=0.5
            ]
        \nextgroupplot[]
        \addplot[] table[x=x,y=y]{
            x y
            -0.1 1
            1 -1
            };
        \nextgroupplot[]
        \addplot[] table[x=x,y=y]{
            x y
            0.9 1
            1 -1
            };
        \end{groupplot}
    \end{tikzpicture}
\end{figure}
\end{document}

two plots in a groupplot, the first crosses zero, the second doesn't A major symptom of the problem is that the second plot only has 1 x-label, so doesn't have a sense of scale. Also I'd like the y-axis to go through x=0. In reality there are enough of this type of figures that I could miss one by doing things manually

2
  • Would be easier for answerers if you include a minimal (non)working example.
    – user202729
    May 24, 2022 at 23:39
  • edited with mwe
    – sqek
    May 25, 2022 at 0:58

2 Answers 2

1

Answering myself for reference because I've found a solution (but not quite ideal because it needs an addition to every \nextgroupplot): Add \addplot[mark=none] coordinates { (0,0) }; to each plot, and leave xmin unset.

[mark=none] still leaves the line enabled, but it looks like it doesn't draw a line with only one point, so there's no extra floating dot at the origin or similar.

1

This answer use some undocumented feature/trick, and I do not fully understand what is happening. There is no guarantee that it will keep working and it does not break anything.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{pgfplots.groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 1
},
axis y line=middle,
axis x line=bottom,
x=3cm,
xtick distance=0.5,
execute at end survey={\pgfmathparse{\csname pgfplots@xmin\endcsname<0?\csname pgfplots@xmin\endcsname:0} \expandafter\global\expandafter\let\csname pgfplots@xmin\endcsname=\pgfmathresult},
]
\nextgroupplot[]
\addplot[] table[x=x,y=y]{
x y
-0.1 1
1 -1
};
\nextgroupplot[]
\addplot[] table[x=x,y=y]{
x y
0.9 1
1 -1
};
\end{groupplot}
\end{tikzpicture}
\end{document}

Two plots

I would use

\addplot[mark=none, forget plot] coordinates { (0,0) };

as in your own answer - notice forget plot so that it is not counted as a real plot in e.g. the legend.

You must log in to answer this question.

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