Y 轴无法设置为对数刻度

Y 轴无法设置为对数刻度

我想要一个在两个轴上都有对数刻度的图表,并且有下面的工作代码。如果我在第五行开始注释,y axis代码就不会编译。

\begin{tikzpicture} [scale=2]

\datavisualization [
scientific axes,
x axis={logarithmic, ticks={major={at={1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048}}}},
%% y axis={logarithmic, ticks={major={at={0.01, 1, 100, 10000}}}},
visualize as smooth line/.list={nlogn, testedTriangles, edgeFlips}, 
nlogn ={pin in data={text=$2.43184\cdot n\cdot log(n)$}},
testedTriangles={pin in data={text=$\varnothing Anzahl getesteter Dreiecke$}},
edgeFlips={pin in data={text=$\varnothing Anzahl Edge-Flips$}}
]

data [set=nlogn] {
    x, y
    1, 0
    2, 4.86368
    4, 19.45472
    8, 58.36416
    16, 155.63776
    32, 389.0944
    64, 933.82656
    128, 2178.92864
    256, 4980.40832
    512, 11205.91872
    1024, 24902.0416
    2048, 54784.49152
}
data [set=testedTriangles] {
    x, y
    1, 0
    2, 0
    4, 3.5773
    8, 16.5964
    16, 55.4506
    32, 166.9217
    64, 469.8356
    128, 1293.5265
    256, 3583.0332
    512, 9794.2665
    1024, 27411.0824
    2048, 76718.862
}
data [set=edgeFlips] {
    x, y
    1, 0
    2, 0
    4, 0.4331
    8, 4.4301
    16, 17.7114
    32, 52.7935
    64, 131.8179
    128, 305.597
    256, 668.4145
    512, 1411.642
    1024, 2922.9684
    2048, 5960.823
};

\end{tikzpicture}

错误消息是Missing number, treated as zero.,并指示上述代码片段的倒数第二行包含};

答案1

除了Huang_d 指出的数据问题我建议进行一些改变,主要是为了让视觉信息更加明显。主要观点:

  • delete datax 和 y 的值均为 0,因为两个轴都是对数
  • 用于scientific axes = clean,稍微移动轴
  • 用 -s 替换 pins legendtext除非我错过了什么要点
  • 或者使用 aclassical log-scale表示 x (10, 100, 1000 ...)
  • 或者定义一个合适的放置策略,这里\bnry:它省略了一些刻度,以避免与右侧重叠
  • 使用info{ }块将节点作为标题
  • label具有有用名称的 x 轴和 y 轴
  • 重新排列选项\datavisualization,从必选变为可选,并按照某种逻辑顺序排列
  • 使用类单独绘制此图standalone,使用考虑到 pdf 的导入其他文件

结果

\documentclass[10pt,border=3mm,tikz]{standalone}
\usetikzlibrary{datavisualization}  %

% ~~~ shortcuts ~~~~~~~~~~~~~~~~~~~~~~~~
\def\dvbb{data visualization bounding box}  % shortcut to this box

\def\bnry{%                     partitioning the x-axis
 \tikzdatavisualizationset{%
    major={at={1, 2, 4, 8, 16, 32, 64, 128, 512, 2048}}%
 }%
}


% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}

 \begin{tikzpicture}
    \datavisualization [
        scientific axes = clean,
        visualize as smooth line/.list=
                    {nlogn,testedTriangles, edgeFlips},
        x axis={
            logarithmic,
            min value=1,
            tick placement strategy=\bnry,% see preamble
            label = {X=?},
        },
        y axis={
            logarithmic,
            min value=0.1,
            label = {Y=?},
        },
        style sheet=vary dashing,
        % ~~~ legend ~~~
        nlogn           = {label in legend={text=nlogn}},
        testedTriangles = {label in legend={text=testedTriangles}},
        edgeFlips       = {label in legend={text=edgeFlips}},
        % ~~~ grids ~~~~~~~~
        all axes = grid,
    ]
    data [set=nlogn] {
        x, y
        2, 4.86368
        4, 19.45472
        8, 58.36416
        16, 155.63776
        32, 389.0944
        64, 933.82656
        128, 2178.92864
        256, 4980.40832
        512, 11205.91872
        1024, 24902.0416
        2048, 54784.49152
    }
    data [set=testedTriangles] {
        x, y
        4, 3.5773
        8, 16.5964
        16, 55.4506
        32, 166.9217
        64, 469.8356
        128, 1293.5265
        256, 3583.0332
        512, 9794.2665
        1024, 27411.0824
        2048, 76718.862
    }
    data [set=edgeFlips] {
        x, y
        4, 0.4331
        8, 4.4301
        16, 17.7114
        32, 52.7935
        64, 131.8179
        128, 305.597
        256, 668.4145
        512, 1411.642
        1024, 2922.9684
        2048, 5960.823
    }
    info{
        \node[anchor=south] at  (\dvbb.north) 
                                {Comparing different strategies};
    }
    ;

 \end{tikzpicture}
\end{document}

相关内容