0

I have a data set with the columns age, sex, bmi, children, smoker, region, charges. I am trying to write a query to create a box plot that shows children on the x axis and BMI on the y axis but the children should be conditional if sex=='female'.

I am trying to show a boxplot of bmi for females based on how many children they have. I can't figure it out. I am close. I have a boxplot but it includes data for both men and women.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

d =\
{'Age': {0: 19, 1: 18, 2: 28, 3: 33, 4: 32, 5: 31, 6: 46, 7: 37, 8: 37, 9: 60},
 'Sex': {0: 'female', 1: 'male', 2: 'male', 3: 'male', 4: 'male', 5: 'female', 6: 'female', 7: 'female', 8: 'male', 9: 'female'},
 'BMI': {0: 27.9, 1: 33.77, 2: 33.0, 3: 22.705, 4: 28.88, 5: 25.74, 6: 33.44, 7: 27.74, 8: 29.83, 9: 25.84},
 'Children': {0: 0, 1: 1, 2: 3, 3: 0, 4: 0, 5: 0, 6: 1, 7: 3, 8: 2, 9: 0},
 'Smoker': {0: 'yes', 1: 'no', 2: 'no', 3: 'no', 4: 'no', 5: 'no', 6: 'no', 7: 'no', 8: 'no', 9: 'no'},
 'Region': {0: 'southwest', 1: 'southeast', 2: 'southeast', 3: 'northwest', 4: 'northwest', 5: 'southeast', 6: 'southeast', 7: 'northwest', 8: 'northeast', 9: 'northwest'},
 'Charges': {0: 16884.924, 1: 1725.5523, 2: 4449.462, 3: 21984.47061, 4: 3866.8552, 5: 3756.6216, 6: 8240.5896, 7: 7281.5056, 8: 6406.4107, 9: 28923.13692}}

data = pd.DataFrame(d)

plt.figure(figsize=(15,7))
sns.boxplot(data["Children"],data["BMI"],palette="viridis")
plt.show()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • You have two issues 1. filtering the data to a specific category (e.g. `'female'`) and 2. Incorrectly using positional and keyword arguments in the seaborn plot call. Also make sure pandas, matplotlib, and seaborn are up to date. For the Anaconda distribution `conda update --all` at the Anaconda prompt, or activate an environment if you're using a conda environment. Otherwise, update with pip. – Trenton McKinney Nov 07 '22 at 18:56

0 Answers0