0

So I'm just learning LaTeX so I'm sorry if this is a dumb question, but i've read the manual, watched youtube videos and read blogs and I still can't figure out why this is not working. I want to do a simple bar graph. Everything goes well in showing the data until I try to add the code that makes it actually look like a bar graph, then, the last coordinate disappears and I do not know why. This is my code, I've linked "pgfplots" at the top (and also "pgf-pie for another graph, if that might be relevant):

\begin{tikzpicture}
    \begin{axis}[
    %title= ,
    ymin=0, ymax=10,
    x tick label style={/pgf/number format/1000 sep=},
    ybar interval=1,
    xlabel={$estado$},
    ylabel={$frecuencia$},
    symbolic x coords={CA,TX,AZ},
    xtick=data,
    ]

    \addplot coordinates { (CA,9) (TX,8) (AZ,8)};

    \end{axis}
    \end{tikzpicture}

The issues start when i add these two lines, which i got from overleaf:

x tick label style={/pgf/number format/1000 sep=},
    ybar interval=1

Does anyone know why this is happening and how can I make a bar graph? Thank you

1
  • 2
    Welcome to TeX.SE! Please -- as usual here -- make your code snippets compilable, then we do not have to guess what you are doing ...
    – Mensch
    Sep 21, 2020 at 8:41

2 Answers 2

0

With more complex input data, the coordinate plot can quickly become confusing (I would only use the coordinate plot for adding individual points or similar).
Usually you have a table of input data of the kind

X         Y
CA        9
TX        8
AZ        8

So it's good to know how to do this as a table plot.

symbolic x coords={CA,TX,AZ}, is not needed here, instead:
xticklabels from table={\inputdata}{X}, % <--- !

enter image description here

\documentclass[margin=3mm, tikz]{standalone}
\usepackage{pgfplotstable} % <--- !
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\pgfplotstableread[]{
X         Y
CA        9
TX        8
AZ        8
}\inputdata

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=0, ymax=10,
%x tick label style={/pgf/number format/1000 sep=},% not used
ybar,                       % changed
enlarge x limits = 0.4,     %  new
xlabel=\textit{estado},
ylabel=\textit{frecuencia},
%symbolic x coords={CA,TX,AZ}, % not needed
xticklabels from table={\inputdata}{X}, % <--- !
xtick=data, 
]
\addplot table[x expr=\coordindex, y=Y]{\inputdata};
\end{axis}
\end{tikzpicture}
\end{document}
1
  • Thank you so much, I understand much better and it works now! <3
    – Kraken
    Sep 21, 2020 at 15:16
0

I guess that you looking for something like this:

enter image description here

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
ymin=0, ymax=10,
x tick label style={/pgf/number format/1000 sep=},
ybar,                       % changed
enlarge x limits = 0.4,     %  new
xlabel=\textit{estado},
ylabel=\textit{frecuencia},
symbolic x coords={CA,TX,AZ},
xtick=data,
]
\addplot coordinates { (CA,9) (TX,8) (AZ,8)};
\end{axis}
    \end{tikzpicture}
\end{document}
2
  • Thank you, this helped too, what does enlarge x limits do exactly?
    – Kraken
    Sep 21, 2020 at 15:17
  • @Kraken, it add space before and after diagram. To see effect, in test remove option enlarge x limits. What you mean with "helped too". Is suggested solution what you looking for?
    – Zarko
    Sep 21, 2020 at 15:33

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.