我想创建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
明确设置仅在指定范围内的整数处进行刻度。 - 由于您明确定义了 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}