Sahitya Reddy
Sahitya Reddy

Reputation: 21

Plotting bar charts side by side

Can I plot these two side bar charts side by side

import pandas as pd

a=[('Female','0'),
('Female','0'),
('Female','0'),
('Female','1'),
('Female','1'),
('Female','1'),
('Female','1'),
('Female','1'),
('Female','1'),
('Female','1'),
('Female','1'),
('Female','1'),
('Male','0'),
('Male','0'),
('Male','0'),
('Male','0'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1'),
('Male','1')]

df=pd.DataFrame(a,columns=['Gender','Placement_B'])

gender=pd.crosstab(df['Gender'], df['Placement_B'])

g_norm=pd.crosstab(df['Gender'], df['Placement_B'],margins=True, normalize='index')

gender.plot.bar(stacked=True)

g_norm.plot.bar(stacked=True)

Upvotes: 1

Views: 1496

Answers (2)

Heikura
Heikura

Reputation: 1089

I understood that you had problems with plotting the crosstab data. Here is a way to plot it.

import matplotlib.pyplot as plt

df=pd.DataFrame(a,columns=['Gender','Placement_B'])
gender=pd.crosstab(df['Gender'], df['Placement_B'])
g_norm=pd.crosstab(df['Gender'], df['Placement_B'],margins=True, normalize='index')

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.bar(gender.index.values, gender.iloc[:, 1])
ax1.bar(gender.index.values, gender.iloc[:, 0])
ax2.bar(g_norm.index.values, g_norm.iloc[:, 1])
ax2.bar(g_norm.index.values, g_norm.iloc[:, 0])

# Probably the way that is more relevant for this case

fig, (ax1, ax2) = plt.subplots(1, 2)
gender.iloc[:].plot.bar(stacked=True, ax=ax1)
g_norm.iloc[:].plot.bar(stacked=True, ax=ax2)

Upvotes: 1

Dmitri Savil'ev
Dmitri Savil'ev

Reputation: 11

This question does not specify what package you are using. If you use DataMelt program, you can plot bar charts or histograms side by side by using HPlot canvas, and navigate usibf cd() method. Here are several DataMelt example that show histograms side by side. If you do not like the histogram presentation, use HChart class as shown here

Upvotes: 0

Related Questions