0

This post is building from the post found here that has an elegant solution proposed by JMP.

I wanted to append just red markers to the existing plot so that the plot looks like:

enter image description here

In summary, I have two data sets, and they are currently the same values hence the appearance of the plot above. How can I get this same effect in pgfplots? Thanks.

Here is the code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{graphicx}

\begin{filecontents*}{data.csv}
  Iter   y1 y2
     1, -0.9317521, -0.9317521
     2, 1.8946202, 1.8946202
     3, 0, 0
     4, 1.5797030, 1.5797030
     5, -1.8814457, -1.8814457
     6, 0, 0
     7, 2.0373926, 2.0373926
     8, 0, 0
     9, 1.9972528, 1.9972528
    10, 0, 0
\end{filecontents*}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[ybar,bar width=1pt,xlabel={$Number~of~Recursions$},ylabel={Absolute Parameter Error}]
      \addplot [mark=*,blue, fill=blue,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma] {data.csv};
      %\addplot [mark=*,red, fill=blue,mark options={scale=.35}]table[x index=0,y index=1,col sep=comma] {data.csv};
      \addplot[blue,line width=1pt,sharp plot, update limits=false] coordinates {(0,0) (11,0)};
    \end{axis}
  \end{tikzpicture}
\end{document}
Joe
  • 9,080

1 Answers1

2

Edited Answer according to the edited question

You state that the different y values can be different but you have provided the same values for both y columns. That is why I have changed the first value of y2 to -1.5.

For the rest have a look at the comments in the code.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
    \begin{filecontents*}{data.csv}
        Iter,   y1,         y2
         1,     -0.9317521, -1.5
         2,     1.8946202,  1.8946202
         3,     0,          0
         4,     1.5797030,  1.5797030
         5,     -1.8814457, -1.8814457
         6,     0,          0
         7,     2.0373926,  2.0373926
         8,     0,          0
         9,     1.9972528,  1.9972528
        10,     0,          0
    \end{filecontents*}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0,
            xmax=11,
            xlabel=Number~of~Recursions,
            ylabel=Absolute Parameter Error,
        ]
            \addplot [
                mark=*,
                only marks,
                mark options={
                    %%% only draw the marker
                    % draw=color, fill=none
                    draw=red,
                    fill=red,
                    % adjust the line width of the marker
                    line width=1pt,
                }
            ] table [x=Iter,y=y2,col sep=comma] {data.csv};
            \addplot [
                ycomb,
                mark=*,
                blue,
                fill=blue,
                line width=1pt,
                % make marker smaller
                mark options={
                    scale=.5,
                }
            ] table [x=Iter,y=y1,col sep=comma] {data.csv};
            \addplot [
                blue,
                sharp plot,
                line width=1pt,
            ] coordinates {
                (\pgfkeysvalueof{/pgfplots/xmin},0)
                (\pgfkeysvalueof{/pgfplots/xmax},0)
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
  • Thanks for your response. I am not sure that I will be able to use that solution, as the other data set (y2) is what should be plotted. It just so happens in this current data set, both y1 and y2 are the same. I have other data sets, where y1 and y2 will be different. Sorry for the discrepancy, but I just updated the data set of my original question. Thanks again for your time and help. – Joe Mar 31 '16 at 07:25
  • 1
    Have a look at my edited answer, if that is now what you are looking for. – Stefan Pinnow Mar 31 '16 at 07:52
  • Thanks again for your help and time. Is it possible to remove the black line on the first data point to the red marker? I tried putting the line width = 0pt but I still see the black line. Thanks again! – Joe Mar 31 '16 at 08:07
  • 1
    Sure. See my edited answer. In short: I moved the ycomb to the \addplot and removed the fill from the first \addplot to achieve the desired result. – Stefan Pinnow Mar 31 '16 at 08:38