【问题标题】:Best fit to a histogramplot Iris最适合直方图 Iris
【发布时间】:2021-07-21 19:19:23
【问题描述】:

我想为每个 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()

【问题讨论】:

  • “最佳拟合线”是什么意思?每个类别的平均值的垂直线?还是中位数?还是完全不同的东西?
  • @JohanC 我想要示例 1 中的行,他在其中绘制了直方图,但在一个图中,每个子图中都有图例

标签: python matplotlib seaborn scipy.stats iris-dataset


【解决方案1】:

使用 seaborn,您可以通过 sns.histplot(..., kde=True) 添加 kde curve。这是一个例子:

import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm
import pandas as pd

sns.set()
iris = sns.load_dataset('iris')
# make the 'species' column categorical to fix the order
iris['species'] = pd.Categorical(iris['species'])

fig, axs = plt.subplots(2, 2, figsize=(12, 6))
for col, ax in zip(iris.columns[:4], axs.flat):
    sns.histplot(data=iris, x=col, kde=True, hue='species', common_norm=False, legend=ax==axs[0,0], ax=ax)
plt.tight_layout()
plt.show()

sns.histplot()的一些参数:

  • common_norm=:当True(默认)根据属于每个色调值的行数缩小每条曲线(或直方图)时
  • stat=:“计数”之一, “频率”, “密度”, “概率”`;确定如何缩放 y 轴
  • multiple=“layer”:默认,都在同一个位置;“dodge”:条形相邻; “stack”:条形和/或曲线堆叠; “fill”: for each x-value the bars (and/or curves) are stacked to sum to 1`。

【讨论】:

  • 是否有可能在每个子图中得到“图例”?
  • 使用默认即可,或者将legend=ax==axs[0,0]改为legend=True。我删除了它们以避免重复信息使情节混乱。
猜你喜欢
  • 2023-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
相关资源
最近更新 更多