2

This is the code:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np 

plt.figure(figsize=(15,8))
ax=sns.histplot(x='type',data=df[df.isFraud == 1],stat='percent',multiple='dodge',common_norm=False,bins=25)
ax=sns.histplot(x='type',data=df[df.isFraud == 0],stat='percent',multiple='dodge',common_norm=False,bins=25)
ax.set_ylabel('Percentage in Each Type')
ax.set_xlabel('Transaction Amount')
plt.legend(labels=['Fraud', 'Not Fraud'])
plt.show()

This is the output Output(https://i.stack.imgur.com/2et9z.jpg)

I need different colours for 'fraud' and 'not fraud'

I tried adding the colour and palette parameter according to the documentation but it's not working out. I think its getting overridden by some other thing but i am not sure what it is.

I am working on a dataframe from kaggle. I want to show the correlation between the column [isFraud 0 or 1] and the types of transactions. I have edited the post and added a sample of the dataframe.

here is a screenshot of the dataframe dataframe

Maha
  • 21
  • 2
  • 1
    Could show some examples of your data? – JayPeerachai Dec 24 '22 at 12:39
  • @JayPeerachai I am working on a dataframe from kaggle. I want to show the correlation between the column [isFraud 0 or 1] and the types of transactions. I have edited the post and added a sample of the dataframe. – Maha Dec 24 '22 at 12:50
  • 1
    Have you tried the hue parameter? `ax = sns.histplot(x='type', hue='isFraud', data=df, stat='percent', multiple='dodge', common_norm=False, bins=25)` – amur Dec 24 '22 at 14:20
  • @amur Yes. Doesn't work – Maha Dec 24 '22 at 19:26

1 Answers1

1

I replicated your code with the tips dataset of seaborn. Try to use the color parameter of histplot the same as below.

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset('tips')

plt.figure(figsize=(15,8))
ax=sns.histplot(x='time',data=df[df.smoker == "Yes"],stat='percent',multiple='dodge',common_norm=False,bins=25, color='b')
ax=sns.histplot(x='time',data=df[df.smoker == "No"],stat='percent',multiple='dodge',common_norm=False,bins=25, color='r')
ax.set_ylabel('/')
ax.set_xlabel('Time')
plt.legend(labels=['Smoker', 'Not smoker'])
plt.show()

Here is the output.

CelKyo
  • 19
  • 4
  • Did it. But this is what it shows me https://imgur.com/17yzu9g.jpg I'm hoping for my histogram to look something like this. https://imgur.com/stN6s1v.jpg – Maha Dec 24 '22 at 13:38