直方图刻度对齐

直方图刻度对齐

在此处输入图片描述我正在尝试在乳胶中创建直方图,但刻度与条形图不对齐,我不确定为什么。

这是我目前所拥有的:

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

答案1

您的示例代码可以大大简化为以下 MWE,但仍然显示对齐问题:

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

在此处输入图片描述

我们将draw=red其添加到x tick label style并得到以下输出:

在此处输入图片描述

从上图可以看出,每个数字都放在一个 1.5 厘米宽的框内。数字在框内水平居中,每个框的右上角与相应的刻度对齐,因为您使用了text width=1.5cmanchor=north east。从 的定义x tick label style中删除前面提到的两个选项将得到:

在此处输入图片描述

应用于您的示例代码,最终得到以下内容:

在此处输入图片描述

答案2

看来您想要的xtick=data除了删除您之外,xticklabel style正如 Ieandriis 所描述的。

代码输出

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

相关内容