5

I need some help with this diagram:

  1. I would like to combine two entries in the “meta=” section. It should contain the {y*100}–value and the Label-value. Is it possible to add them somehow? In the end there should be the value in percentages and a star above each bar.
  2. I would like to change the color of the stars and error bars to black.
  3. Is it possible to extend the y-axis a tiny bit so it doesn’t end with a horizontal markline?

.

\documentclass{article} 
\usepackage[libertine]{newtxmath}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.11}
\usepackage{filecontents} 
\usepackage{tikz} 
\usepackage{pgfplotstable}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{arrows}
\usetikzlibrary{patterns}

\begin{document}

  \begin{filecontents}{data1.dat}
    X Y Y_error Label
    a 1 0 \\
    b .155 .038 $\star$
    c 0.80 0.019 $\star$
    d 0.54 0.027 \\
  \end{filecontents}  

  \begin{figure}
    \begin{tikzpicture} 
      \begin{axis}[
 bar width=1cm, % Width of the bar
    x=2cm, % Distance between the centers of the bars
    enlarge x limits={abs=1cm}, % The distance between the center of the first bar and the left edge
    enlarge y limits=false,
        symbolic x coords={a,b,c,d},
            ymin=0,
        xtick=data,
        ybar,
            axis x line*=bottom,
    axis y line=left,
    clip=false,
  %  point meta={y*100}, %y-Werte mal 100 für Prozent 
    point meta=explicit symbolic,
   yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%} ,
        nodes near coords,
        nodes near coords align={vertical},
        visualization depends on=abs(y)/y*(0.1+\thisrow{Y_error}) \as \myshift,
        every node near coord/.append style={
          anchor=center,shift={(0,transformdirectiony(\myshift))},
        }]
        \addplot+[draw=black, fill=white, error bars/.cd, y dir=both, y explicit, ]
                  table[x=X, y=Y, y error=Y_error, meta=Label,] {data1.dat};
                        \end{axis} 

    \end{tikzpicture}
  \end{figure}
  \end{document}

enter image description here

  • Maybe one can use something like this. But it can only read numbers no text.: visualization depends on=\thisrow{Y} \as \labela, visualization depends on=\thisrow{Label} \as \labelb, nodes near coords=\pgfmathprintnumber{\labela}/\pgfmathprintnumber{\labelb}, – Andreas Oct 14 '14 at 13:01
  • It would be much better if you only asked a single question in each post. That way, it's easier for others with similar problems to find the question. Try to phrase your questions as general as possible, and make sure the example code is minimal, i.e. reduce it as much as possible. – Jake Oct 15 '14 at 9:09
7

You can use a second \addplot+ command to add a second meta entry.

Use color=black istead only draw=black to get black stars and error bars.

Use ymax=1.05 to enlarge the y axis a little bit.

Updated code using the suggestions by Christian Feuersänger (see comments):

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\usepackage{filecontents}
\begin{filecontents}{data1.dat}
  X Y Y_error Label
  a 1 0 \\
  b .155 .038 $\star$
  c 0.80 0.019 $\star$
  d 0.54 0.027 \\
\end{filecontents}

\begin{document}
\begin{tikzpicture} 
  \begin{axis}[
      bar width=1cm,
      x=2cm,
      enlarge x limits={abs=1cm},
      symbolic x coords={a,b,c,d},
      ymin=0,
      ymax=1.05,
      xtick=data,
      axis x line*=bottom,
      axis y line=left,
      clip=false,
      yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%} ,
      nodes near coords,
      visualization depends on=abs(y)/y*(0.15+\thisrow{Y_error}) \as \myshift,
      every node near coord/.append style={yshift=transformdirectiony(\myshift),inner sep=1pt},
    ]
    \addplot+[
        ybar,
        color=black,
        fill=white,
        mark=none,
        point meta=explicit symbolic,
        every node near coord/.append style={anchor=south},
        error bars/.cd,
          y dir=both,
          y explicit
      ]
      table[x=X, y=Y, y error=Y_error, meta=Label,] {data1.dat};
    \addplot+[
        color=black,
        only marks,
        mark=none,
        %%% as suggested by Christian Feuersänger (see comments):
        nodes near coords={$\pgfmathprintnumber{\pgfplotspointmeta}\%$},
        point meta=100*y,
        every node near coord/.append style={anchor=north,/pgf/number format/.cd, fixed, fixed zerofill=true, precision=1}
        %%%
      ]
      table[x=X, y=Y,forget plot] {data1.dat};
  \end{axis} 
\end{tikzpicture}
\end{document}

enter image description here


Original code (does not work anymore):

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\usepackage{filecontents}
\begin{filecontents}{data1.dat}
  X Y Y_error Label
  a 1 0 \\
  b .155 .038 $\star$
  c 0.80 0.019 $\star$
  d 0.54 0.027 \\
\end{filecontents}

\begin{document}
\begin{tikzpicture} 
  \begin{axis}[
      bar width=1cm,
      x=2cm,
      enlarge x limits={abs=1cm},
      symbolic x coords={a,b,c,d},
      ymin=0,
      ymax=1.05,
      xtick=data,
      axis x line*=bottom,
      axis y line=left,
      clip=false,
      yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%} ,
      nodes near coords,
      visualization depends on=abs(y)/y*(0.15+\thisrow{Y_error}) \as \myshift,
      every node near coord/.append style={yshift=transformdirectiony(\myshift),inner sep=1pt},
    ]
    \addplot+[
        ybar,
        color=black,
        fill=white,
        mark=none,
        point meta=explicit symbolic,
        every node near coord/.append style={anchor=south},
        error bars/.cd,
          y dir=both,
          y explicit
      ]
      table[x=X, y=Y, y error=Y_error, meta=Label,] {data1.dat};
    \addplot+[
        color=black,
        only marks,
        mark=none,
        point meta={TeX code symbolic={\def\pgfplotspointmeta{\pgfmathresult\%}}},
        every node near coord/.append style={anchor=north}
      ]
      table[x=X, y=Y,forget plot] {data1.dat};
  \end{axis} 
\end{tikzpicture}
\end{document}
  • 2
    Good idea to add the second line. One remark, though: the line point meta={TeX code symbolic={\def\pgfplotspointmeta{\pgfmathresult\%}}}, is quite fragile as it assumes knowledge about the value and internal format of \pgfmathresult which is likely to break without further notice. In fact, my unstable results in unanticipated output. Suggested change ... [next comment] – Christian Feuersänger Dec 31 '15 at 12:48
  • 2
    ... nodes near coords={$\pgfmathprintnumber{\pgfplotspointmeta}\%$}, point meta=100*y. The number printer will result in a different style, so you may also want to use every node near coord/.append style={anchor=north,/pgf/number format/.cd, fixed, fixed zerofill=true, precision=1} – Christian Feuersänger Dec 31 '15 at 12:49
  • 1
    @ChristianFeuersänger Thank you for the hints. I have changed my answer. – esdd Dec 31 '15 at 13:33

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.