python – 使用matplotlib将点的散点添加到boxplot

我在this article看过这个精彩的箱形图(图2).

正如您所看到的,这是一个箱线图,其上叠加了黑点的散布:x索引黑点(按随机顺序),y是感兴趣的变量.我想用Matplotlib做类似的事情,但我不知道从哪里开始.到目前为止,我在网上发现的箱形图并不那么酷,看起来像这样:

matplotlib的文档:
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.boxplot

如何将箱形图着色:
https://github.com/jbmouret/matplotlib_for_papers#colored-boxes

解决方法:

您正在寻找的是一种向x轴添加抖动的方法.

here开始这样的事情:

bp = titanic.boxplot(column='age', by='pclass', grid=False)
for i in [1,2,3]:
    y = titanic.age[titanic.pclass==i].dropna()
    # Add some random "jitter" to the x-axis
    x = np.random.normal(i, 0.04, size=len(y))
    plot(x, y, 'r.', alpha=0.2)

引用链接:

One way to add additional information to a boxplot is to overlay the
actual data; this is generally most suitable with small- or
moderate-sized data series. When data are dense, a couple of tricks
used above help the visualization:

  1. reducing the alpha level to make the points partially transparent
  2. adding random “jitter” along the x-axis to avoid overstriking

代码如下所示:

import pylab as P
import numpy as np

# Define data
# Define numBoxes

P.figure()

bp = P.boxplot(data)

for i in range(numBoxes):
    y = data[i]
    x = np.random.normal(1+i, 0.04, size=len(y))
    P.plot(x, y, 'r.', alpha=0.2)

P.show()
上一篇:python – 多列的pandas box plot


下一篇:python boxplot的使用方法