Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill color by groups in histogram using Matplotlib?

I know how to do this in R and have provided a code for it below. I want to know how can I do something similar to the below mentioned in Python Matplotlib or using any other library

library(ggplot2)
ggplot(dia[1:768,], aes(x = Glucose, fill = Outcome)) +
  geom_bar() +
  ggtitle("Glucose") +
  xlab("Glucose") +
  ylab("Total Count") +
  labs(fill = "Outcome")

like image 222
MetalRaptor11 Avatar asked Jun 17 '19 20:06

MetalRaptor11


1 Answers

Using pandas you can pivot the dataframe and directly plot it.

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

# dataframe with two columns in "long form"
g = np.array([np.random.normal(5, 10, 500),
              np.random.rayleigh(10, size=500)]).flatten()
df = pd.DataFrame({'Glucose': g, 'Outcome': np.repeat([0,1],500)})

# pivot and plot
df.pivot(columns="Outcome", values="Glucose").plot.hist(bins=100)

plt.show()

enter image description here

like image 68
ImportanceOfBeingErnest Avatar answered Sep 22 '22 10:09

ImportanceOfBeingErnest