Under- and Overfull Tikzpicture

overfulloverleafscalingtikz-pgfunderfull

Im trying for DAYS now to correctly format my two Diagrams. I tried plenty of solutions on this Page, but nothing worked (Adjustbox, environ, scale,…)
The first one is this:

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
      \begin{axis}[
        ybar,
        bar width=17pt,
        width = 0.7\textwidth,
        %
        nodes near coords,
        nodes near coords align=center,
        nodes near coords style={
            /pgf/number format/fixed,
            /pgf/number format/precision=0,
            /pgf/number format/fixed zerofill,
            rotate=90,
            anchor=east,
            color=white
            },
        bar shift=0pt,
        %
        axis x line=center,
        axis y line=left,
        %
        ylabel=Anzahl erfasster Fälle,
        ymin=0,
        ytick={0,20000,40000,60000,80000,100000,120000,140000},
        yticklabel style={
            /pgf/number format/fixed,
            /pgf/number format/precision=0,
            /pgf/number format/fixed zerofill
        },
        scaled y ticks=false,
        enlargelimits=auto,
        %
        /pgf/number format/use comma,
        x = 22pt,
        symbolic x coords ={2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021},
        xtick = {2007,...,2021},
        x tick label style={rotate=30,anchor=north east},
        ]
        \addplot[fill=blue!70] coordinates {
          (2007,34180)
          (2008,37900)
          (2009,50254)
          (2010,59839)
          (2011,59494)
          (2012,63959)
          (2013,64426)
          (2014,49925)
          (2015,45793)
          (2016,82649)
          (2017,85960)
          (2018,87106)
          (2019,100514)
          (2020,108474)
          (2021,124137)
        };
      \end{axis} 
    \end{tikzpicture}
\end{figure}

Error is:

Overfull \hbox (12.0321pt too wide) in paragraph at lines 141--142

141 is \end{tikzpicture} and 142 is \end{figure}
It is visually visible, that it stands out of the textwidth. Strangly, the ´width´ i setted adjusts the height of the diagram. So if i scale the 0.7 to 0.9 for example, the diagram increases in height.

And the second is this:

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            xbar,
            width=\textwidth/1.5, 
            height=10cm,
            %enlarge x limits=0.5,
            xlabel={Anteil der Befragten},
            xticklabel={\pgfmathparse{\tick}\pgfmathprintnumber{\pgfmathresult}\%},
            axis y line*=left,
            axis x line*=bottom,
            xmin=0,
            %tick pos=left,
            nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
            symbolic y coords={Man-in-the-Middle Angriffe, Cross-Site-Scripting, \Gls{sql}-Injection, Ransomware, Angriffe auf Passwörter, Phishing-Angriffe, Spoofing, \Gls{ddos}-Attacken, Infizierung mit Schadsoftware bzw. Malware},
            ytick=data,
            y tick label style={
                %rotate=30,
                anchor=east,
                text width=5cm,
                align=right
            }
        ]
        \addplot[fill=blue!70] coordinates {
            (31,Infizierung mit Schadsoftware bzw. Malware) 
            (27,\Gls{ddos}-Attacken)
            (20,Spoofing)
            (18,Phishing-Angriffe)
            (18,Angriffe auf Passwörter)
            (18,Ransomware)
            (17,\Gls{sql}-Injection)
            (9,Cross-Site-Scripting)
            (5,Man-in-the-Middle Angriffe)
        };
        \end{axis}
\end{figure}

Error here is:

Underfull \hbox (badness 10000) in paragraph at lines 185--185
Underfull \hbox (badness 2005) in paragraph at lines 185--185
Underfull \hbox (badness 5359) in paragraph at lines 185--185
Underfull \hbox (badness 1052) in paragraph at lines 185--185

185 is \end{axis}

Im getting pretty frustrated with this. Both are my first diagrams ever in Latex. Am i missing something very obvious? Im using Overleaf for Latex, if this matters.

Edit: Example:

\documentclass{scrreport}

\usepackage[utf8]{inputenc}%Encoding
\usepackage[shorthands=off,english,ngerman]{babel}%Sprache
\usepackage[T1]{fontenc}
\usepackage[acronym]{glossaries}% nutzung von acronymen
\usepackage[chapter]{minted}% minted für code listing sieht einfach schöner aus
\usepackage{csquotes}
\usepackage{pgfplots}
\pgfplotsset{
  compat=newest,
  scaled y ticks=false
}
\usetikzlibrary{positioning}
\newacronym{ddos}{DDOS}{Distributed Denial of Service}
\newacronym{sql}{SQL}{Structured Query Language}
\begin{document}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            xbar,
            width=\textwidth/1.5, 
            height=10cm,
            %enlarge x limits=0.5,
            xlabel={Anteil der Befragten},
            xticklabel={\pgfmathparse{\tick}\pgfmathprintnumber{\pgfmathresult}\%},
            axis y line*=left,
            axis x line*=bottom,
            xmin=0,
            %tick pos=left,
            nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
            symbolic y coords={Man-in-the-Middle Angriffe, Cross-Site-Scripting, \Gls{sql}-Injection, Ransomware, Angriffe auf Passwörter, Phishing-Angriffe, Spoofing, \Gls{ddos}-Attacken, Infizierung mit Schadsoftware bzw. Malware},
            ytick=data,
            y tick label style={
                %rotate=30,
                anchor=east,
                text width=5cm,
                align=right
            }
        ]
        \addplot[fill=blue!70] coordinates {
            (31,Infizierung mit Schadsoftware bzw. Malware) 
            (27,\Gls{ddos}-Attacken)
            (20,Spoofing)
            (18,Phishing-Angriffe)
            (18,Angriffe auf Passwörter)
            (18,Ransomware)
            (17,\Gls{sql}-Injection)
            (9,Cross-Site-Scripting)
            (5,Man-in-the-Middle Angriffe)
        };
        \end{axis}
    \end{tikzpicture}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
      \begin{axis}[
        ybar,
        bar width=17pt,
        width = 0.7\textwidth,
        %
        nodes near coords,
        nodes near coords align=center,
        nodes near coords style={
            /pgf/number format/fixed,
            /pgf/number format/precision=0,
            /pgf/number format/fixed zerofill,
            rotate=90,
            anchor=east,
            color=white
            },
        bar shift=0pt,
        %
        axis x line=center,
        axis y line=left,
        %
        ylabel=Anzahl erfasster Fälle,
        ymin=0,
        ytick={0,20000,40000,60000,80000,100000,120000,140000},
        yticklabel style={
            /pgf/number format/fixed,
            /pgf/number format/precision=0,
            /pgf/number format/fixed zerofill
        },
        scaled y ticks=false,
        enlargelimits=auto,
        %
        /pgf/number format/use comma,
        x = 22pt,
        symbolic x coords ={2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021},
        xtick = {2007,...,2021},
        x tick label style={rotate=30,anchor=north east},
        ]
        \addplot[fill=blue!70] coordinates {
          (2007,34180)
          (2008,37900)
          (2009,50254)
          (2010,59839)
          (2011,59494)
          (2012,63959)
          (2013,64426)
          (2014,49925)
          (2015,45793)
          (2016,82649)
          (2017,85960)
          (2018,87106)
          (2019,100514)
          (2020,108474)
          (2021,124137)
        };
      \end{axis} 
    \end{tikzpicture}
\end{figure}
\end{document}

Best Answer

Here is one possibility to fix your problems (see commands in the code):

\documentclass{scrreport}

\usepackage[utf8]{inputenc}%Encoding
\usepackage[shorthands=off,english,ngerman]{babel}%Sprache
\usepackage[T1]{fontenc}
\usepackage[acronym]{glossaries}% nutzung von acronymen
\usepackage[chapter]{minted}% minted für code listing sieht einfach schöner aus
\usepackage{csquotes}
\usepackage{pgfplots}
\pgfplotsset{
  compat=newest,
  scaled y ticks=false
}
\usetikzlibrary{positioning}
\newacronym{ddos}{DDOS}{Distributed Denial of Service}
\newacronym{sql}{SQL}{Structured Query Language}
\begin{document}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            xbar,
            width=\textwidth/1.5,% recommendation: use X/\textwidth 
            height=10cm,
            %enlarge x limits=0.5,
            xlabel={Anteil der Befragten},
            xticklabel={\pgfmathparse{\tick}\pgfmathprintnumber{\pgfmathresult}\%},
            axis y line*=left,
            axis x line*=bottom,
            xmin=0,
            %tick pos=left,
            nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
            symbolic y coords={Man-in-the-Middle\\Angriffe, Cross-Site-Scripting, SQL-Injection, Ransomware, Angriffe auf Passwörter, Phishing-Angriffe, Spoofing, DDOS-Attacken, Infizierung mit Schad-\\software bzw. Malware},
            ytick=data,
            y tick label style={
                %rotate=30,
%                anchor=east,   % <-- removed
%                text width=5cm,% <-- removed
                align=right
            }
        ]
        \addplot[fill=blue!70] coordinates {% <-- added line breaks manually
            (31,Infizierung mit Schad-\\software bzw. Malware) 
            (27,DDOS-Attacken)
            (20,Spoofing)
            (18,Phishing-Angriffe)
            (18,Angriffe auf Passwörter)
            (18,Ransomware)
            (17,SQL-Injection)
            (9,Cross-Site-Scripting)
            (5,Man-in-the-Middle\\Angriffe)
        };
        \end{axis}
    \end{tikzpicture}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
      \begin{axis}[
        ybar,
        bar width=12pt,% <-- smaller number to get a little space between the bars
        width = 0.7\textwidth,
        %
        nodes near coords,
        nodes near coords align=center,
        nodes near coords style={
            /pgf/number format/fixed,
            /pgf/number format/precision=0,
            /pgf/number format/fixed zerofill,
            rotate=90,
            anchor=east,
            color=white
            },
        bar shift=0pt,
        %
        axis x line=center,
        axis y line=left,
        %
        ylabel=Anzahl erfasster Fälle,
        ymin=0,
        ytick={0,20000,40000,60000,80000,100000,120000,140000},
        yticklabel style={
            /pgf/number format/fixed,
            /pgf/number format/precision=0,
            /pgf/number format/fixed zerofill
        },
        scaled y ticks=false,
        enlargelimits=auto,
        %
        /pgf/number format/use comma,
%       x = 22pt,% <-- removed
        symbolic x coords ={2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021},
        xtick = {2007,...,2021},
        x tick label style={rotate=30,anchor=north east,font=\footnotesize},%  <-- added font=\footnotesize
        ]
        \addplot[fill=blue!70] coordinates {
          (2007,34180)
          (2008,37900)
          (2009,50254)
          (2010,59839)
          (2011,59494)
          (2012,63959)
          (2013,64426)
          (2014,49925)
          (2015,45793)
          (2016,82649)
          (2017,85960)
          (2018,87106)
          (2019,100514)
          (2020,108474)
          (2021,124137)
        };
      \end{axis} 
    \end{tikzpicture}
\end{figure}
\end{document}

Output without overfull \hbox errors.

Related Question