pgfplots: apenas números inteiros e apenas ticks em casas inteiras no eixo x

pgfplots: apenas números inteiros e apenas ticks em casas inteiras no eixo x

insira a descrição da imagem aqui

Quero criar xbardiagramas como este, mas quero definir

  • apenas números inteirosno eixo x inferior (sem 0,5, 1,5, 2,5, ...) e
  • apenas marca nas casas dos números inteiros(sem marcações em 0,5, 1,5, 2,5, ...)

Há também outro problema: xmaxvai de 1 até '~400' (com a mesma largura do diagrama), então não será uma boa ideia definir x explicitamente, como xtick={1,2,...,\n}(aqui \def\n{400}).
A rotulagem automática e adaptativa do eixo x pgfplotsestá OK, mas apenas números inteiros devem aparecer.


Eu tentei deaqui

% 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%
},  

mas este método ainda definirá os ticks (em 0,5, 1,5, 2,5 e assim por diante)...


O que eu tenho que fazer?

insira a descrição da imagem aqui

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

Responder1

talvez assim?

  • Você define xtickexplicitamente para ter ticks apenas em números inteiros dentro do intervalo especificado.
  • Remova o código xticklabel condicional, pois você define explicitamente as posições xtick.

insira a descrição da imagem aqui

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

Edite 1 para levar em consideraçãoxmax =400 insira a descrição da imagem aqui

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

informação relacionada