このような図を作成したいのですxbar
が、設定したい
- 整数のみ下部の x 軸 (0.5、1.5、2.5、... なし) そして
- 整数の場所にのみチェックマークを付けます(0.5、1.5、2.5、... には目盛りがありません)
他にも問題があります: xmax
xtick={1,2,...,\n}
1 から '~400' までの範囲になります (図の幅は同じ)。そのため、 (ここ \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}