不足和过满的 Tikzpicture

不足和过满的 Tikzpicture

我花了好几天时间尝试正确格式化我的两个图表。我在这个页面上尝试了很多解决方案,但都没有奏效(Adjustbox、environ、scale……)第一个是这样的:

\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}

错误是:

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

141 是\end{tikzpicture},142 是\end{figure} 从视觉上看,它突出于文本宽度。奇怪的是,我设置的“宽度”调整了图表的高度。因此,如果我将 0.7 缩放到 0.9,图表的高度就会增加。

第二条是这样的:

\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}

这里的错误是:

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 是\end{axis}

我对此感到非常沮丧。这两张图都是我第一次用 Latex 制作的。我是不是忽略了一些非常明显的东西?如果这很重要的话,我正在使用 Overleaf for Latex。

编辑:示例:

\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}

答案1

以下是解决您的问题的一种可能性(请参阅代码中的命令):

\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}

输出时不包含过满 \hbox 错误。

相关内容