-1

I have a question I'm trying to solve.

What is the average claim amount for gender and age categories and suitably represent the above using a facetted bar chart, one facet that represents fraudulent claims and the other for non-fraudulent claims.

How do I make a facet bar chart. Using matplotlib and seaborn.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 1
    Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). “Show/tell me how to solve this coding problem” [is off-topic for Stack Overflow](https://meta.stackoverflow.com/q/284236). We expect you to [make an honest attempt](https://meta.stackoverflow.com/q/261592), and then ask a *specific* question about your algorithm or technique. Stack Overflow is not intended to replace existing documentation and tutorials. – AlexK Sep 18 '22 at 06:48

1 Answers1

0

If we check the documentation for seaborn, we find that they have a function for faceted categorical plots.

You'll need to organize your data into a long format, and indicate the appropriate arguments into the catplot function, with kind being set to "bar".

Example:

import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib.pyplot import show
np.random.seed(42)

df = pd.DataFrame({
    "Gender": np.random.choice(["Male", "Female"], 100),
    "AgeGroup": np.random.choice(["18-40", "40-65", "65+"], 100),
    "IsFraud": np.random.choice([True, False], 100),
    "ClaimAmount": np.random.randint(100, 1000, 100),
})

g = sns.catplot(data=df, x="Gender", y="ClaimAmount", hue="AgeGroup", col="IsFraud", kind="bar")
show()