LevelScheme 中的直方图

发布于 2024-10-27 03:32:23 字数 1535 浏览 1 评论 0原文

我刚刚开始使用 LevelScheme,并且在使直方图正确拟合方面遇到问题的数字。一个最小的非工作示例:

<<"LevelScheme`"
Figure[{FigurePanel[{{0, 1}, {0, 1}},
   LabB -> textit["x"], BufferB -> 2.5,
   LabL -> textit["p(x)"], BufferL -> 2.5,
   FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]},
   PlotRange -> {{-3, 3}, {0, 0.5}}], 
  RawGraphics[
   Histogram[RandomReal[NormalDistribution[], 1000], Automatic, 
    "ProbabilityDensity"]]},
 Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.1, 1.03}}]

输出看起来像这样

在此处输入图像描述

当它应该看起来像这样

在此处输入图像描述

基本上,Histogram 图形对象不遵循 FigurePanelPlotRange,而是遵循主 FigurePlotRange。当直方图绘图或类似命令替换时,不会发生此行为。因此,以下内容会生成一个干净的图

Figure[{FigurePanel[{{0, 1}, {0, 1}},
   LabB -> textit["x"], BufferB -> 2.5,
   LabL -> textit["p(x)"], BufferL -> 2.5,
   FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]},
   PlotRange -> {{-3, 3}, {0, 0.5}}], 
  RawGraphics[Plot[1/Sqrt[2 Pi] Exp[-x^2/2], {x, -4, 4}]]},
 Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.1, 1.03}}]

在此处输入图像描述

还有其他人遇到过此问题吗?或者,您有修复建议吗?

编辑

我想我应该在问题中添加一些绿色。我仍然有兴趣知道如何克服这个障碍。

I've just started using LevelScheme, and have issues with getting the histogram to fit correctly within the figure. A minimal non-working example:

<<"LevelScheme`"
Figure[{FigurePanel[{{0, 1}, {0, 1}},
   LabB -> textit["x"], BufferB -> 2.5,
   LabL -> textit["p(x)"], BufferL -> 2.5,
   FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]},
   PlotRange -> {{-3, 3}, {0, 0.5}}], 
  RawGraphics[
   Histogram[RandomReal[NormalDistribution[], 1000], Automatic, 
    "ProbabilityDensity"]]},
 Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.1, 1.03}}]

The output looks like this

enter image description here

when it should look like this

enter image description here

Basically, the Histogram graphics object doesn't obey the FigurePanel's PlotRange, but instead obeys the main Figure's PlotRange. This behaviour doesn't occur when the Histogram is replaced by a Plot or similar commands. So the following produces a clean plot

Figure[{FigurePanel[{{0, 1}, {0, 1}},
   LabB -> textit["x"], BufferB -> 2.5,
   LabL -> textit["p(x)"], BufferL -> 2.5,
   FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]},
   PlotRange -> {{-3, 3}, {0, 0.5}}], 
  RawGraphics[Plot[1/Sqrt[2 Pi] Exp[-x^2/2], {x, -4, 4}]]},
 Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.1, 1.03}}]

enter image description here

Has anyone else encountered this issue? Or, do you have suggestions for a fix?

EDIT

I thought I'd add some green to the question. I'm still interested in knowing how to overcome this hurdle.

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

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

发布评论

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

评论(3

早乙女 2024-11-03 03:32:23

好吧,我猜你不会太喜欢这个,但它是一种解决方法。

如果我给出 PerformanceGoal -> “速度” 作为直方图选项(而不是 PerformanceGoal ->“质量”),我禁用了交互行为,但是,通过一些小的调整,我得到以下结果:

<< "LevelScheme`"
Figure[{FigurePanel[{{0, 1}, {0, 1}}, LabB -> textit["x"], 
   BufferB -> 2.5, LabL -> textit["p(x)"], BufferL -> 2.5, 
   FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]}, 
   PlotRange -> {{-3, 3}, {0, 0.55}}], 
  RawGraphics[
   Histogram[RandomReal[NormalDistribution[], 1000], Automatic, 
    "ProbabilityDensity", PerformanceGoal -> "Speed"]]}, 
 Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.15, 1.1}}]

在此处输入图像描述

Well, I recon you won't like this one too much but it is a workaround of sorts.

If I give PerformanceGoal -> "Speed" as a Histogram option (rather than PerformanceGoal -> "Quality") I disable interactive behaviour but, with a few minor tweaks, I get the following:

<< "LevelScheme`"
Figure[{FigurePanel[{{0, 1}, {0, 1}}, LabB -> textit["x"], 
   BufferB -> 2.5, LabL -> textit["p(x)"], BufferL -> 2.5, 
   FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]}, 
   PlotRange -> {{-3, 3}, {0, 0.55}}], 
  RawGraphics[
   Histogram[RandomReal[NormalDistribution[], 1000], Automatic, 
    "ProbabilityDensity", PerformanceGoal -> "Speed"]]}, 
 Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.15, 1.1}}]

enter image description here

梦里的微风 2024-11-03 03:32:23

正如 Simon 在评论中提到的,您可以使用 LevelScheme 的 DataPlot 来绘制直方图。

<< "LevelScheme`"
histData[x_] := 
  Cases[x, RectangleBox[{bl_, _}, {br_, c_}] :> {{bl, br}, c}, 
   Infinity];
hist = histData[
   Histogram[RandomReal[NormalDistribution[], 1000], {-4, 4, 0.1}, 
    "ProbabilityDensity"]];
bins = hist[[All, 1, 1]]; counts = hist[[All, 2]];
data = Table[{bins[[i]], counts[[i]]}, {i, 1, Length@counts}];

Figure[{FigurePanel[{{0, 1}, {0, 1}}, LabB -> textit["x"], 
   BufferB -> 2.5, LabL -> textit["p(x)"], BufferL -> 2.5, 
   FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]}, 
   PlotRange -> {{-3, 3}, {0, 0.5}}],
  DataPlot[data, 
   DataLine -> {LineShape -> "Histogram", LineColor -> Darker@Blue}, 
   DataSymbol -> {SymbolSize -> 0.00001}],
  RawGraphics[
   Plot[1/Sqrt[2 Pi] Exp[-x^2/2], {x, -4, 4}, 
    PlotStyle -> {Red, Thick}]]
  }, Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.1, 1.03}}]

在此处输入图像描述

但是,我还没有像 Histogram 或 BarChart,如果这也是您的意图。

顺便说一句,函数 histData 类似于我很久以前在 mathematica 帮助论坛上看到的东西,它出现在我有用的函数工具包中。我不记得我在哪里或何时读到的,归功于它。然而,现在对我来说,这并不像当时那样具有神奇的功能。

As Simon mentioned in a comment, you can use LevelScheme's DataPlot to plot a histogram.

<< "LevelScheme`"
histData[x_] := 
  Cases[x, RectangleBox[{bl_, _}, {br_, c_}] :> {{bl, br}, c}, 
   Infinity];
hist = histData[
   Histogram[RandomReal[NormalDistribution[], 1000], {-4, 4, 0.1}, 
    "ProbabilityDensity"]];
bins = hist[[All, 1, 1]]; counts = hist[[All, 2]];
data = Table[{bins[[i]], counts[[i]]}, {i, 1, Length@counts}];

Figure[{FigurePanel[{{0, 1}, {0, 1}}, LabB -> textit["x"], 
   BufferB -> 2.5, LabL -> textit["p(x)"], BufferL -> 2.5, 
   FrameTicks -> {LinTicks[-4, 4], LinTicks[0, 1]}, 
   PlotRange -> {{-3, 3}, {0, 0.5}}],
  DataPlot[data, 
   DataLine -> {LineShape -> "Histogram", LineColor -> Darker@Blue}, 
   DataSymbol -> {SymbolSize -> 0.00001}],
  RawGraphics[
   Plot[1/Sqrt[2 Pi] Exp[-x^2/2], {x, -4, 4}, 
    PlotStyle -> {Red, Thick}]]
  }, Frame -> False, PlotRange -> {{-0.075, 1.1}, {-0.1, 1.03}}]

enter image description here

However, I haven't managed to get filled histogram bars like that produced by Histogram or BarChart, if that was also what you had intended.

BTW, the function histData is similar to something I saw on a mathematica help forum long ago, and it went in my useful functions toolkit. I don't remember where I read that or when, to credit it. However, it is not all that of a magic function now to me, as it was back then.

何处潇湘 2024-11-03 03:32:23

我知道问题出在哪里,但我没有立即解决的办法。 LevelScheme 的工作方式是转换 Graphics 对象,以便它们正确适合。为此,RawGraphics 使用 LegacyPackages\Graphics\Graphics.m 中的旧函数 TransformGraphics,该函数包含在 LegacyTransformGraphics.m< /code> 在 LevelScheme 包的 v. 3.51 中。查看 HistogramFullForm,您可以看到 TransformGraphics 对处理生成的对象类型一无所知。 Mark Caprio 正在接下来的几个月内对 LevelScheme 进行更新,因此可能会进行修复。同时,在将直方图提供给 RawGraphics 之前尝试使用 Rasterize,尽管它可能不会给您带来好的结果。

编辑
更新的版本可能看起来像这样,而不是使用旧版本的 TransformGraphics

TransformGraphics[ 
  (g:(Graphics | Graphics3D))[prims__, opts:OptionsPattern[], transform_]:=
    g[ GeometricTransformation[prims, transform], opts ]

当然,现在的技巧是提供一个可以实现 GeometricTransformation 的 transform 版本> 可以接受。尽管旧版 TransformGraphics 将函数(其第二个参数)直接应用于 g 中找到的点,因此使用上述代码无需进行任何其他更改即可工作。

要尝试它,请在 LevelScheme.nb(并重新生成 LevelScheme.m)或直接在 LevelScheme.m 中将 Needs["LevelScheme`LegacyTransformGraphics`"] 替换为上述代码。它可能无法完全工作,因为我看不到选项在哪里被替换,但这应该是一个开始。

I know what the problem is, but I don't have an immediate fix. The way LevelScheme works is that it transforms the Graphics objects so that they fit correctly. To do this, RawGraphics uses the legacy function TransformGraphics from LegacyPackages\Graphics\Graphics.m which is included in LegacyTransformGraphics.m in v. 3.51 of the LevelScheme packages. Looking at the FullForm of your Histogram, you can see that TransformGraphics knows nothing about dealing with the sort of objects produced. Mark Caprio is working on an update to LevelScheme over the next couple of months, so there may be a fix on the way. In the mean time, try using Rasterize before supply your histogram to RawGraphics, although it may not give you good results.

Edit:
Instead of using the legacy version of TransformGraphics, a more recent version might look like

TransformGraphics[ 
  (g:(Graphics | Graphics3D))[prims__, opts:OptionsPattern[], transform_]:=
    g[ GeometricTransformation[prims, transform], opts ]

Of course, the trick is now supplying a version of transform that GeometricTransformation can accept. Although, the legacy TransformGraphics, applies a function, its second argument, directly to the points found in g, so using the above code may work without any additional changes.

To try it, replace Needs["LevelScheme`LegacyTransformGraphics`"] with the above code in either LevelScheme.nb (and regenerate LevelScheme.m) or in LevelScheme.m directly. It may not work completely, as I don't see where the options are substituted, but it should be a start.

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