我想使用python中来自matplotlib的pyplot创建一个具有2个箱形图的图形。
我正在使用虹膜数据集,该数据集提供三种类型的150种花朵的花瓣长度:Setosa,Versicolor和Virginica。 我想为Setosa的花瓣长度创建一个箱形图,为 花色的花瓣长度,全部在同一图上。
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from matplotlib import pyplot as plt
# From the iris dataset I create a dataframe which contains only the features
# of the flowers (sepal length, sepal width, petal length, petal width and the
# flower type.
data = load_iris()
X= data["data"]
y = data ["target"]
iris=pd.DataFrame(X)
iris["target"]=y
iris.columns=data['feature_names']+["target"]
iris["target"]=iris["target"].apply(lambda x:'Setosa' if x == 0 else 'Versicolor' if x == 1 else 'Virginica')
# I create my sub-dataframes which each contain the petal length of one type of flower
ar1 = np.array(iris.loc[lambda iris: iris["target"] == "Setosa", ["petal width (cm)"]])
ar2 = np.array(iris.loc[lambda iris: iris["target"] == "Versicolor", ["petal width (cm)"]])
# This works:
fig, ax = plt.subplots()
ax.boxplot(ar1)
plt.show()
# But this doesn't work:
data1 = [ar1, ar2]
fig, ax = plt.subplots()
ax.boxplot(data1)
plt.show()
我希望有2个箱型图。相反,我收到错误:“ ValueError:X必须具有2个或更少的尺寸”。但是,ar1和ar2具有2个维度,与上面提到的matplotlib示例完全相同。
非常感谢您的帮助,
答案 0 :(得分:2)
问题是
'3'
创建形状为'5'
的2D数组。因此,您可以做的就是先将数组展平,
ar1 = np.array(iris.loc[lambda iris: iris["target"] == "Setosa", ["petal width (cm)"]])