【问题标题】:Multiple histograms in Julia using Plots.jlJulia 中使用 Plots.jl 的多个直方图
【发布时间】:2017-02-13 12:27:13
【问题描述】:

我正在处理大量的观察结果,为了真正了解它,我想使用 Plots.jl 制作直方图 我的问题是如何在一个图中制作多个直方图,因为这真的很方便。我已经尝试了多种方法,但我对 julia 中不同的绘图源(plots.jl、pyplot、gadfly、...)有点困惑。

我不知道发布我的一些代码是否对我有帮助,因为这是一个更普遍的问题。但如果需要,我很乐意发布。

【问题讨论】:

  • 你的意思是要叠加多个直方图还是要有子图?
  • 很抱歉造成误会。我想叠加多个直方图。

标签: julia plots.jl


【解决方案1】:

an example 就是这样做的:

using Plots
pyplot()

n = 100
x1, x2 = rand(n), 3rand(n)

# see issue #186... this is the standard histogram call
# our goal is to use the same edges for both series
histogram(Any[x1, x2], line=(3,0.2,:green), fillcolor=[:red :black], fillalpha=0.2)

我在 the Plots.jl repo 中查找“直方图”,找到 this related issue 并跟随 the links 到示例。

【讨论】:

  • 非常感谢。对不起,对此还是很陌生;)
【解决方案2】:

使用Plots,在一个情节中显示多个系列有两种可能性:

首先,您可以使用矩阵,其中每一列构成一个单独的系列:

a, b, c = randn(100), randn(100), randn(100)
histogram([a b c])

这里,hcat 用于连接向量(注意空格而不是逗号)。

这相当于

histogram(randn(100,3))

您可以使用行矩阵将选项应用于各个系列:

histogram([a b c], label = ["a" "b" "c"])

(同样,请注意空格而不是逗号)

其次,您可以使用plot! 及其变体来更新之前的情节:

histogram(a)  # creates a new plot
histogram!(b) # updates the previous plot
histogram!(c) # updates the previous plot

或者,您可以指定要更新的绘图:

p = histogram(a) # creates a new plot p
histogram(b)     # creates an independent new plot
histogram!(p, c) # updates plot p

如果您有多个子图,这很有用。

编辑:

按照 Felipe Lema 的链接,您可以实现共享边缘的直方图配方:

using StatsBase
using PlotRecipes

function calcbins(a, bins::Integer)
    lo, hi = extrema(a)
    StatsBase.histrange(lo, hi, bins) # nice edges
end

calcbins(a, bins::AbstractVector) = bins

@userplot GroupHist 

@recipe function f(h::GroupHist; bins = 30)
    args = h.args
    length(args) == 1 || error("GroupHist should be given one argument")
    bins = calcbins(args[1], bins)
    seriestype := :bar
    bins, mapslices(col -> fit(Histogram, col, bins).weights, args[1], 1)
end

grouphist(randn(100,3))

编辑 2:

因为它更快,我将配方更改为使用StatsBase.fit 创建直方图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2017-01-09
    相关资源
    最近更新 更多