![pgfplots: 정수만 있고 x축의 정수 자리에만 틱 표시](https://rvso.com/image/475720/pgfplots%3A%20%EC%A0%95%EC%88%98%EB%A7%8C%20%EC%9E%88%EA%B3%A0%20x%EC%B6%95%EC%9D%98%20%EC%A0%95%EC%88%98%20%EC%9E%90%EB%A6%AC%EC%97%90%EB%A7%8C%20%ED%8B%B1%20%ED%91%9C%EC%8B%9C.png)
xbar
이런 다이어그램을 만들고 싶지만 설정하고 싶습니다.
- 정수만하단 x축(0.5, 1.5, 2.5, ... 없음) 그리고
- 정수 자리에만 틱 표시(0.5, 1.5, 2.5, ...에서는 틱 없음)
또 다른 문제도 있습니다: xmax
xtick={1,2,...,\n}
1에서 '~400'(동일한 다이어그램 너비 사용)까지 진행되므로 (here \def\n{400}
) 처럼 x를 명시적으로 설정하는 것은 좋지 않습니다 .
자동 적응형 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
지정된 범위 내의 정수에만 틱이 표시되도록 명시적으로 설정했습니다 .- xtick 위치를 명시적으로 정의했으므로 조건부 xticklabel 코드를 제거하세요.
\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}
\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}