使用matplotlib将散点图添加到箱线图 [英] Adding a scatter of points to a boxplot using matplotlib

查看:1190
本文介绍了使用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)

引用链接:

向箱图中添加其他信息的一种方法是覆盖 实际数据;这通常最适合小型或小型 中等大小的数据系列.当数据密集时,有一些技巧 上面使用的有助于可视化:

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. 降低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将散点图添加到箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆