1

enter image description hereI'm trying to create a histogram in latex but the ticks are not aligning with the bars and i'm not sure why.

this is what i have so far:

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand*{\ttdefault}{\familydefault}
\usepackage[paperwidth=42cm,paperheight=29.7cm,left =1cm, top = 1cm, right =1cm, bottom = 1cm ,marginparwidth=0cm, includeheadfoot,headheight=66pt, headsep=0cm]{geometry}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\usepackage[table,xcdraw]{xcolor}
\usepackage[document]{ragged2e}
\usepackage[export]{adjustbox}
\usepackage{eurosym}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{float}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{caption}
\usepackage{calc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tcolorbox}
\usepackage{tabularx}
\usepackage{multicol}
\usepackage{lastpage}
\usepackage{longtable}
\usepackage{pdflscape}
\usepackage{filecontents}
\usepackage{textcomp}
\usepackage{changepage}
\usepackage{times}
\captionsetup[table]{labelformat=empty}
\captionsetup{justification=raggedright,singlelinecheck=false}
\pgfplotsset{width=10cm,compat=newest}
\usetikzlibrary{plotmarks}
\usepgfplotslibrary{dateplot}
\usepgfplotslibrary{units}
\tikzset{every picture/.append style={font=\normalsize}} % size graph font
\usetikzlibrary{arrows, positioning, calc}
\newcolumntype{Y}{>{\centering\arraybackslash}X} %
\def\tabularxcolumn#1{m{#1}}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\pagestyle{fancy}
\makeatletter
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\tikzstyle{chart}=[
legend label/.style={font={\Large},anchor=west,align=left},
legend box/.style={rectangle, draw, minimum size=10pt},
axis/.style={black,semithick,->},
axis label/.style={anchor=east,font={\tiny}}]
\newcommand{\pie}[5][]{
\begin{scope}[#1]
\pgfmathsetmacro{\curA}{90}
\pgfmathsetmacro{\r}{#2}
\def\c{(0,0)}
\node[pie title] at (270:\r + 0.5) {#3};
\foreach \v/\s/\l/\x in{#4}{
\pgfmathsetmacro{\deltaA}{\v/100*360}
\pgfmathsetmacro{\nextA}{\curA + \deltaA}
\pgfmathsetmacro{\midA}{(\curA+\nextA)/2}
\path[slice,\s] \c
-- +(\curA:\r)
arc (\curA:\nextA:\r)
-- cycle;
\begin{pgfonlayer}{foreground}
\path \c -- node[pos=\x,pie values,values of \s,font={#5}]{\l} +(\midA:\r);
\end{pgfonlayer}
\global\let\curA\nextA
}
\end{scope}
}
\newcommand{\legend}[2][]{
\begin{scope}[#1]
\path
\foreach \n/\s in {#2}{++(0,-10pt) node[\s,legend box] {} +(5pt,0) node[legend label] {\n}};
\end{scope}
}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\definecolor{color1}{HTML}{181F50}
\definecolor{imagine}{HTML}{87cfeb}
\definecolor{lightblue}{HTML}{4f81c5}
\definecolor{oxfordblue}{HTML}{1f3057}
\definecolor{seagreen}{HTML}{96bf65}
\definecolor{olivegreen}{HTML}{4b5729}
\definecolor{lightbeige}{HTML}{f5e7a1}
\definecolor{goldenyellow}{HTML}{fcc808}
\definecolor{champagne}{HTML}{e6c18d}
\definecolor{salmonpink}{HTML}{f3b28b}
\definecolor{ruby}{HTML}{982d57}
\definecolor{lightorange}{HTML}{ef841e}
\definecolor{lightgray}{HTML}{bec0b8}
\definecolor{lightviolet}{HTML}{c9a8ce}
\definecolor{shellpink}{HTML}{fbded6}
\definecolor{mediumblue}{HTML}{0000CD}
\definecolor{lilac}{HTML}{c9a8ce}
\definecolor{saxo}{HTML}{1C5B92}
\definecolor{blue2}{HTML}{99c2ff}
\definecolor{lightgray2}{HTML}{d1d1e0}
\definecolor{redpink}{HTML}{ff9999}
\definecolor{lightyellow}{HTML}{ffffb3}
\definecolor{lightgreen}{HTML}{b3ffb3}
\definecolor{customcolor}{HTML}{1d5893}
\begin{document}

\begin{minipage}{\linewidth}
\vspace*{1cm}
\end{minipage}

\begin{minipage}{0.95\linewidth}
\flushleft
\begin{tikzpicture}
\pgfplotscreateplotcyclelist{defaultCycle}{%
ybar interval,ybar legend,fill=customcolor,draw=black,opacity=1,thin,solid,mark=no,mark options=solid,\\%
}
\begin{axis}
[
    xbar,
    cycle list name=defaultCycle,
    width=0.96\linewidth,
    height=0.15\textheight,
    use units,
    scale only axis,
    xticklabel style={/pgf/number format/fixed},,
    yticklabel style={/pgf/number format/fixed},,
    ymin=0,
    ytick pos=left,
    axis y line*=left,
    xtick pos=bottom,
    axis x line*=bottom,
    legend style={draw=none,at={(0,1.03)},anchor=south west},
    legend columns=-1,
    xtick align=center,
    ytick align=center,
    xtick distance=,
    ytick distance=,
    x tick label style ={font=\normalsize,text width=1.5cm,anchor=north east,rotate=0,align=center},
    y tick label style ={font=\normalsize,text width=2cm,anchor=east,rotate=0,align=right},
    scaled y ticks=false,
    bar width=10pt,
    grid=none,
    colorbar=false,
    colormap/viridis,
    xlabel=\textbf{Value},
    ylabel=\textbf{Count},
    x unit=,
    y unit=,
    title=\textbf{Sample Graph},
    ,
    ]
        \addplot+ table [x={x},y={y},meta index=2,col sep=semicolon] {
        x;  y;  z

        -6; 25; 0
        2;  22; 0
        9;  14; 0
        17; 14; 0
        24; 14; 0
        31; 10; 0
        38; 7;  0
        45; 8;  0
        52; 4;  0
        59; 1;  0
        66; 2;  0
        73; 1;  0
        80; 2;  0
        87; 3;  0
        94; 0;  0
        };
\end{axis}
\end{tikzpicture}
\end{minipage}

\begin{minipage}{\linewidth}
\vspace*{1cm}
\end{minipage}

\end{document}
2
  • 2
    Welcome to TeX.SE. It's helpful that you have added your code, however it is very long and contains many things that are not relevant for the issue. This makes it harder to answer because it is not clear where to look. Maybe you can edit the code to remove anything that is unrelated, like color definitions, minipages, tabular column definitions, all packages that are not pgfplots, any axis option that does not influence the issue. Make sure it still compiles after you have removed everything. You could also add a screenshot to illustrate what the problem is. – Marijn May 12 at 11:45
  • Remove text width=1.5cm,anchor=north east from your definition of x tick label style. – leandriis May 12 at 11:55
0

Your example code can be drastically simplified to the following MWE that still shows the alignment issue:

\documentclass[12pt]{article}

\usepackage{pgfplots}
\usepgfplotslibrary{units}

\begin{document}

\begin{tikzpicture}
\pgfplotscreateplotcyclelist{defaultCycle}{ybar interval}
\begin{axis}
[   cycle list name=defaultCycle,
    x tick label style ={font=\normalsize,text width=1.5cm,anchor=north east,rotate=0,align=center},
    y tick label style ={font=\normalsize,text width=2cm,anchor=east,rotate=0,align=right},
    ]
        \addplot+ table [x={x},y={y},col sep=semicolon] {
        x;  y;  z
        -6; 25; 0
        2;  22; 0
        9;  14; 0
        17; 14; 0
        };
\end{axis}
\end{tikzpicture}


\end{document}

enter image description here

Let's add draw=red to the x tick label style and we get the following output:

enter image description here

As one can see from the above image, each number is placed inside of a box that is 1.5cm wide. Numbers are horizontally centered within their box, and each boxes upper right corer is aligned with the corresponding tick, since youused text width=1.5cm and anchor=north east. Removing the two before mentioned options from the definition of x tick label style results in:

enter image description here

Applied to your example code, one ends up with the following:

enter image description here

5
  • this is very helpful but it still doesn't make the actual xticks line up with the bar lines – texstar May 12 at 12:11
  • I apparently misunderstood your question.Could you add a sketch of the expected output to your question? – leandriis May 12 at 12:19
  • As far as I can tell, the ticks don't line up with the outlines of the bars since the numbers don't match. You have an x tick at -5 but a bar that has an x value of -6. What kind of output/alignment do you expect? – leandriis May 12 at 12:20
  • i want the bar to match the xtick so the -6 should line up with the bar – texstar May 12 at 12:51
  • i have also added an expected output, the numbers in the image are just exemplary – texstar May 12 at 12:51
0

Seems you want xtick=data, in additiion to removing your xticklabel style as Ieandriis described.

output of code

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{units}
\pgfplotsset{width=10cm,compat=newest}
\definecolor{customcolor}{HTML}{1d5893}
\begin{document}

\begin{minipage}{0.95\linewidth}
\flushleft
\begin{tikzpicture}
\pgfplotscreateplotcyclelist{defaultCycle}{%
ybar interval,ybar legend,fill=customcolor,draw=black,opacity=1,thin,solid,mark=no,mark options=solid,\\%
}
\begin{axis}
[
    cycle list name=defaultCycle,
    width=0.96\linewidth,
    height=0.15\textheight,
    use units,
    scale only axis,
    xtick=data,
    xticklabel style={/pgf/number format/fixed},
    yticklabel style={/pgf/number format/fixed},
    ymin=0,
    ytick pos=left,
    axis y line*=left,
    xtick pos=bottom,
    axis x line*=bottom,
    legend style={draw=none,at={(0,1.03)},anchor=south west},
    legend columns=-1,
    xtick align=center,
    ytick align=center,
    ytick distance=,
    scaled y ticks=false,
    bar width=10pt,
    grid=none,
    colorbar=false,
    colormap/viridis,
    xlabel=\textbf{Value},
    ylabel=\textbf{Count},
    x unit=,
    y unit=,
    title=\textbf{Sample Graph},
    ]
        \addplot+ table [x={x},y={y},meta index=2,col sep=semicolon] {
        x;  y;  z
        -6; 25; 0
        2;  22; 0
        9;  14; 0
        17; 14; 0
        24; 14; 0
        31; 10; 0
        38; 7;  0
        45; 8;  0
        52; 4;  0
        59; 1;  0
        66; 2;  0
        73; 1;  0
        80; 2;  0
        87; 3;  0
        94; 0;  0
        };
\end{axis}
\end{tikzpicture}
\end{minipage}

\end{document}

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.