在mathematica图中使Axis和ticks不可见,但保留标签

时间:2017-04-07 12:19:39

标签: wolfram-mathematica

我想制作一个没有可见y轴的mathematica图,但保留刻度标签。

我已经尝试过AxesStyle - > {Thickness [.001],Thickness [0]}没有效果,并且将不透明度设置为0也会使刻度标签完全透明(因此不可见)。

非常感谢任何帮助...

1 个答案:

答案 0 :(得分:1)

p = Plot[Sin[x], {x, 0, 6 Pi},
 AxesStyle -> {Black, Opacity[0]},
 TicksStyle -> Directive[Opacity[1], Black]]

ticks = AbsoluteOptions[p, Ticks];

ticks[[1, 2, 2]] = DeleteCases[ticks[[1, 2, 2]], {_, "", __}];

ticks[[1, 2, 2, All, 3]] = ConstantArray[{0, 0},
   Length[ticks[[1, 2, 2, All, 3]]]];

ticks[[1, 2, 2, All, 2]] = Map[ToString,
    ticks[[1, 2, 2, All, 2]]] /. a_String :>
    If[StringTake[a, -1] == ".", a <> "0", a];

Plot[Sin[x], {x, 0, 6 Pi},
 AxesStyle -> {Black, Directive[Opacity[0], Red]},
 TicksStyle -> Directive[Opacity[1], Black],
 Ticks -> {Automatic, ticks[[1, 2, 2]]}]

enter image description here

要获得确切的原始刻度,您可以使用

Cases[Charting`FindTicks[{0, 1}, {0, 1}] @@ PlotRange[p][[2]], {_, _}]
  

{{ - 1, - 1.0},{ - 0.5,-0.5},{0,0},{0.5,0.5},{1,1.0}}

在此处实施:

p = Plot[Sin[x], {x, 0, 6 Pi},
   AxesStyle -> {Automatic, Opacity[0]},
   TicksStyle -> Opacity[1]];
ticks = AbsoluteOptions[p, Ticks];
onestyledtick = ticks[[1, 2, 2, 1]];
labels = Cases[Charting`FindTicks[{0, 1}, {0, 1}] @@
    PlotRange[p][[2]], {_, _}];
yticks = Map[Join[#, {{0, 0}},
     Take[onestyledtick, -1]] &, labels];
Plot[Sin[x], {x, 0, 6 Pi},
 AxesStyle -> {Automatic, Opacity[0]},
 TicksStyle -> Opacity[1],
 Ticks -> {Automatic, yticks}]