TikZ 数据可视化:图钉/标签在图形和标题之间添加空白

TikZ 数据可视化:图钉/标签在图形和标题之间添加空白

我正在使用 TikZ 的数据可视化功能来创建图形。这工作得很好,直到我向图形添加图钉和/或标签;这样做会在图形本身和其标题之间产生大量空白。以下是 MWE:

%!TEX TS-program = xelatex
\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}

\begin{document}
    \begin{figure}[h!]
        \centering%
        \caption{Pareto frontier of risky investments}%
        \begin{tikzpicture}[scale=2.5]
            \datavisualization [
                scientific axes = clean
            ]
            [
                visualize as line/.list = { pareto, tangent },
                visualize as scatter/.list = { investments, portfolios },
                y axis = { 
                    label     = { $\mu$ [\%] },
                    attribute = M,
                    min value = 0,
                    max value = 25
                },
                x axis = { 
                    label     = { $\sigma$ [\%]}, 
                    attribute = S,
                    min value = 0,
                    max value = 70
                },
                pareto = {
                    label in data = {
                        when = S is 19,
                        text = Natasha Fund
                    },
                    label in data = {
                        when = S is 60,
                        text = Hannah Corporation
                    }
                },
                tangent = {
                    style = dashed,
                },
                investments  = {
                    pin in data = {
                        when = S is 0,
                        text = risk-free investment
                    }
                },
                portfolios = {
                    pin in data = {
                        when = S is 18.29379,
                        text' = $P^\star$
                    },
                }
            ]
            data [set = pareto, format = function] {
                var  t : interval [0:1] samples 1000;
                func M = 15 + 5*\value{t};
                func S = sqrt(3961*\value{t}^2 - 722*\value{t} + 361);
            }
            data [set = tangent, headline = {S, M}] {
                0, 3.2
                32.09828, 25
            }
            data [set = investments, headline = {S, M}] {
                0, 3.2
                19, 15
                60, 20
            }
            data [set = portfolios, headline = {S, M}] {
                18.29379, 15.62466
            }
            ;
        \end{tikzpicture}%
    \end{figure}
\end{document}

得出的结果为:

MWE:数据中存在图钉/标签时会出现额外的空白

注释掉引脚/标签可得到预期结果:

MWE:一旦数据中的图钉/标签被移除,就不会有多余的空格

寻找类似的问题,我发现图片和标题之间的间距较大,但没有解决方案。我被困住了,希望得到任何帮助、提示、指点或建议。谢谢!

附录:以防万一,我在 Windows 上使用 MiKTeX 2.9。我已更新 MiKTex 以确保问题不是由过时的软件包引起的,但无济于事。

答案1

我认为我建议使用datavisualization库的缩放功能,而不是scale直接使用。也就是说,例如添加

x axis={length=12cm},
y axis={length=7cm}

\datavisualization选项中,设置轴的大小。不过我确实需要稍微修改一下引脚,请参阅代码中的注释。

还有其他方法:

  • scientific axes = {clean,width=12cm,height=7cm}widthheight是的属性scientific axis)。
  • x axis={unit length=1cm per 10 units}y 轴也类似。即设置给定数量的单位长度应使用的长度。
  • 可能还有其他。我不太了解这个库,翻阅手册可能会找到更多选项。

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}

\begin{document}
    \begin{figure}[h!]
       \centering%
        \caption{Pareto frontier of risky investments}%
        \begin{tikzpicture}%[scale=2.5]
            \datavisualization [
                scientific axes = clean,
                x axis={length=12cm},
                y axis={length=7cm}
            ]
            [
                visualize as line/.list = { pareto, tangent },
                visualize as scatter/.list = { investments, portfolios },
                y axis = { 
                    label     = { $\mu$ [\%] },
                    attribute = M,
                    min value = 0,
                    max value = 25
                },
                x axis = { 
                    label     = { $\sigma$ [\%]}, 
                    attribute = S,
                    min value = 0,
                    max value = 70
                },
                pareto = {
                    label in data = {
                        when = S is 19,
                        text' = Natasha Fund % changed from text to text'
                    },
                    label in data = {
                        when = S is 60,
                        text = Hannah Corporation
                    }
                },
                tangent = {
                    style = dashed,
                },
                investments  = {
                    pin in data = {
                        when = S is 0,
                        text = risk-free investment,
                        node style={right} % added
                    }
                },
                portfolios = {
                    pin in data = {
                        when = S is 18.29379,
                        text' = $P^\star$,
                        pin length = 1cm, % added
                        pin angle = -30 % added
                    },
                }
            ]
            data [set = pareto, format = function] {
                var  t : interval [0:1] samples 1000;
                func M = 15 + 5*\value{t};
                func S = sqrt(3961*\value{t}^2 - 722*\value{t} + 361);
            }
            data [set = tangent, headline = {S, M}] {
                0, 3.2
                32.09828, 25
            }
            data [set = investments, headline = {S, M}] {
                0, 3.2
                19, 15
                60, 20
            }
            data [set = portfolios, headline = {S, M}] {
                18.29379, 15.62466
            }
            ;
        \end{tikzpicture}%
    \end{figure}
\end{document}

相关内容