pgfplots: только целые числа и только отметки в целых числах на оси x

pgfplots: только целые числа и только отметки в целых числах на оси x

введите описание изображения здесь

Я хочу создать xbarтакие диаграммы, но я хочу установить

  • только целые числана нижней оси x (без 0,5, 1,5, 2,5, ...) и
  • только галочки на местах целых чисел(нет отметок 0,5, 1,5, 2,5, ...)

Есть и еще одна проблема: xmaxxtick={1,2,...,\n}изменяется от 1 до «~400» (при той же ширине диаграммы), поэтому явно задавать x, как (здесь ) , будет не очень хорошей идеей \def\n{400}.
Автоматическая адаптивная маркировка оси X pgfplotsдопустима, но должны отображаться только целые числа.


Я попробовал изздесь

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

но этот метод все равно установит такты (на 0,5, 1,5, 2,5 и т. д.)...


Что мне нужно сделать?

введите описание изображения здесь

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

решение1

может быть, вот так?

  • Вы xtickявно указываете, что отметки должны быть только для целых чисел в указанном вами диапазоне.
  • Удалите условный код xticklabel, поскольку вы явно определяете позиции xtick.

введите описание изображения здесь

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

Изменить 1, чтобы принять во вниманиеxmax =400 введите описание изображения здесь

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

Связанный контент