pgfplots: solo números enteros y solo ticks en lugares enteros en el eje x

pgfplots: solo números enteros y solo ticks en lugares enteros en el eje x

ingrese la descripción de la imagen aquí

Quiero crear xbardiagramas como este, pero quiero establecer

  • solo numeros enterosen la parte inferior del eje x (no 0,5, 1,5, 2,5, ...) y
  • sólo marca en los lugares de los números enteros(sin tics en 0,5, 1,5, 2,5, ...)

También hay otro problema: xmaxva desde 1 hasta '~400' (con el mismo ancho de diagrama), por lo que no será una buena idea establecer x explícitamente, como xtick={1,2,...,\n}(aquí \def\n{400}).
El etiquetado automático y adaptativo del eje x pgfplotsestá bien, pero solo deberían aparecer números enteros.


lo intenté desdeaquí

% https://tex.stackexchange.com/a/286623/46023
xticklabel={%
        \pgfmathtruncatemacro{\IntegerTick}{\tick}%
        \pgfmathprintnumberto[verbatim,fixed,precision=3]{\tick}\tickAdjusted%
        \pgfmathparse{\IntegerTick == \tickAdjusted ? 1: 0}%
        \ifnum\pgfmathresult>0\relax$\IntegerTick$\else\fi%
},  

pero este método seguirá configurando los ticks (en 0,5, 1,5, 2,5 y así sucesivamente)...


¿Que tengo que hacer?

ingrese la descripción de la imagen aquí

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\foreach \n in {1,...,6,155,400}{%%%%%%%%%%%%%%%%%

\pgfmathtruncatemacro\A{random(0,\n)}
\pgfmathtruncatemacro\B{random(0,\n-1)}
\pgfmathtruncatemacro\R{abs(\n-\A-\B)}

\begin{tikzpicture}[]
\begin{axis}[
width=11cm,% needed 
height=5cm, 
xbar,
bar shift=0pt,
bar width=5mm,
y tick label as interval,
nodes near coords, 
axis x line*=none, % hide upper x-axis
%
ymin=0.5, ymax=4.5,
xmin=0, xmax=\n,
extra x ticks={\n}, % for savety, sometimes he will not set the last....
ytick={1,...,4},
yticklabels={Rest, B, A}, 
title={Diagram No. \n},
title style={anchor=south, yshift=3mm,},
%%% https://tex.stackexchange.com/a/286623/46023
%xticklabel={%
%        \pgfmathtruncatemacro{\IntegerTick}{\tick}%
%        \pgfmathprintnumberto[verbatim,fixed,precision=3]{\tick}\tickAdjusted%
%        \pgfmathparse{\IntegerTick == \tickAdjusted ? 1: 0}%
%        \ifnum\pgfmathresult>0\relax$\IntegerTick$\else\fi%
%},  
]
\addplot[fill=blue] coordinates { (\A, 3.5) };
\addplot[fill=brown] coordinates { (\B, 2.5) };
\addplot[fill=gray] coordinates { (\R, 1.5) };
\end{axis}

% 2nd percentual axis ==========================
\begin{axis}[
width=11cm, 
height=5cm, 
axis x line=top, 
hide y axis, 
x axis line style={-},
xticklabel={\pgfmathparse{100*\tick}\pgfmathprintnumber[precision=0]{\pgfmathresult}\%},
minor tick num=1,
]
\end{axis}
% ======================================
\end{tikzpicture}
}%% end foreach %%%%%%%%%%%%%%%%%%%%%
\end{document}

Respuesta1

tal vez así?

  • Lo configura xtickexplícitamente para tener ticks solo en números enteros dentro de su rango especificado.
  • Elimine el código xticklabel condicional ya que define explícitamente las posiciones de xtick.

ingrese la descripción de la imagen aquí

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\foreach \n in {1,...,6}{%%%%%%%%%%%%%%%%%

\pgfmathtruncatemacro\A{random(0,\n)}
\pgfmathtruncatemacro\B{random(0,\n-1)}
\pgfmathtruncatemacro\R{abs(\n-\A-\B)}

\begin{tikzpicture}[]
\begin{axis}[
width=11cm,% needed 
height=5cm, 
xbar,
bar shift=0pt,
bar width=5mm,
y tick label as interval,
nodes near coords, 
axis x line*=none, % hide upper x-axis
%
ymin=0.5, ymax=4.5,
xmin=0, xmax=\n,
xtick={0,...,\n}, % Set ticks only at integer positions
ytick={1,...,4},
yticklabels={Rest, B, A}, 
title={Diagram No. \n},
title style={anchor=south, yshift=3mm,},
]
\addplot[fill=blue] coordinates { (\A, 3.5) };
\addplot[fill=brown] coordinates { (\B, 2.5) };
\addplot[fill=gray] coordinates { (\R, 1.5) };
\end{axis}

% 2nd percentual axis ==========================
\begin{axis}[
width=11cm, 
height=5cm, 
axis x line=top, 
hide y axis, 
x axis line style={-},
xticklabel={\pgfmathparse{100*\tick}\pgfmathprintnumber[precision=0]{\pgfmathresult}\%},
minor tick num=1,
]
\end{axis}
% ======================================
\end{tikzpicture}
}%% end foreach %%%%%%%%%%%%%%%%%%%%%
\end{document}

Edito 1 para tener en cuentaxmax =400 ingrese la descripción de la imagen aquí

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\foreach \n in {1,...,6}{%%%%%%%%%%%%%%%%%

\pgfmathtruncatemacro\A{random(0,\n)}
\pgfmathtruncatemacro\B{random(0,\n-1)}
\pgfmathtruncatemacro\R{abs(\n-\A-\B)}

\begin{tikzpicture}[]
\begin{axis}[
width=11cm,% needed 
height=5cm, 
xbar,
bar shift=0pt,
bar width=5mm,
y tick label as interval,
nodes near coords, 
axis x line*=none, % hide upper x-axis
%
ymin=0.5, ymax=4.5,
xmin=0, xmax=400, % <= Adjusted here
xtick distance=40, % Set ticks at every 40 units
ytick={1,...,4},
yticklabels={Rest, B, A}, 
title={Diagram No. \n},
title style={anchor=south, yshift=3mm,},
]
\addplot[fill=blue] coordinates { (\A, 3.5) };
\addplot[fill=brown] coordinates { (\B, 2.5) };
\addplot[fill=gray] coordinates { (\R, 1.5) };
\end{axis}

% 2nd percentual axis ==========================
\begin{axis}[
width=11cm, 
height=5cm, 
axis x line=top, 
hide y axis, 
x axis line style={-},
xticklabel={\pgfmathparse{100*\tick}\pgfmathprintnumber[precision=0]{\pgfmathresult}\%},
minor tick num=1,
]
\end{axis}
% ======================================
\end{tikzpicture}
}%% end foreach %%%%%%%%%%%%%%%%%%%%%
\end{document}

información relacionada