2017-08-09 29 views
0

在我所维护的分类广告网站中,我比较了接受大于中位数的分类的分类与在此准则中低于中值的分类。我称之为“高性能”分类广告。这里是展示这种简单的countplot:比例(带色调)的剧情类型

enter image description here

hue简直就是分类过的照片数量。

我的问题是 - 是否有在seaborn或matplotlib中显示比例而不是绝对计数的阴谋类型?

我基本上想要相同的计数绘图,但每个小节占特定类别中总项目的百分比。例如,请注意,在计数图中,使用3张照片进行分类的比例在high perf类别中占很大比例。收集这些信息需要一段时间。如果每个酒吧的高度代表它对其类别的贡献百分比,那么这将是一个更容易的比较。这就是为什么我正在寻找我正在寻找的东西。

一个说明性的例子会很棒。

回答

2

而不是试图找到一个特定的情况下绘图功能,会做你想要的,我建议考虑保持数据的生成和可视化分开。最后,你想要绘制一些值的条形图,所以这个想法应该是生成数据以便于绘制。

为此,您可以crosstab问题的两列,并将结果表中的每一行(或列)除以其总和。然后可以使用熊猫绘图包装器轻松绘制该表格。

import matplotlib.pyplot as plt 
import numpy as np; np.random.seed(42) 
import pandas as pd 
plt.rcParams["figure.figsize"] = 5.6, 7.0 

n = 100 
df = pd.DataFrame({"performance": np.random.choice([0,1], size=n, p=[0.7,0.3]), 
        "photo" : np.random.choice(range(4), size=n, p=[0.6,0.1,0.2,0.1]), 
        "someothervalue" : np.random.randn(n) }) 

fig, (ax,ax2, ax3) = plt.subplots(nrows=3) 

freq = pd.crosstab(df["performance"],df["photo"]) 
freq.plot(kind="bar", ax=ax) 

relative = freq.div(freq.sum(axis=1), axis=0) 
relative.plot(kind="bar", ax=ax2) 

relative = freq.div(freq.sum(axis=0), axis=1) 
relative.plot(kind="bar", ax=ax3) 


ax.set_title("countplot of absolute frequency") 
ax2.set_title("barplot of relative frequency by performance") 
ax3.set_title("barplot of relative frequency by photo") 
for a in [ax, ax2, ax3]: a.legend(title="Photo", loc=6, bbox_to_anchor=(1.02,0.5)) 
plt.subplots_adjust(right=0.8,hspace=0.6) 
plt.show() 

enter image description here