1

I am creating a bar plot for a situation, however the scale of each bar is different and should be represented in the scale of 10. My code is:

\documentclass[runningheads]{llncs}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}

    \pgfplotscreateplotcyclelist{gray}{ % define a new cycle list
     {fill=black!20},
     {fill=black!40},
     {fill=black!60}
    }


    \begin{tikzpicture}
    \begin{axis}[
      ymin = 0, ymax = 25,
      ybar,
      bar width=17, % added
      enlarge x limits=0.3,
      legend  style={at={(0.7 ,1)},
      anchor=north,legend  columns =-1},
      ylabel ={Gas Cost and Price in ETH},
      xlabel ={Sources Quantity},
      symbolic x coords ={7,14,21},
      xtick=data,
      nodes  near  coords ,
      nodes  near  coords  align ={vertical},
      nodes near coords style={font=\tiny}, % reduce font size of nodes near coords
      cycle list name=gray, % use the new cycle list
    ]

    \addplot  coordinates  {(7,11) (14,15) (21,19)};
    \addplot  coordinates  {(7,5) (14,7) (21,9)};

     \addlegendentry{Gas}
     \addlegendentry{ETH}
    \end{axis}
    \end{tikzpicture}
\end{document}

My values are:

%7:   0.11449906 ETH - 5 724 953 Gas
%14: 0.153421 ETH     - 7 671 050 Gas
%21: 0.1962195 ETH -  9 810 975 Gas
  • How could I represent the value shown above the bars on a scale of ten (i.e., 5.75 * 10⁶)
  • 1
    Not quite, you also need ylabel ={Gas Cost \& Price in ETH},, i.e. a \& instead of &. Note that your MWE produces an interesting effect. When one fixes the & the code runs through but it no longer does so once one adds \pgfplotsset{compat=1.16}. – marmot May 20 at 2:00
  • Changed it for "and" – Mutante May 20 at 2:03
  • You also need something like bar width=17pt if you use a more recent version. That is, you need to add units to the bar width. – marmot May 20 at 2:07
  • With my edit the MVE is complete. – Mutante May 20 at 2:09
  • Let us continue this discussion in chat. – Mutante May 20 at 2:13
2

Another option would be to read the values into a table, and do some scaling here and there with visualization depends on and y expr.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\begin{document}

    \pgfplotscreateplotcyclelist{gray}{ % define a new cycle list
     {fill=black!20},
     {fill=black!40},
     {fill=black!60}
    }


    \begin{tikzpicture}
    \pgfplotstableread{
       p  ETH  gas
       7  0.11449906 5724953
      14 0.153421   7671050
      21 0.1962195  9810975
     }\DataTable
    \begin{axis}[
      ymin = 0, ymax = 25,
      ybar,
      bar width=17pt, % added
      enlarge x limits=0.3,
      legend  style={at={(0.7 ,1)},
      anchor=north,legend  columns =-1},
      ylabel ={Gas Cost and Price in ETH},
      xlabel ={Sources Quantity},
      xtick=data,
      xticklabels from table={\DataTable}{p}, % instead of symbolic coords
      nodes  near  coords  align ={vertical},
      nodes near coords style={font=\tiny}, % reduce font size of nodes near coords
      cycle list name=gray, % use the new cycle list
    ]

     \addplot  +[
       visualization depends on=y*1e-2\as\RawY,
       nodes near coords={\pgfmathprintnumber{\RawY}}
       ]  
           table[x expr=\coordindex, y expr=\thisrow{ETH}*1e2] {\DataTable};
     \addplot  +[
       visualization depends on=y*1e6\as\RawY,
       nodes near coords={\pgfmathprintnumber{\RawY}},
       node near coords style={anchor=south west,rotate=45}]
            table[x expr=\coordindex, y expr=\thisrow{gas}*1e-6] {\DataTable};

     \addlegendentry{ETH}
     \addlegendentry{Gas}
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

0

Here is a proposal. Note that I had to add dimensions to the bar width in order to be able to compile it with the most recent version of pgfplots.

\documentclass[border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16} 
\begin{document}

    \pgfplotscreateplotcyclelist{gray}{ % define a new cycle list
     {fill=black!20},
     {fill=black!40},
     {fill=black!60}
    }


    \begin{tikzpicture}
    \begin{axis}[
      ymin = 0, ymax = 25,
      ybar,
      bar width=17pt, % added
      enlarge x limits=0.3,
      legend  style={at={(0.7 ,1)},
      anchor=north,legend  columns =-1},
      ylabel ={Gas Cost and Price in ETH},
      xlabel ={Sources Quantity},
      symbolic x coords ={7,14,21},
      xtick=data,
      nodes  near  coords  align ={vertical},
      nodes near coords style={font=\tiny}, % reduce font size of nodes near coords
      cycle list name=gray, % use the new cycle list
    ]
    %7:   0.11449906 ETH - 5 724 953 Gas
    %14: 0.153421 ETH     - 7 671 050 Gas
    %21: 0.1962195 ETH -  9 810 975 Gas
    \edef\myLstA{0.11449906,0.153421,0.1962195}
    \addplot+[nodes  near  coords=\pgfmathparse{{\myLstA}[\coordindex]}%
      \pgfmathprintnumber{\pgfmathresult}]  coordinates  {(7,11) (14,15) (21,19)};
    \edef\myLstB{5724953,7671050,9810975}
    \addplot+[nodes  near  coords=\pgfmathparse{{\myLstB}[\coordindex]}%
      \pgfmathprintnumber{\pgfmathresult},
      nodes near coords style={xshift=1.5ex,yshift=3.5ex,rotate=70}]  coordinates  {(7,5) (14,7) (21,9)};

     \addlegendentry{Gas}
     \addlegendentry{ETH}
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

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.