0

I am working with the iris dataset.

I would like to plot, for each one of the four variables, in the dataset a violin plot and a boxplot per row.

variables=['SepalLengthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm']

for var in variables:
    sns.boxplot(x = 'Species', y = var, data = iris)
    plt.show()
    sns.violinplot(x='Species', y=var, data= iris)
    plt.show()

sns.pairplot(iris, hue="Species")
plt.show()

I have used the code above and each figure appears in a row.

May somebody help me with the subplotting to order the figures and get a matrix 4(number of variables) x 2 (chars per variable)?

Thank in advance.

RuthC
  • 1,269
  • 1
  • 10
  • 21
  • 1
    Does this answer your question? [Subplot for seaborn boxplot](https://stackoverflow.com/questions/41384040/subplot-for-seaborn-boxplot) – Diziet Asahi May 07 '20 at 11:45

1 Answers1

0

You can set up the subplot grid with plt.subplots(), assigning its axes to a variable, which is then a two-dimensional array (the dimensions corresponding to the rows and columns of the subplot grid). Then you can reference the respective sub-axes in the seaborn plot calls with the ax parameter.

Note that I'm using the original variables for convenience, and you only need to call plt.show() once, at the end.

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

iris = sns.load_dataset('iris')
variables=['sepal_length', 'sepal_width', 'petal_length', 'petal_width']

fig, axes = plt.subplots(4, 2, figsize=[10, 15])

for row, var in enumerate(variables):
    sns.boxplot(x='species', y=var, data=iris, ax=axes[row, 0])
    sns.violinplot(x='species', y=var, data=iris, ax=axes[row, 1])

plt.show()

Iris data subplots

Arne
  • 9,990
  • 2
  • 18
  • 28