在 ListLogLogPlot 的刻度标签中强制使用科学计数法

发布于 2024-11-01 15:05:51 字数 445 浏览 1 评论 0原文

我正在尝试在 ListLogLogPlot 上自定义格式的刻度标签。通过搜索 Mathgroup 档案,看起来混乱刻度标签的常用方法是使用 AbsoluteOptions 提取它们,使用自定义格式运行替换规则,然后使用以下命令显式地将它们提供给绘图函数: Ticks->{...} 选项。但是,以下内容不适用于 ListLogLogPlot:

foo = ListLogLogPlot[Range[20]^3, Frame -> True];
ticks=(FrameTicks /. AbsoluteOptions[foo, FrameTicks])

有关如何处理此问题的任何想法?..


编辑:这里有很多好的答案!接受巫师先生的方法,因为它被证明是解决当前问题的最简洁的方法,但我认为自己将来会使用建议的其他方法。

I am trying to custom-format tick labels on a ListLogLogPlot. By searching Mathgroup archives, it looks like the usual way to mess with tick labels is to extract them using AbsoluteOptions, run a replacement rule with the custom format, and then explicitly feed them to the plotting function with the Ticks->{...} option. However, the following doesn't work for ListLogLogPlot:

foo = ListLogLogPlot[Range[20]^3, Frame -> True];
ticks=(FrameTicks /. AbsoluteOptions[foo, FrameTicks])

Any ideas on how to deal with this?..


Edit: lots of good answers here! Accepting Mr. Wizard's since it proved to be the most concise way to solve the immediate problem at hand, but I see myself using the other methods suggested in the future.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷月断魂刀 2024-11-08 15:05:51

人们可以使用替换来直接弄乱标签,绕过 Option/AbsoluteOptions

ListLogLogPlot[Range[20]^3, Frame -> True] /.
   (FrameTicks -> x_) :>
      (FrameTicks -> (x /. {a_?NumericQ, b_Integer, s___} :>
         {a, Superscript[10, Log10@b], s} ))

在此处输入图像描述


感谢Alexey Popkov,现在它已经得到了改进并且不再那么脆弱。

One can use replacements to mess with the labels directly, bypassing Option/AbsoluteOptions:

ListLogLogPlot[Range[20]^3, Frame -> True] /.
   (FrameTicks -> x_) :>
      (FrameTicks -> (x /. {a_?NumericQ, b_Integer, s___} :>
         {a, Superscript[10, Log10@b], s} ))

enter image description here


Thanks to Alexey Popkov this is now improved and less fragile.

失眠症患者 2024-11-08 15:05:51

和 Sjoerd 一样,我通常更喜欢编写一个动态计算刻度的函数:

PowerTicks[label_][min_, max_] := Block[{min10, max10},
  min10 = Floor[Log10[min]];
  max10 = Ceiling[Log10[max]];
  Join[Table[{10^i, 
     If[label, Superscript[10, i], Spacer[{0, 0}]]}, {i, min10, 
     max10}],
   Flatten[
    Table[{k 10^i, 
      Spacer[{0, 0}], {0.005, 0.`}, {Thickness[0.001`]}}, {i, min10, 
      max10}, {k, 9}], 1]]
  ]

ListLogLogPlot[Range[20]^3, Frame -> True, 
 FrameTicks -> {{PowerTicks[True], 
    PowerTicks[False]}, {PowerTicks[True], PowerTicks[False]}}]

Like Sjoerd, I generally prefer to write a function that computes the ticks on the fly:

PowerTicks[label_][min_, max_] := Block[{min10, max10},
  min10 = Floor[Log10[min]];
  max10 = Ceiling[Log10[max]];
  Join[Table[{10^i, 
     If[label, Superscript[10, i], Spacer[{0, 0}]]}, {i, min10, 
     max10}],
   Flatten[
    Table[{k 10^i, 
      Spacer[{0, 0}], {0.005, 0.`}, {Thickness[0.001`]}}, {i, min10, 
      max10}, {k, 9}], 1]]
  ]

ListLogLogPlot[Range[20]^3, Frame -> True, 
 FrameTicks -> {{PowerTicks[True], 
    PowerTicks[False]}, {PowerTicks[True], PowerTicks[False]}}]
静谧 2024-11-08 15:05:51

为了补充 Brett 的答案,请查看LevelScheme 中的 CustomTicks 包。它提供了两个用于生成刻度线“LinTicksLogTicks”的函数,每个函数都有许多格式选项。目前,它要求您自己执行对数,即

Plot[ {Log[10,Cosh[x]], Log[10, Sinh[x]]}, {x, 0, 4}, Frame -> True,
      FrameTicks -> { LinTicks, LogTicks, None, None }]

给出

LogPlot of cosh and sinh over 0 to 4

的列表数据,显然你必须将 Log[Base, data]ListPlot 一起使用,但它是可行的。我已向 Mark Caprio 提交了一个补丁,以便以下内容将执行与上面完全相同的操作

LogPlot[ {Cosh[x], Sinh[x]}, {x, 0, 4}, Frame -> True,
      FrameTicks -> { LinTicks, LogTicks, None, None }]

如果补丁被接受,则可以通过设置选项 PlotType 来访问旧形式的 LogTicks code> 为 Linear,默认为 Logarithmic。使用 CustomTicks 的优点是其他基础很容易

Exp[-x^ 的代码和绘图] 2] 从 0 到 3

,它会自动按照您想要的格式设置格式。

编辑:我还想指出,CustomTicks 本身是可加载的,与 LevelScheme 的其余部分分开。而且,由于它是一个小包,因此没有那么多额外的开销。

To complement Brett's answer, look at the CustomTicks package in LevelScheme. It provides two functions for generating tick marks 'LinTicksandLogTicks`, and each has a host of formatting options. Currently, it requires you to perform the logarithm yourself, i.e.

Plot[ {Log[10,Cosh[x]], Log[10, Sinh[x]]}, {x, 0, 4}, Frame -> True,
      FrameTicks -> { LinTicks, LogTicks, None, None }]

gives

LogPlot of cosh and sinh over 0 to 4

For a list of data, obviously you'd have to use Log[Base, data] with ListPlot, but it is workable. I have submitted a patch to Mark Caprio so that the following would do the exact same thing as above

LogPlot[ {Cosh[x], Sinh[x]}, {x, 0, 4}, Frame -> True,
      FrameTicks -> { LinTicks, LogTicks, None, None }]

If the patch is accepted the old form of LogTicks would be accessible by setting the option PlotType to Linear, Logarithmic is default. The advantage of using CustomTicks is that other bases are easy

code for and plot of Exp[-x^2] from 0 to 3

and it automatically formats it like you want.

Edit: I'd also like to point out, that CustomTicks is loadable by itself, separate from the rest of LevelScheme. And, as it is a small package, there isn't all that much additional overhead.

南烟 2024-11-08 15:05:51

对我来说看起来像一个错误。简单调用 AbsoluteOptions[foo] 会产生错误消息。不过,普通的旧 Options[foo] 工作正常。

Looks like a bug to me. Simple calling AbsoluteOptions[foo] yields error messages. Plain old Options[foo] works fine, though.

橘亓 2024-11-08 15:05:51

将包含此代码的电子邮件发送至[电子邮件受保护]。他们将能够告诉您是否有已知的更好的解决方法。

Send an email with this code in it to [email protected]. They will be able to tell you if there's a known better workaround.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文