DataFrames中的点框图

时间:2014-05-07 13:25:09

标签: python matplotlib pandas seaborn

Pandas中的数据帧有boxplot方法,但有没有办法在Pandas中创建 dot-boxplots ,或者用seaborn创建?

通过点框图,我的意思是一个箱图,显示图中的实际数据点(或它们的相关样本),例如像下面的例子(在R中获得)。

enter image description here

2 个答案:

答案 0 :(得分:16)

有关OP问题(与熊猫)相关的更精确答案:

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

data = pd.DataFrame({ "A":np.random.normal(0.8,0.2,20),
                      "B":np.random.normal(0.8,0.1,20), 
                      "C":np.random.normal(0.9,0.1,20)} )

data.boxplot()

for i,d in enumerate(data):
    y = data[d]
    x = np.random.normal(i+1, 0.04, len(y))
    plt.plot(x, y, mfc = ["orange","blue","yellow"][i], mec='k', ms=7, marker="o", linestyle="None")

plt.hlines(1,0,4,linestyle="--")

boxplot


旧版本(更通用):

使用matplotlib:

import numpy as np
import matplotlib.pyplot as plt

a = np.random.normal(0,2,1000)
b = np.random.normal(-2,7,100)
data = [a,b]

plt.boxplot(data) # Or you can use the boxplot from Pandas

for i in [1,2]:
    y = data[i-1]
    x = np.random.normal(i, 0.02, len(y))
    plt.plot(x, y, 'r.', alpha=0.2)

这给出了: dot-boxplot

灵感来自this tutorial

希望这有帮助!

答案 1 :(得分:12)

使用stripplot函数可以使用seaborn版本0.6(目前在github上的主分支中)。这是一个例子:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.boxplot(x="day", y="total_bill", data=tips)
sns.stripplot(x="day", y="total_bill", data=tips,
              size=4, jitter=True, edgecolor="gray")

enter image description here