首页 > 解决方案 > 最适合直方图 Iris

问题描述

我想为每个特征直方图绘制每个虹膜类的最佳拟合线。我已经尝试了这些示例中的解决方案:12,但没有得到我想要的结果。

这就是直方图现在的样子,也是我希望它们看起来的样子,但每个类都有一条最佳拟合线。图像

这是我用来实现这一目标的代码。

def load_data(path):
    data = pd.read_csv(path, sep=',')
    return data 

#the reason I have imported it like this is because I needed it on this form for something else.
tot_data = load_data(Iris.csv)
setosa = load_data(path_setosa)    
versicolor = load_data(path_versicolour,)
virginica = load_data(path_virginica)
split_data_array = [setosa,versicolor,virginica]
fig, axes = plt.subplots(nrows= 2, ncols=2, sharex='col', sharey='row')#basis for subplots
colors= ['blue', 'red', 'green', 'black'] #colors for histogram



for i, ax in enumerate(axes.flat):#loop through every feature
    for label, color in zip(range(len(iris_names)), colors): #loop through every class
        _,bins,_ = ax.hist(data[label][features[i]], label=iris_names[label], color=color, stacked=True,alpha=0.5)
        b = np.arange(50)
        
    ax.set(xlabel='Measured [cm]', ylabel='Number of samples') #sets label name
    ax.label_outer() #makes the label only be on the outer part of the plots
    ax.legend(prop={'size': 7}) #change size of legend
    ax.set_title(f'Feature {i+1}: {features[i]}') #set title for each plot
    #ax.grid('on') #grid on or off
    
#plt.savefig('histogram_rap.png',dpi=200)

plt.show()

标签: pythonmatplotlibseabornscipy.statsiris-dataset

解决方案



推荐阅读