Mathematica 中没有垂直线的直方图

发布于 2024-12-26 15:43:40 字数 298 浏览 5 评论 0原文

我正在尝试制作没有垂直线的直方图。我想要一个看起来像函数的图。像这样: 在此处输入图像描述

之前曾对 R 提出过同样的问题 ( 没有垂直线的直方图)但我在 Mathematica 上。

我一直在研究 ChartStyle 选项,但没有成功。

I am trying to make an histogram without vertical lines. I'd like to have a plot which looks like a function. Like this:
enter image description here

The same question has been asked for R before ( histogram without vertical lines ) but I'm on Mathematica.

I have been looking into the ChartStyle options without success.

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

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

发布评论

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

评论(3

饮惑 2025-01-02 15:43:40

您还可以将 ListPlotInterpolationOrder->0 一起使用:

(* example data *)
data = RandomVariate[NormalDistribution[], 10^3];

hist = HistogramList[data, {.5}];

ListPlot[Transpose[{hist[[1]], ArrayPad[hist[[2]], {0, 1}, "Fixed"]}],
  InterpolationOrder -> 0, 
  Joined -> True, 
  AxesOrigin -> {hist[[1, 1]], 0}]

histogram

You could also use ListPlot with InterpolationOrder->0:

(* example data *)
data = RandomVariate[NormalDistribution[], 10^3];

hist = HistogramList[data, {.5}];

ListPlot[Transpose[{hist[[1]], ArrayPad[hist[[2]], {0, 1}, "Fixed"]}],
  InterpolationOrder -> 0, 
  Joined -> True, 
  AxesOrigin -> {hist[[1, 1]], 0}]

histogram

傻比既视感 2025-01-02 15:43:40

可能有多种方法可以通过摆弄 Histogram 中的 EdgeForm[]FaceForm[] 来做到这一点,但我发现滚动起来更简单每当我需要时,我就可以使用它。这是一个非常简单快速的示例:

histPlot[data_, bins_, color_: Blue] := Module[{
        countBorder = 
    Partition[Riffle[Riffle[#1, #1[[2 ;;]]], Riffle[#2, #2]], 2] & @@ 
     HistogramList[data, bins, "PDF"]
    },
    ListLinePlot[countBorder, PlotStyle -> color]
    ]

执行 histPlot[RandomReal[NormalDistribution[],{1000}],{-3,3,0.1}] 给出

在此处输入图像描述

然后,您可以扩展此选项以采用任何选项,而不仅仅是 "PDF",并且适用于以下情况您想自动选择垃圾箱。我不喜欢自动分箱,因为我喜欢控制分箱宽度和范围,以实现可预测性并轻松与其他图进行比较。

There probably are ways to do this by fiddling with EdgeForm[] and FaceForm[] in Histogram, but I've found it simpler to roll one on my own, whenever I need it. Here's a very simple and quick example:

histPlot[data_, bins_, color_: Blue] := Module[{
        countBorder = 
    Partition[Riffle[Riffle[#1, #1[[2 ;;]]], Riffle[#2, #2]], 2] & @@ 
     HistogramList[data, bins, "PDF"]
    },
    ListLinePlot[countBorder, PlotStyle -> color]
    ]

Doing histPlot[RandomReal[NormalDistribution[],{1000}],{-3,3,0.1}] gives

enter image description here

You can then extend this to take any option instead of just "PDF", and for cases when you'd like to choose the bins automatically. I dislike automatic binning, because I like to control my bin widths and extents for predictability and easy comparison against other plots.

雨轻弹 2025-01-02 15:43:40

以下是在版本 7 中使用后处理的两种方法:

rdat = RandomReal[NormalDistribution[0, 1], 200];
MapAt[
  {Blue,
   Line[# /. {{Rectangle[{x_, y_}, {X_, Y_}]}} :> Sequence[{x, Y}, {X, Y}]] } &,
  Histogram[rdat, PerformanceGoal -> "Speed"],
  {1, 2, 2, 2}
]

Mathematicagraphics

Cases[
  Histogram[rdat, PerformanceGoal -> "Speed"],
  Rectangle[{x_, y_}, {X_, Y_}] :> {{x, Y}, {X, Y}},
  \[Infinity]
];

Graphics[Line[Join @@ %], AspectRatio -> 1/GoldenRatio, Axes -> True]

Mathematica 图形

Here are two methods that work in version 7, using post-processing:

rdat = RandomReal[NormalDistribution[0, 1], 200];
MapAt[
  {Blue,
   Line[# /. {{Rectangle[{x_, y_}, {X_, Y_}]}} :> Sequence[{x, Y}, {X, Y}]] } &,
  Histogram[rdat, PerformanceGoal -> "Speed"],
  {1, 2, 2, 2}
]

Mathematica graphics

Cases[
  Histogram[rdat, PerformanceGoal -> "Speed"],
  Rectangle[{x_, y_}, {X_, Y_}] :> {{x, Y}, {X, Y}},
  \[Infinity]
];

Graphics[Line[Join @@ %], AspectRatio -> 1/GoldenRatio, Axes -> True]

Mathematica graphics

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