本文介绍了使用matplotlib将散点图添加到箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在本文中看到了这个精美的箱线图(图2).

I have seen this wonderful boxplot in this article (Fig.2).

如您所见,这是一个箱形图,上面叠加了一些黑点:x索引黑点(以随机顺序),y是目标变量.我想使用Matplotlib做类似的事情,但是我不知道从哪里开始.到目前为止,我在网上发现的箱线图看上去不太酷,就像这样:

As you can see, this is a boxplot on which are superimposed a scatter of black points: x indexes the black points (in a random order), y is the variable of interest. I would like to do something similar using Matplotlib, but I have no idea where to start. So far, the boxplots which I have found online are way less cool and look like this:

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

Documentation of matplotlib:http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.boxplot

为箱形图着色的方法: https://github.com/jbmouret/matplotlib_for_papers#colored-boxes

Ways to colorize boxplots:https://github.com/jbmouret/matplotlib_for_papers#colored-boxes

推荐答案

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

What you're looking for is a way to add jitter to the x-axis.

类似的内容摘自此处:

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)

引用链接:

  1. 降低Alpha级别以使点部分透明
  2. 在x轴上添加随机抖动",以避免过度打击

代码如下:

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()

这篇关于使用matplotlib将散点图添加到箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:34