Julia中使用Plots.jl的多个直方图

Julia中使用Plots.jl的多个直方图,julia,plots.jl,Julia,Plots.jl,我正在处理大量的观察结果,为了真正了解它,我想使用Plots.jl绘制直方图 我的问题是如何在一个绘图中绘制多个直方图,因为这将非常方便。我已经尝试了多种方法,但我对julia中不同的绘图来源(plots.jl、pyplot、gadfly等)感到有点困惑 我不知道发布一些代码是否有帮助,因为这是一个更一般的问题。但是如果需要的话,我很乐意发布它。有这样一个功能: using Plots pyplot() n = 100 x1, x2 = rand(n), 3rand(n) # see iss

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

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

有这样一个功能:

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)

我在中查找“直方图”,找到并遵循示例。

对于
绘图
,有两种可能在一个绘图中显示多个系列:

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

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
创建直方图。

您的意思是要叠加多个直方图还是要有子图?很抱歉造成误解。我想有多个直方图叠加。非常感谢。很抱歉,对这一点我还是很陌生;)