【问题标题】:How to choose the numbers shown on the axes of a plot in mathemetica?如何选择mathematica中绘图轴上显示的数字?
【发布时间】:2011-08-20 15:40:11
【问题描述】:

我已经检查了 Mathematica 文档中心中的所有示例和设置,但找不到任何有关如何选择将在轴上显示的数字的示例。

如何将绘图轴编号(如 2、4、6、..)更改为 PI、2PI、3PI、...?

【问题讨论】:

标签: wolfram-mathematica plot


【解决方案1】:

如果您希望 标签 Pi2 Pi 等位于 Pi、@,

霍华德已经给出了正确答案987654326@等

有时您可能希望在特定值处使用替代刻度标签,而无需重新调整数据。

文档中的其他示例之一显示了如何:

Plot[Sin[x], {x, 0, 10}, 
 Ticks -> {{{Pi, 180 \[Degree]}, {2 Pi, 360 \[Degree]}, {3 Pi, 
  540 \[Degree]}}, {-1, 1}}]

我有一套小型自定义函数,用于按照我想要的方式格式化 Ticks。如果您刚刚开始,这可能是太多的信息,但值得知道的是,您可以使用任何数字格式并根据需要将任何内容替换为您的刻度。

myTickGrid[min_, max_, seg_, units_String, len_?NumericQ, 
  opts : OptionsPattern[]] := 
 With[{adj = OptionValue[UnitLabelShift], bls = OptionValue[BottomLabelShift]}, 
Table[{i, 
If[i == max, 
 DisplayForm[AdjustmentBox[Style[units, LineSpacing -> {0, 12}], 
   BoxBaselineShift ->  If[StringCount[units, "\n"] > 0, adj + 2, adj]]], 
 If[i == min, 
  DisplayForm@AdjustmentBox[Switch[i, _Integer, 
     NumberForm[i, DigitBlock -> 3, 
      NumberSeparator -> "\[ThinSpace]"], _, N[i]], 
    BoxBaselineShift -> bls], 
  Switch[i, _Integer, NumberForm[i, DigitBlock -> 3, 
    NumberSeparator -> "\[ThinSpace]"], _, N[i]]]], {len, 0}}, {i,
 If[Head[seg] === List, Union[{min, max}, seg], Range[min, max, seg]]}]]

以及设置:

Options[myTickGrid] = {UnitLabelShift -> 1.3, BottomLabelShift -> 0}
SetOptions[myTickGrid, UnitLabelShift -> 1.3, BottomLabelShift -> 0]

例子:

Plot[Erfc[x], {x, -2, 2}, Frame -> True, 
 FrameTicks -> {myTickGrid[-2, 2, 1, "x", 0.02, UnitLabelShift -> 0], 
   myTickGrid[0, 2, {0.25, .5, 1, 1.8}, "Erfc(x)", 0.02]}]

【讨论】:

    【解决方案2】:

    你可以找到一个例子here:

    Ticks -> {{Pi, 2 Pi, 3 Pi}, {-1, 0, 1}}
    

    【讨论】:

    • 能不能自动生成PI,2PI,... 不指定总组数?谢谢~
    • 这将自动生成刻度,但您仍然需要指定最大 x 值:xmax = 10;绘图[Sin[x], {x, 0, xmax}, Ticks -> {Pi*Range[xmax/Pi], {-1, 1}}]
    【解决方案3】:

    Ticks 还接受一个函数,这将省去您手动列出点或每次都必须更改最大值的麻烦。这是一个例子:

    xTickFunc[min_, max_] := 
     Table[{i, i, 0.02}, {i, Ceiling[min/Pi] Pi, Floor[max/Pi] Pi, Pi}]
    Plot[Sinc[x], {x, -5 Pi, 5 Pi}, Ticks -> {xTickFunc, Automatic}, 
     PlotRange -> All]
    

    如果您想更灵活地自定义报价,您可能需要查看LevelScheme

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      相关资源
      最近更新 更多