并排绘制条形图

时间:2019-07-25 13:45:09

标签: python bar-chart visualization

我可以并排绘制这两个侧面条形图

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)

2 个答案:

答案 0 :(得分:0)

此问题未指定您使用的软件包。如果使用DataMelt program,则可以使用HPlot画布并排绘制条形图或直方图,并浏览usibf cd()方法。 several DataMelt example that show histograms并排放置。如果您不喜欢直方图表示,请使用HChart类作为shown here

答案 1 :(得分:0)

我知道您在绘制交叉表数据时遇到问题。这是一种绘制方法。

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)