2017-01-05 73 views
3

ヒストグラムを描き、密度プロットを重ねるためのという名前のPandasデータフレームがvという列を含んでいます。PythonのPandas/Matplotlibでのヒストグラムと密度の重ね合わせ

import pandas as pd 
import matplotlib.pyplot as plt 

Maxv=200 

plt.subplot(211) 
plt.hist(clean['v'],bins=40, range=(0, Maxv), color='g') 
plt.ylabel("Number") 

plt.subplot(212) 
ax=clean['v'].plot(kind='density') 
ax.set_xlim(0, Maxv) 
plt.xlabel("Orbital velocity (km/s)") 
ax.get_yaxis().set_visible(False) 

enter image description here

しかし、私はスーパーインポーズしようとすると、Yスケールが一致していない(と私は、Y軸の目盛りとラベルを失う):

私は他の下で1をプロットすることができますこの方法を知っています
yhist, xhist, _hist = plt.hist(clean['v'],bins=40, range=(0, Maxv), color='g') 
plt.ylabel("Number") 

ax=clean['v'].plot(kind='density') #I would like to insert here a normalization to max(yhist)/max(ax) 
ax.set_xlim(0, Maxv) 
plt.xlabel("Orbital velocity (km/s)") 
ax.get_yaxis().set_visible(False) 

enter image description here

いくつかのヒント? (追加の質問:どのように私は、密度の平滑化の幅を変更することができますか?)

+1

[この回答は役に立ちました](http://stackoverflow.com/a/39987117/2336654) – piRSquared

+0

はい、ありがとうございます。私は離れてxの範囲のサイズを設定し、2番目のy軸を隠す必要があります...ありがとう! – Matt

+0

['seaborn'](http://seaborn.pydata.org/tutorial/distributions.html#plotting-univariate-distributions)を使ってみませんか? – IanS

答えて

1

ありません、私はこれを試してみてください。

ax = clean.v.plot(kind='hist', bins=40, range=(0, Maxv)) 
clean.v.plot(kind='kde', ax=ax, secondary_y=True) 

しかし範囲の一部が動作しない、とTHERはまだ左のy軸上の問題

です

enter image description here

+1

プロット後に範囲を設定してみてください: 'ax.set(xlim = [0、Maxv])' – IanS

+0

左側のy軸については[この回答](http://stackoverflow.com/a/17877159/5276797)を参照してください。 – IanS

+0

@IanS:ありがとう、それは範囲で動作します。 :)私はY軸で成功していませんが、それほど重要ではないと思います。 – Matt

4

あなたのコードに基づいて、この作業をする必要があります:

ax = clean.v.plot(kind='hist', bins=40, normed=True) 
clean.v.plot(kind='kde', ax=ax, secondary_y=True) 
ax.set(xlim=[0, Maxv]) 

あなたはもうsecondary_yも必要ないかもしれません。

+0

非常にきれいです。私は実際に 'secondary_y'を取り除いた。唯一のことは、正規化されたyの中で(ヒストから)実際の数を失ったことですが、それも問題ありません。 – Matt

関連する問題