我正在处理大量的观察,为了真正了解它,我想使用Plots.jl来做直方图,我的问题是如何在一个图中做多个直方图,因为这样做非常方便。我已经尝试过多种方法,但我对朱莉娅中不同的绘图源(plots.jl、pyplot、一站式、.)感到有点困惑。
我不知道张贴一些代码对我是否有帮助,因为这是一个更普遍的问题。但如果需要的话,我很乐意贴出来。
发布于 2017-02-14 12:14:20
一个例子就是这样做的:
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)我在Plots.jl回购中查找了“直方图”,找到了这一相关问题,并遵循了这个 链接的示例。
发布于 2017-02-14 12:15:14
使用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创建直方图的配方。
https://stackoverflow.com/questions/42204051
复制相似问题