繁体   English   中英

Python 散点图 - 重叠数据

[英]Python Scatter Plot - Overlapping data

我有一个散点图,但很多时候这些值可以在同一个地方,我使用颜色和 alpha 来尝试纠正这种情况。 但是,正如您所看到的,仍然很难区分某些区域中究竟绘制了什么。

在此处输入图片说明

有没有更万无一失的方法来解决这个问题?

谢谢

您可以抖动值(添加一些随机噪声),以使它们不会完全在同一地点。

import numpy as np
import matplotlib.pyplot as plt


x = np.random.randint(low=1,high=5,size=50)
y = np.random.randint(low=0,high=2,size=50)
jittered_y = y + 0.1 * np.random.rand(len(y)) -0.05
jittered_x = x + 0.1 * np.random.rand(len(x)) -0.05

plt.figure(figsize=(10,5))

plt.subplot(221)
plt.scatter(x,y,s=10,alpha=0.5)
plt.title('No Jitter')

plt.subplot(222)
plt.scatter(x,jittered_y,s=10,alpha=0.5)
plt.title('Y Jittered')

plt.subplot(223)
plt.scatter(jittered_x,y,s=10,alpha=0.5)
plt.title('X Jittered')

plt.subplot(224)
plt.scatter(jittered_x,jittered_y,s=10,alpha=0.5)
plt.title('Y and X Jittered')

plt.tight_layout();

在此处输入图片说明

如果您更喜欢确定性偏移,我创建了这个函数是为了解决一个类似的问题(这让我在这里寻求答案)。 请注意,此功能仅适用于完全重叠的点。 但是,您很可能可以舍入您的点并稍微修改此函数以适应“足够接近”的点。

希望这会有所帮助。

import numpy as np

def dodge_points(points, component_index, offset):
    """Dodge every point by a multiplicative offset (multiplier is based on frequency of appearance)

    Args:
        points (array-like (2D)): Array containing the points
        component_index (int): Index / column on which the offset will be applied 
        offset (float): Offset amount. Effective offset for each point is `index of appearance` * offset

    Returns:
        array-like (2D): Dodged points
    """

    # Extract uniques points so we can map an offset for each
    uniques, inv, counts = np.unique(
        points, return_inverse=True, return_counts=True, axis=0
    )

    for i, num_identical in enumerate(counts):
        # Prepare dodge values
        dodge_values = np.array([offset * i for i in range(num_identical)])
        # Find where the dodge values must be applied, in order
        points_loc = np.where(inv == i)[0]
        #Apply the dodge values
        points[points_loc, component_index] += dodge_values

    return points

这是之前和之后的示例。

前:

闪避前

后:

道奇之后

此方法仅适用于完全重叠的点(或者如果您愿意以np.unique查找匹配点的方式np.unique点)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM