6

I want to plot a large set of data points generated from a numerical experiment. The plot looks like a step function, that is, for an interval in x-axis, it takes a constant value. The code is (for space constraint a small sample of data points is given)

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotstableread{
    X     Y
    0.9020688   1
    0.933219    1
    1.1420047   2
    1.154532    2
    1.2753  3
    1.2865  3
    1.3649  4
    1.3844  4
    1.4358  5
    1.4746  5
    1.5 6
    1.6 6
    1.5658954   7
    1.75    7
    1.6461004   8
    1.75    8
}\Rtable    
\begin{document}
\begin{tikzpicture} []
\begin{axis}[xlabel={$X$},ylabel={$Y$},xmin=0.6, xmax=1.8, ymin=0, ymax=8.5  ]
\addplot[mark=x,color=blue,const plot,jump mark right] table[y = Y] from \Rtable ;
\end{axis}
\end{tikzpicture}
\end{document}

In the generated plot, the line segment extends to the left beyond the node points (x marks). It should end within the node points (x marks).

enter image description here

To get rid of this problem, I replace

\addplot[mark=x,color=blue,const plot,jump mark right] table[y = Y]
% from \Rtable ;

by

\draw[blue] (0.9020688, 1) -- (0.933219,1) (1.1420047,2)--(1.154532,2) (1.2753,3)--(1.2865,3) (1.3649,4)--(1.3844,4) (1.4358,5)--(1.4746,5) (1.5,6)--(1.6,6) (1.5658954,7)-- (1.75,7) (1.6461004,8) -- (1.75,8);

in the code and get the desired plot. The second method using \draw is okay for a small set of data points and will take more time to write large number of data points manually.

  1. Any help on the first method to get rid of the problem is appreciated.

  2. Is it possible to use different node marks for the left node points (say, [) and right node points (say ))?

  • Your problem is that the x values are not monotonic - this will create problems for sure. It seems to me more a kind of histogram if you think y as the independent variable.... And: do you have just 2 x values for the same y, or you can have more? – Rmano Dec 2 '18 at 9:10
  • @Rmano Y takes a constant value in the interval X. For example, Y=1 for X in [0.9020688, 0.933219]. – Litun John Dec 2 '18 at 9:16
7

You could just store the coordinates in nodes and connect them in a loop.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{%https://tex.stackexchange.com/a/75811/121799
    name nodes near coords/.style={
        every node near coord/.append style={
            name=#1-\coordindex,
            alias=#1-last,
        },
    },
    name nodes near coords/.default=coordnode
}
\pgfplotstableread{
    X     Y
    0.9020688   1
    0.933219    1
    1.1420047   2
    1.154532    2
    1.2753  3
    1.2865  3
    1.3649  4
    1.3844  4
    1.4358  5
    1.4746  5
    1.5 6
    1.6 6
    1.5658954   7
    1.75    7
    1.6461004   8
    1.75    8
}\Rtable   
\pgfplotstablegetrowsof{\Rtable}
\pgfmathtruncatemacro{\rownum}{\pgfplotsretval}

\begin{document}
\begin{tikzpicture} []
\begin{axis}[xlabel={$X$},ylabel={$Y$},xmin=0.6, xmax=1.8, ymin=0,
ymax=8.5,node near coord style={anchor=center,opacity=0},
nodes near coords,name nodes near coords]
\addplot[mark=x,color=blue,only marks] table[y = Y] from \Rtable ;
\end{axis}
\pgfmathtruncatemacro{\Xmax}{\rownum/2}
\foreach \X [evaluate=\X as \Y using {int(2*\X-1)},evaluate=\X as \Z using
{int(2*\X-2)}] in {1,...,\Xmax}
{\draw[blue] (coordnode-\Y.center) -- (coordnode-\Z.center);}
\end{tikzpicture}
\end{document}

enter image description here

You can of course place everything you like there.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{%https://tex.stackexchange.com/a/75811/121799
    name nodes near coords/.style={
        every node near coord/.append style={
            name=#1-\coordindex,
            alias=#1-last,
        },
    },
    name nodes near coords/.default=coordnode
}
\pgfplotstableread{
    X     Y
    0.9020688   1
    0.933219    1
    1.1420047   2
    1.154532    2
    1.2753  3
    1.2865  3
    1.3649  4
    1.3844  4
    1.4358  5
    1.4746  5
    1.5 6
    1.6 6
    1.5658954   7
    1.75    7
    1.6461004   8
    1.75    8
}\Rtable   
\pgfplotstablegetrowsof{\Rtable}
\pgfmathtruncatemacro{\rownum}{\pgfplotsretval}

\begin{document}
\begin{tikzpicture} []
\begin{axis}[xlabel={$X$},ylabel={$Y$},xmin=0.6, xmax=1.8, ymin=0,
ymax=8.5,node near coord style={anchor=center,opacity=0},
nodes near coords,name nodes near coords]
\addplot[draw=none] table[y = Y] from \Rtable ;
\end{axis}
\pgfmathtruncatemacro{\Xmax}{\rownum/2}
\foreach \X [evaluate=\X as \Y using {int(2*\X-1)},evaluate=\X as \Z using
{int(2*\X-2)}] in {1,...,\Xmax}
{\draw[blue] (coordnode-\Z.center) node {[} -- 
(coordnode-\Y.center) node {)};}
\end{tikzpicture}
\end{document}

enter image description here

If you want these symbols to be smaller, use e.g.

\draw[blue] (coordnode-\Z.center) node[scale=0.5] {[} -- 
(coordnode-\Y.center) node[scale=0.5] {)};

to get

enter image description here

  • How to use different node marks for the left and right node points? – Litun John Dec 2 '18 at 14:09
  • 1
    @LitunJohn I added them. I missed that part of your question, sorry! – user121799 Dec 2 '18 at 14:53
1

It is possible to add an empty line in the table to be read using the operation \addplot table.

I quote from page 118 of the manual:

Interrupted Plots

Sometimes it is desirable to draw parts of a single plot separately, without connection between the parts (discontinuities). pgfplots offers three ways to generate interrupted plots:

  1. empty lines in the input file or
  2. by providing unbounded coords or
  3. by providing unbounded point meta.

To do this, the table is read here in the \add plot operation itself and not previously as you did. It may be possible to do this, but obviously empty lines are eliminated when they are read.

addplot-table

\documentclass[tikz,border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture} []
\begin{axis}[xlabel={$X$},ylabel={$Y$},xmin=0.6, xmax=1.8, ymin=0, ymax=8.5]
\addplot[mark=x,color=blue] table{
   X     Y
    0.9020688   1
    0.933219    1

    1.1420047   2
    1.154532    2

    1.2753  3
    1.2865  3

    1.3649  4
    1.3844  4

    1.4358  5
    1.4746  5

    1.5 6
    1.6 6

    1.5658954   7
    1.75    7

    1.6461004   8
    1.75    8
    };
\end{axis}
\end{tikzpicture}
\end{document}
  • Thank you for a much simpler code for the same plot. Is it possible to change the node marks like the first code? – Litun John Dec 3 '18 at 3:22
  • 1
    @LitunJohn You have already accepted a solution, so it is preferable that you write a new question on this subject. This will make this issue visible to everyone. Remember that the principle of a question and answer site is to ask only one question at a time. – AndréC Dec 3 '18 at 5:38

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.