3

I have the following problem creating an histogram with pgfplots. I am quite a beginner, so there could be some clear mistakes. When I compile this:

\begin{tikzpicture}
\begin{axis}[
xtick={-0.0003, -0.0002, -0.0001, 0, 0.0001, 0.0002, 0.0003},
xmin=-0.0003, xmax=0.0003,
ymin=0, ymax=90,
xticklabels={-3e-4, -2e-4, -1e-4, 0, 1e-4, 2e-4, 3e-4},
ytick={0, 10, 20, 30, 40, 50, 60, 70, 80},
ylabel=Number of Occurrences,
enlargelimits=0.05,
ybar interval=0.7,
]
\addplot coordinates {(-0.00027,3) (-0.000231,2) (-0.000192,19) (-0.000153,29) (-0.000114, 49) (-0.000075, 73) (-0.000036,77) (0.000003,87) (0.000042,77) (0.000081,47) (0.000120, 15) (0.000159, 13) (0.000198,6) (0.000237, 2) (0.000276, 1)};
\end{axis}
\end{tikzpicture}

I get this:

enter image description here

Therefore, I don't get why:

1) The ticks on the x-axis are not where they are supposed to be and all crowded at a random (?) point of the axis;

2) The x10^-4 is that far from the plot.

As I said I am probably getting something wrong even though it is a simple plot. Thanks in advance!

  • 1
    Welcome to TeX.SE! You need to say ybar interval=0.7, before you set the xtick labels because otherwise this will get overridden. – user121799 Nov 16 '18 at 19:45
2

I guess there is a slight misunderstanding of what ybar interval does. To see that, look first at the MWE in which I moved the directive up,

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar interval=0.7,
xtick={-0.0003, -0.0002, -0.0001, 0, 0.0001, 0.0002, 0.0003},
xmin=-0.00031, xmax=0.00031,
ymin=0, ymax=90,
%xticklabels={-3e-4, -2e-4, -1e-4, 0, 1e-4, 2e-4, 3e-4},
ytick={0, 10, 20, 30, 40, 50, 60, 70, 80},
ylabel=Number of Occurrences,
enlargelimits=0.05,
]
\addplot coordinates {(-0.00027,3) (-0.000231,2) (-0.000192,19) (-0.000153,29) (-0.000114, 49) (-0.000075, 73) (-0.000036,77) (0.000003,87) (0.000042,77) (0.000081,47) (0.000120, 15) (0.000159, 13) (0.000198,6) (0.000237, 2) (0.000276, 1)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

According to the text of your question, this may not be quite what you want. However, I'd like to remind you that the pgfplots manual says on p. 87 that

enter image description here

This means that you may either want to use slightly different coordinates or just to an ybar plot. Since my crystal ball got stolen, I am not sure what precisely you are after, and stop here. However, if you specify this in an update of your question, I'll be happy to try to give it a shot.

  • 1
    Indeed, I was missing the last coordinate. And yes, I had to put the ybar interval before the xtick. So, the example you provided is exactly what I was looking for. Thanks – Wiki Nov 17 '18 at 11:22

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.