![pgfplots: nur Ganzzahlen und nur Striche an ganzzahligen Stellen auf der x-Achse](https://rvso.com/image/475720/pgfplots%3A%20nur%20Ganzzahlen%20und%20nur%20Striche%20an%20ganzzahligen%20Stellen%20auf%20der%20x-Achse.png)
Ich möchte xbar
Diagramme wie dieses erstellen, aber ich möchte
- nur Ganzzahlenan der unteren x-Achse (Nr. 0,5, 1,5, 2,5, ...) Und
- setzt nur Häkchen an den Stellen von Ganzzahlen(keine Häkchen bei 0,5, 1,5, 2,5, …)
Es gibt noch ein weiteres Problem: xmax
geht von 1 bis '~400' (bei gleicher Diagrammbreite), daher ist es keine gute Idee, x explizit festzulegen, wie xtick={1,2,...,\n}
(hier \def\n{400}
).
Die automatische, adaptive Beschriftung der X-Achse pgfplots
ist in Ordnung, es sollten jedoch nur ganze Zahlen angezeigt werden.
Ich habe es versucht vonHier
% 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%
},
aber diese Methode setzt trotzdem die Ticks (bei 0,5, 1,5, 2,5 usw.) ...
Was muss ich tun?
\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}
Antwort1
vielleicht so?
- Sie legen
xtick
explizit fest, dass nur bei Ganzzahlen innerhalb des von Ihnen angegebenen Bereichs Ticks vorhanden sein sollen. - Entfernen Sie den bedingten Xticklabel-Code, da Sie Xtick-Positionen explizit definieren.
\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}
Bearbeiten 1 zu berücksichtigenxmax =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}