1

What I obtained:

enter image description here

What I need:

enter image description here

Operations needed:

  • Positioning y axis on Zero
  • Anchoring node near coords on the right side of the blue line

The code I used is:

\begin{filecontents*}{RR.dat}
y   x   Label
2   116.6   MT600
3   -17.43  SC600
4   17.6    TC700
\end{filecontents*}

    \begin{tikzpicture}
    \begin{axis}[
        axis lines*=left,
        yticklabels from table={RR.dat}{Label},
        ytick=data,
        nodes near coords={\thenumber{}},
        nodes near coords align={horizontal},
        visualization depends on={value \thisrow{x} \as \thenumber},
        ]
        \addplot+[xcomb] table [x=x, y=y] {RR.dat};
    \end{axis}
    \end{tikzpicture}

Thanks!! :)

1
  1. Add a new column for shifting the nodes near coordniates since these have to be shifted non-uniformly. I did it with shift column and added values accordingly. Then add the following lines:

    visualization depends on={value \thisrow{shift}\as\myshift},
    every node near coord/.append style={xshift=\myshift},
    
  2. Use axis y line*=middle, so that y axis is drawn at x=0.

  3. Move the y tick labels to left using yticklabel style={xshift=-8mm},.

Code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents*}{RR.dat}
y   x   Label     shift
2   116.6   MT600    0
3   -17.43  SC600    8mm
4   17.6    TC700    0
\end{filecontents*}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
        axis y line*=middle,
        yticklabels from table={RR.dat}{Label},
        ytick=data,
        yticklabel style={xshift=-8mm},
        nodes near coords={\thenumber{}},
        nodes near coords align={horizontal},
        visualization depends on={value \thisrow{shift}\as\myshift},
        every node near coord/.append style={xshift=\myshift},
        visualization depends on={value \thisrow{x} \as \thenumber},
        ]
        \addplot+[xcomb,mark=square*] table [x=x, y=y] {RR.dat};
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • thanks. Instead of adding a new column I modified your solution: visualization depends on={value 2.1cm*(-\thisrow{x}+max(0,\thisrow{x})) \as \myshift}, every node near coord/.append style={xshift=\myshift}, – Stefano Oct 27 '15 at 10:56

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.