0

PDFのプロットと2つの「ステップ」ヒストグラムを重ねた図があります。凡例には、1 本の線と 2 つの四角形が示されています。2 つのヒストグラムの凡例ハンドラーを変更して線も表示する方法はありますか? サンプルコードは次のとおりです。

x = np.linspace(0,2.5,1000)
plt.xlim((0,2.5))
plt.ylim((0,2.5))
plt.plot(x,rv.pdf(x),'k-.',label='pdf')
hist(series1,125,color='k',normed=1,histtype='step',label='hist 1',linestyle='dashed')
hist(series2,125,color='k',normed=1,histtype='step',label='hist 2')
plt.legend(loc='best')

rv は scipy.stats 確率変数です。

4

1 に答える 1

2

ヒストグラムと同じフォーマットで線を描画し、それらを使用して凡例を作成できます。

p1, = plt.plot(rv,'k-.',label='pdf')
plt.hist(series1,125,color='k',normed=1,histtype='step',label='hist 1',linestyle='dashed')
plt.hist(series2,125,color='k',normed=1,histtype='step',label='hist 2')

# plot lines that have the same formating as the histograms
p2, = plt.plot([0,0], label='hist 1',linestyle='dashed')
p3, = plt.plot([0,0],label='hist 2')

# create the legend
plt.legend([p1, p2, p3], ['pdf', 'hist 1', 'hist2'], loc='best')

# make the lines used in the legend invisible. 
p2.set_visible(False)
p3.set_visible(False)
于 2013-11-14T23:54:43.960 に答える