最适合直方图 Iris
Best fit to a histogramplot Iris
我想为每个 Iris class 每个特征直方图绘制最佳拟合线。
我尝试了这些示例中的解决方案:1 and 2,但没有得到我想要的结果。
这是直方图现在的样子,也是我希望它们看起来的样子,但每个 class.
都有一条最佳拟合线
这是我用来实现此目的的代码。
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()
使用 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”
: bars and/or 曲线堆叠; “fill”: for each x-value the bars (and/or curves) are stacked to sum to
1`.
我想为每个 Iris class 每个特征直方图绘制最佳拟合线。 我尝试了这些示例中的解决方案:1 and 2,但没有得到我想要的结果。
这是直方图现在的样子,也是我希望它们看起来的样子,但每个 class.
这是我用来实现此目的的代码。
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()
使用 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”
: bars and/or 曲线堆叠;“fill”: for each x-value the bars (and/or curves) are stacked to sum to
1`.