1

I am trying to get a plot where there are just lines from the x-axis to the markers like this:

enter image description here

Here is a MWE that I have so far:

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

\begin{filecontents*}{data.csv}
  Iter   y 
     1, -0.9317521
     2, 1.8946202
     3, 0
     4, 1.5797030
     5, -1.8814457
     6, 0
     7, 2.0373926
     8, 0
     9, 1.9972528
    10, 0
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel={$Number~of~Recursions$},ylabel={Absolute Parameter Error}]
\addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document} 
Joe
  • 9,080

2 Answers2

1

Something like this? Just edit the bar width and line width to the desired value

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

\begin{filecontents*}{data.csv}
  Iter   y 
     1, -0.9317521
     2, 1.8946202
     3, 0
     4, 1.5797030
     5, -1.8814457
     6, 0
     7, 2.0373926
     8, 0
     9, 1.9972528
    10, 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[blue,line width=1pt,sharp plot, update limits=false] coordinates {(0,0) (11,0)};
    \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

JMP
  • 3,238
1

That's a comb plot, isn't it? If you need a line for y=0 you can either add a plot (\addplot[blue] coordinates {(0,0)(10,0)};) or use the method from How can I add a zero line to a plot?

By the way, Number of recursions is not math, don't write it in math mode. Use \textit{Number of recursions}, or xlabel style={font=\itshape},xlabel=Number of recursions if you want italics.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
  Iter   y 
     1, -0.9317521
     2, 1.8946202
     3, 0
     4, 1.5797030
     5, -1.8814457
     6, 0
     7, 2.0373926
     8, 0
     9, 1.9972528
    10, 0
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xlabel={Number of Recursions},
  ylabel={Absolute Parameter Error},
  after end axis/.append code={
   \draw[ultra thin,blue] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
}]

\addplot+ [ycomb] table[x index=0,y index=1,col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document} 

enter image description here

Torbjørn T.
  • 206,688