条形图中的不同比例

条形图中的不同比例

我正在为某种情况创建条形图,但每个条形的比例不同,应以 10 的比例表示。我的代码是:

\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}

我的价值观是:

%7:   0.11449906 ETH - 5 724 953 Gas
%14: 0.153421 ETH     - 7 671 050 Gas
%21: 0.1962195 ETH -  9 810 975 Gas
  • 我怎样才能以十为单位表示条形图上方显示的值(即 5.75 * 10⁶)

答案1

另一种选择是将值读入表中,然后使用和进行一些visualization depends on缩放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}

在此处输入图片描述

答案2

这是一项建议。请注意,我必须增加条形宽度的尺寸,以便能够使用最新版本的 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}

在此处输入图片描述

相关内容