![pgfplots:僅整數且僅在 x 軸上的整數位置處進行刻度](https://rvso.com/image/475720/pgfplots%EF%BC%9A%E5%83%85%E6%95%B4%E6%95%B8%E4%B8%94%E5%83%85%E5%9C%A8%20x%20%E8%BB%B8%E4%B8%8A%E7%9A%84%E6%95%B4%E6%95%B8%E4%BD%8D%E7%BD%AE%E8%99%95%E9%80%B2%E8%A1%8C%E5%88%BB%E5%BA%A6.png)
我想創建xbar
這樣的圖,但我想設置
- 僅限整數位於底部 x 軸(無 0.5、1.5、2.5,...) 和
- 只在整數的地方打勾(0.5、1.5、2.5...處無刻度)
還有一個問題: xmax
從 1 到 '~400'(具有相同的圖表寬度),因此明確設定 x 不是一個好主意,例如xtick={1,2,...,\n}
(此處 \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}
\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}