1

My code creates a bar chart, but the bars are quite thin and far apart. Also, I want to add the labels ("Bitcoin" and "VISA" under each bar respectively, but this isn't working for me either...

Please could someone help with this? Thank you very much in advance for your help. enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{cfets.csv}
column1 column2
8  1005
14 169
\end{filecontents*}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
      \begin{axis}[
           ybar,
            bar width=.4cm,
            enlarge x limits={abs=.4cm},
            xtick=data,
            x tick label style={color=white},
            nodes near coords,
            nodes near coords align={vertical},
            ymin=0,ymax=1200,
            ylabel={Energy consumption in kilowatt-hours},
        ]
        \addplot table[x expr=\coordindex,y=column2] {cfets.csv}; 

      \end{axis}
    \end{tikzpicture}
\end{document}
3
  • Welcome to TeX.SE...Your tag works well, hope the problem may lies in old packages, update your TeX setup and then check...
    – MadyYuvi
    Feb 10 '20 at 13:32
  • Thanks! I'm running it in Overleaf... Could that be the problem?
    – ABCBAA
    Feb 10 '20 at 13:35
  • Hope so...but not sure, as I don't have enough experience in Overleaf
    – MadyYuvi
    Feb 10 '20 at 13:46
1

Welcome to TeX.SE! You can use, for instance, x=1.1cm in the axis options to determine the length of the x vector. This will make the bars closer to each other here, and reduce the plot width (in your code, pgfplots determines x and y units in order to obtain a plot that is \axisdefaultwidth wide and \axisdefaultheight high, the defaults for these being respectively 240 and 207 points, which gives the customary rectangular box for plots made with pgfplots).

For your Bitcoin and VISA labels, my suggestion would be to include them in your data, for instance like this:

column1  column2
Bitcoin  1005
VISA     169

and use symbolic x coords={Bitcoin,VISA} in conjunction with \addplot table[x=column1, y=column2] {cfets.csv};.

I also used small in the axis options so as to reduce the font size—the bars are not that wide and closer now; we don't want the x labels to touch each other. I would recommend setting a recent pgfplots compatibility level, as in \pgfplotsset{compat=1.16}.

Finally, please note that the filecontents package is obsolete with recent LaTeX. As a consequence, there is no need for \usepackage{filecontents} anymore.

\begin{filecontents*}{cfets.csv}
column1  column2
Bitcoin  1005
VISA     169
\end{filecontents*}

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    small,
    ybar,
    x=1.1cm, bar width=.6cm,
    enlarge x limits={abs=.4cm},
    symbolic x coords={Bitcoin,VISA},
    xtick=data,
    nodes near coords, nodes near coords align={vertical},
    ymin=0, ymax=1200,
    ylabel={Energy consumption in kilowatt-hours},
    ]
    \addplot table[x=column1, y=column2] {cfets.csv};
  \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.