1

I have a simple plot with two lines:

\nextgroupplot[
    no markers,
    ymin=-0.25,
    ymax=2.25,
    ytick={0, 1, 2},
    yticklabels={state one, other state ,third state},
]

\addplot+ [
        const plot mark right,
        % name this path to later be able to find an intersection on it
        name path=first,
    ] coordinates { (0,0) (200,0) (300,1) (500,2) };
   \addplot+ [
        const plot mark right,
        % name this path to later be able to find an intersection on it
        name path=second,
    ] coordinates { (0,1) (150,0) (250,1) (250,2) (450,2) (500, 1) };

That looks like:

enter image description here

I want the lines to stack on top of each other when they have same state (making it look like the line gets bolder):

enter image description here

(If there were 100 lines it would get a lot taller and if lines would be thicker it would look line a rainbow =))

How would one do it with tikz and pgf?

  • This approach does not fit my needs due to it showing how to shrink - not stack lines on overlapping - I really have more than 100 lines overlapping from time to time. – DuckQueen Mar 15 '17 at 2:04
  • I think you should link this question to your related previous question, so that the readers get the context. In addition, as in your previous question, please add a complete code and not just a fragment. This is a common requirement on this site. – Dr. Manuel Kuehner Mar 15 '17 at 2:13
  • Regarding your question, I don't think that there is an out-of-the-box solution. But maybe others here are more creative :). – Dr. Manuel Kuehner Mar 15 '17 at 2:15
  • 100 lines mean 100 different colours. Even stacking (thin) lines on top of each other might not be enough to distinguish between them. For few lines and few colours (up to 5 maybe) I'd suggest using different line widths, starting with the thickest one and overplotting with decreasing line widths. – AlexG Mar 15 '17 at 11:27
1

I guess there will be no automatic way in LaTeX (and friends) to keep track of when how may lines overlap and shift them accordingly only at the overlapping areas. That is why suggest to just shift the whole lines.

For more details please have a look at the comments in the code.

% used PGFPlots v1.14
% (this is an answer to the follow-up question of 
%  <http://tex.stackexchange.com/q/357358>)
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=10cm,
        height=2cm,
        scale only axis,
        axis lines=left,
        xmin=0,
        xmax=500,
        no markers,
        ymin=-0.25,
        ymax=2.25,
        ytick={0, 1, 2},
        yticklabels={state one, other state ,third state},
        % (moved common `\addplot' options here)
        const plot mark right,
    ]
            % -----------------------------------------------------------------
            % for now please ignore these commands.
            % (Later it will be referenced and then you can/should read the
            %  comments on these commands)
            % -----------------------------------------------------------------
            % define a variable to count the number of `\addplot's
            % and initialize it here to zero
            \pgfmathtruncatemacro{\PlotNum}{0}
            % -----------------------------------------------------------------
        \addplot+ [
            % -----------------------------------------------------------------
            % please skip this option too, if you first come here
            % -----------------------------------------------------------------
            % of course this option should be used at *every* `\addplot'
            % command, also the first one. This has the advantage that you
            % could also apply a negative number to the initial value of
            % `\PlotNum', so that the "middle" `\addplot' command is at the
            % same height as the corresponding `yticks'
            yshift=\PlotNum*\pgflinewidth,
            % -----------------------------------------------------------------
        ] coordinates { (0,0) (200,0) (300,1) (400,2) (500,0) };

            % now just increase the value by one each time a new `addplot' follows
            \pgfmathtruncatemacro{\PlotNum}{\PlotNum+1}
        \addplot+ [
            % when you don't change the linewidth, you can simply shift the
            % plot by the (default/set) linewidth
            yshift=\pgflinewidth,
%            % (for the default linewidth `thin' this would be 0.4pt, as is
%            %  written in the pgfmanual (v3.0.1a) in section 15.3.1 (on page 166))
%            yshift=0.4pt,
        ] coordinates { (0,1) (150,0) (450,2) (500, 1) };

            \pgfmathtruncatemacro{\PlotNum}{\PlotNum+1}
        \addplot+ [
            green,
%            % with changing `linewidth' one would have to do a manual approach
%            % like the following ...
%            % -----
%            thick,
%            % half linewidth of the first `\addplot'
%            % + sum of all linewidth of the "middle" `\addplot's
%            % + half the linewidth of this `\addplot'
%            yshift=0.2pt + 0.4pt + 0.4pt,
            % -----------------------------------------------------------------
%            % ... but if the `linewidth' doesn't change you can simply do ...
%            yshift=\plotnum*\pgflinewidth,
            % ... which unfortunately doesn't work, because in this context
            % `\plotnum' doesn't seem to be not known. This is, why we simulate
            % this by  defining `\PlotNum' which can be found before each
            % `\addplot' command and use it here
            yshift=\PlotNum*\pgflinewidth,
        ] coordinates { (0,1) (100,0) (250,1) (350,2) (500, 0) };

    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.