2

I am trying to do a tornado plot where the base value on the x-axis is different from zero, in my case 5.43.

\documentclass[margin=5pt]{standalone}

\usepackage{tikz,pgfplots,pgfplotstable}

\pgfplotstableread[col sep=comma, header=true]{
parameter,      50,     200
Material cost,  3.67,   8.69
E\&U cost,      5.13,   5.78
FCOP,           4.86,   6.31
ACC,            5.04,   5.94
}\loadedtable

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            enlarge x limits=0.2,
            %xmin=0,
            xmax=10,
            %xticklabels={0,2,4,6,8,10},
            xbar,nodes near coords,bar width=16pt,bar shift=0pt,
            symbolic y coords={Material cost, E\&U cost, FCOP, ACC },
            ytick={Material cost, E\&U cost, FCOP, ACC },
            y dir = reverse,
            ]
            \addplot[nodes near coords align={left},
            draw=blue,fill=blue!25,text=blue]
            table[y=parameter,x expr={-\thisrow{50}},point meta={-x}] \loadedtable;
            \addplot table[y=parameter,x expr={\thisrow{200}}] \loadedtable;
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

2 Answers 2

3

A vairation of this nice solution would be to shift the labels on the x axis by 5.43. Note that you don't need to load tikz and pgfplots if you load pgfplotstable as they are loaded anyways, but you should add \pgfplotsset{compat=1.18} if you use pgfplots:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}

\pgfplotstableread[col sep=comma, header=true]{
parameter,      50,     200
Material cost,  3.67,   8.69
E\&U cost,      5.13,   5.78
FCOP,           4.86,   6.31
ACC,            5.04,   5.94
}\loadedtable

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            enlarge x limits=0.2,
            %xmin=0,
            xmax=10,
            %xticklabels={0,2,4,6,8,10},
            xbar,
            nodes near coords,
            bar width=16pt,
            bar shift=0pt,
            symbolic y coords={Material cost, E\&U cost, FCOP, ACC},
            ytick={Material cost, E\&U cost, FCOP, ACC},
            y dir=reverse,
            xticklabel={\pgfmathparse{\tick+5.43}\pgfmathprintnumber{\pgfmathresult}}]
        ]
            \addplot[nodes near coords align={left},
                draw=blue, fill=blue!25, text=blue]
                table[y=parameter, x expr={-\thisrow{50}}, point meta={-x}] \loadedtable;
            \addplot table[y=parameter, x expr={\thisrow{200}}] \loadedtable;
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

Maybe this is what you want:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}

\pgfplotstableread[col sep=comma, header=true]{
parameter,      50,     200
Material cost,  3.67,   8.69
E\&U cost,      5.13,   5.78
FCOP,           4.86,   6.31
ACC,            5.04,   5.94
}\loadedtable

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            enlarge x limits=0.2,
            %xmin=0,
            %xmax=2,
            %xticklabels={0,2,4,6,8,10},
            xbar,
            nodes near coords,
            bar width=16pt,
            bar shift=0pt,
            symbolic y coords={Material cost, E\&U cost, FCOP, ACC},
            ytick={Material cost, E\&U cost, FCOP, ACC},
            y dir=reverse,
            xticklabel={\pgfmathparse{\tick+5.43}\pgfmathprintnumber{\pgfmathresult}}]
        ]
            \addplot[nodes near coords align={left},
                draw=blue, fill=blue!25, text=blue]
                table[y=parameter, x expr={\thisrow{50}-5.43}, point meta={-x}] \loadedtable;
            \addplot table[nodes near coords align={right},
                y=parameter, x expr={\thisrow{200}-5.43}, point meta={x}] \loadedtable;
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

0
0

Thanks - the latter version is what I wanted. In this case, I can show values as % and there is no need for x-axis shift.

enter image description here

\documentclass[margin=5pt]{standalone}
\usepackage[dvipsnames,table,xcdraw]{xcolor} 
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}

\begin{document}
    
    \begin{tikzpicture}
            \pgfplotstableread[col sep=comma, header=true]{
        parameter,      50,     200
        Material cost,  -31,    63
        E\&U cost,      -4,     8   
        FCOP,           -9,     18
        ACC,            -6,     11
    }\loadedtable
    \begin{axis}[
        enlarge x limits=0.2,
        xlabel = {TCOP deviation},
        xticklabel={\pgfmathprintnumber\tick\%},
        tick label style={font=\footnotesize},
        xlabel style={font=\footnotesize},
        ylabel style={font=\footnotesize},
        %enlarge y limits={0.1},    
        xmin=-50,
        xmax=100,
        %xticklabels={0,2,4,6,8,10},
        xbar,
        nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
        every node near coord/.append style={font=\scriptsize},
        bar width=14pt,bar shift=0pt,
        symbolic y coords={Material cost, E\&U cost, FCOP, ACC },
        ytick={Material cost, E\&U cost, FCOP, ACC },
        y dir = reverse,
        legend pos=south east,
        legend style={
            font=\footnotesize}
        ]
        \addplot[nodes near coords align={left},
        draw=CornflowerBlue,fill=CornflowerBlue,text=CornflowerBlue]
        table[y=parameter,x expr={\thisrow{50}}] \loadedtable;
        \addplot[
        draw=orange,fill=orange,text=orange]
        table[y=parameter,x expr={\thisrow{200}}] \loadedtable;
        \legend{-50\%, +100\%}
    \end{axis}
\end{tikzpicture}   

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .