Matplotlib:避免“分散/点/蜂群"中的重叠数据点;阴谋 [英] Matplotlib: avoiding overlapping datapoints in a "scatter/dot/beeswarm" plot

查看:45
本文介绍了Matplotlib:避免“分散/点/蜂群"中的重叠数据点;阴谋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 matplotlib 绘制点图时,我想偏移重叠的数据点以保持它们全部可见.例如,如果我有:

When drawing a dot plot using matplotlib, I would like to offset overlapping datapoints to keep them all visible. For example, if I have:

CategoryA: 0,0,3,0,5  
CategoryB: 5,10,5,5,10  

我想要每个 CategoryA 0"数据点并排设置,而不是在彼此的正上方,同时仍然与 CategoryB 不同.

I want each of the CategoryA "0" datapoints to be set side by side, rather than right on top of each other, while still remaining distinct from CategoryB.

在 R (ggplot2) 中有一个 jitter" 选项可以做到这一点.matplotlib 中是否有类似的选项,或者是否有另一种方法会导致类似的结果?

In R (ggplot2) there is a "jitter" option that does this. Is there a similar option in matplotlib, or is there another approach that would lead to a similar result?

澄清一下,R 中的 "beeswarm" 情节 基本上就是我的想法,pybeeswarm 是 matplotlib/Python 版本的早期但有用的开始.

to clarify, the "beeswarm" plot in R is essentially what I have in mind, and pybeeswarm is an early but useful start at a matplotlib/Python version.

添加 Seaborn 的 Swarmplot,在 0.7 版中引入,是我想要的一个很好的实现.

to add that Seaborn's Swarmplot, introduced in version 0.7, is an excellent implementation of what I wanted.

推荐答案

扩展@user2467675 的答案,我是这样做的:

Extending the answer by @user2467675, here’s how I did it:

def rand_jitter(arr):
    stdev = .01 * (max(arr) - min(arr))
    return arr + np.random.randn(len(arr)) * stdev

def jitter(x, y, s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs):
    return scatter(rand_jitter(x), rand_jitter(y), s=s, c=c, marker=marker, cmap=cmap, norm=norm, vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths, **kwargs)

stdev 变量确保抖动足以在不同的尺度上看到,但它假设轴的限制为零和最大值.

The stdev variable makes sure that the jitter is enough to be seen on different scales, but it assumes that the limits of the axes are zero and the max value.

然后您可以调用 jitter 而不是 scatter.

You can then call jitter instead of scatter.

这篇关于Matplotlib:避免“分散/点/蜂群"中的重叠数据点;阴谋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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