[Tex/LaTex] How to set intervals for step-like plot (`const plot`) in pgfplots

pgfplotstikz-pgf

How do I set the precise intervals that pgfplots should consider when performing addplot with the option const plot mark mid? For example:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=1.9}
\begin{filecontents*}{data.csv}
1., 1.
2., 2.
4., 3.
7., 4.
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[const plot mark mid, mark=*] table [col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

The issue is that when you have an input table which is irregular in the x-axis, pgfplots divides the intervals in a not so convenient way. It simply ends the "step" in the middle way to the next mid point. So, I would like to set the intervals by hand.

Output:

enter image description here

In this case, I would like to correct it by providing a list of widths

{1.,1.,3.,3.}

to cover the following intervals:

0.5 -- 1.5
1.5 -- 2.5
2.5 -- 5.5
5.5 -- 8.5

This way, the points would be exactly in the middle of the steps.

Additional comment

Pgfplots documentation says that one can avoid the vertical line being drawn in the middle way between two adjacent points, by using jump mark mid. In my example it would be

\addplot[jump mark mid, mark=*] table [col sep=comma] {data.csv};

Nevertheless, this does not solve the problem, as the horizontal lines are kept identical.

Best Answer

I came up with the following solution, which requires the inclusion of an additional column in the original table. Such additional column must contain the widths I referred in my question.

The code is divided in 3 distinct plots:

  • marks for the original points;
  • a const plot shifted (-) by half of the corresponding widths (black line); and
  • a jump mark right shifted (+) by half width, for drawing the last step (dashed red line).

The last item requires ignoring most of the points in data.csv by using skip coords between index={\yini}{\yfin}. \yini and yfin are defined in an automatized way.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=1.9}

\begin{filecontents*}{data.csv}
1., 1., 1.
2., 2., 1.
4., 3., 3.
7., 4., 3.
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
%
\pgfplotstablegetrowsof{data.csv}
\pgfmathsetmacro\yfin{\pgfmathresult-1}
\pgfmathsetmacro\yini{0}
%
\begin{axis}
[minor tick num=3]
%
\addplot[mark=*, only marks] 
table [col sep=comma,
x expr=\thisrowno{0}, y expr=\thisrowno{1}]
{data.csv};
%
\addplot[const plot, black]
table [col sep=comma,
x expr=\thisrowno{0} - 0.5*\thisrowno{2}, y expr=\thisrowno{1}]
{data.csv};
%
\addplot[jump mark right, red, ultra thick, dashed] 
table [col sep=comma,
x expr=\thisrowno{0} + 0.5*\thisrowno{2} , y expr=\thisrowno{1}, skip coords between index={\yini}{\yfin}] 
{data.csv};
%
\end{axis}
\end{tikzpicture}
\end{document}

Output from the solution code.