2017-02-13 7 views
1

私は多数の観測に取り組んでおり、実際にそれを知るためにPlots.jlを使ってヒストグラムを作成したいと思っています。 私の質問は、1つのプロットで複数のヒストグラムをこれは本当に便利でしょう。私はすでに複数のことを試みましたが、私はジュリアのさまざまなプロットソース(plots.jl、pyplot、gadfly、...)とちょっと混乱しています。Juliaで複数のヒストグラムをPlots.jlを使用して

これはもっと一般的な質問ですので、私のコードの一部を投稿するのに役立つかどうか分かりません。しかし、必要に応じて投稿して嬉しいです。

+0

複数のヒストグラムがオーバーレイされているか、またはサブプロットを持つことを意味しますか? –

+0

誤解をおかけして申し訳ありません。複数のヒストグラムを重ねて表示したい。 – DoubleBass

答えて

2

だけでこれを行うこと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を発見し、例にthelinksを追いました。 Plots

+0

ありがとうございました。申し訳ありませんが、まだまだこれに新しいです;) – DoubleBass

1

は、1区で複数の系列を表示するには、2つの可能性があります。hcatは、

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

まず、あなたは、各列が別々のシリーズを構成するマトリックスを使用することができますベクトルを連結するために使用されます(コンマの代わりにスペースに注意してください)。

これはあなたが行行列使用して、個々のシリーズにオプションを適用することができる

histogram(randn(100,3)) 

と等価である:

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

(ここでも、スペースの代わりにカンマを注意)

第2に、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 

これは便利です。

編集:

フェリペ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に変更しました。