How to choose the numbers shown on the axes of a plot in mathemetica?

女生的网名这么多〃 提交于 2019-11-29 08:04:55

You can find an example here:

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

Howard has already given the correct answer in the case where you want the labels Pi, 2 Pi etc to be at the values Pi, 2 Pi etc.

Sometimes you might want to use substitute tick labels at particular values, without rescaling data.

One of the other examples in the documentation shows how:

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

I have a suite of small custom functions for formatting Ticks the way I want them. This is probably too much information if you are just starting out, but it is worth knowing that you can use any number format and substitute anything into your ticks if desired.

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

And setting:

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

Example:

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

Ticks also accepts a function, which will save you the trouble of listing the points manually or having to change the max value each time. Here's an example:

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]

If you want more flexibility in customizing your ticks, you might want to look into LevelScheme.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!