0

I am very new to Python and start to learn matplotlib recently. I have a dataset which have one 5 independent variables and 1 dependent variable. I want to create a stacked histogram which can show the variable distribution within independent variable.

Here is my raw data- Country, age, new_use, source and total_pages_visited are independent variables. Converted is dependent variable. I want to create separate stacked histogram for each independent variables. And in each histogram, it shows the distribution of variable and mark the different category of 'converted' in different color.

3
  • 2
    Welcome to SO, zywwill11. Could you please tell us what you have tried so far? Commented Feb 10, 2019 at 18:42
  • The notions of independent and dependent variable are confusing for a histogram, because a histogram shows the distribution (usually shown on the y axis) along a single dimension (usually shown on the xaxis). Maybe you are looking for a bar plot instead? Commented Feb 10, 2019 at 19:23
  • @ImportanceOfBeingErnest Thanks! You are right, I need the bar plot instead. Here is the expected chart- take 'Country' for example, the plot can show the frequency of each country in three bar (there are 3 different countries). And in each bar, it shows the distribution of "Converted-Yes' and "Converted-No'. How can I do this? Commented Feb 10, 2019 at 21:40

1 Answer 1

1

I think what you want is stacked bar plot and you can use pandas to achieve it.

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

df = pd.DataFrame(np.asarray([[1,2],[3,4],[5,6]]),index=['A','B','C'], columns=['Converted-Yes', 'Converted-No'])

df.plot.bar(stacked=True)
plt.show()

The above code generates the plot:

plot

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.