python不同种类(species)鸢尾花萼片和花瓣的分布情况(箱图或者提琴图)
时间: 2025-01-20 07:13:25 浏览: 47
在Python中,你可以使用数据可视化库如Matplotlib和Seaborn来创建箱线图或提琴图,展示鸢尾花(Iris dataset)不同物种(Setosa、Versicolor和Virginica)的萼片长度(Sepal Length)、萼片宽度(Sepal Width)、花瓣长度(Petal Length)和花瓣宽度(Petal Width)的数据分布。箱线图(Boxplot)是一种直观的方式,它展示了数据的四分位数以及异常值,而提琴图(Violin plot)则进一步显示了数据的概率密度。
例如,你可以按照以下步骤操作:
1. 导入所需库:
```python
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from sklearn.datasets import load_iris
```
2. 加载鸢尾花数据集:
```python
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['species'] = iris.target_names[iris.target]
```
3. 使用Seaborn绘制箱线图或提琴图:
```python
plt.figure(figsize=(10, 6))
sns.violinplot(x="species", y="petal_length", data=df, palette="pastel")
# 或者替换为 boxplot:
# sns.boxplot(x="species", y="petal_length", data=df, palette="pastel")
plt.title('Species Distribution of Iris Petal Length')
plt.xlabel('Species')
plt.ylabel('Petal Length (cm)')
plt.show()
```
这将分别展示每种鸢尾花物种的花瓣长度的分布情况。对于其他特征(如花瓣宽度、萼片长度和宽度),只需将`"petal_length"`替换为相应的特征名即可。
阅读全文
相关推荐


















