Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I want to create one figure with 2 boxplots using pyplot from matplotlib in python.
I am working with the iris dataset which provides petal length for 150 flowers from three types: Setosa, Versicolor, Virginica.
I want to create one boxplot for the petal length of Setosa and one boxplot for
the petal length of Versicolor, all on the same figure.
I based my code on this tutorial:
https://matplotlib.org/gallery/pyplots/boxplot_demo_pyplot.html#sphx-glr-gallery-pyplots-boxplot-demo-pyplot-py
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()
I expect a figure with 2 boxplots. Instead I get the error: "ValueError: X must have 2 or fewer dimensions". However ar1 and ar2 have 2 dimensions exactly like shown in the matplotlib exemple mentioned above.
Thank you very much for your help,
The problem is that
ar1 = np.array(iris.loc[lambda iris: iris["target"] == "Setosa", ["petal width (cm)"]])
creates a 2D array of shape (50,1)
. So what you can do is flatten the array first,
data1 = [ar1.flatten(), ar2.flatten()]
fig, ax = plt.subplots()
ax.boxplot(data1)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.