【问题标题】:Python Scatter Plot - Overlapping dataPython 散点图 - 重叠数据
【发布时间】:2018-11-01 00:10:05
【问题描述】:

我有一个散点图,但很多时候值可以在同一个位置正确,我使用颜色和 alpha 来尝试纠正这种情况。但是,正如您所见,仍然很难区分某些区域的具体绘制内容。

有没有更简单的方法来解决这个问题?

谢谢

【问题讨论】:

  • 我会尝试为每个类别选择不同的标记,使用facecolor='none',以便它们只有轮廓。
  • 抱歉,markerfacecolor 可能是正确的关键字参数。我得检查一下。
  • 我使用的是ax1.plot_date,所以会查找类似的语法

标签: python matplotlib scatter


【解决方案1】:

您可以抖动这些值(添加一些随机噪声),这样它们就不会完全在同一个位置。

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

【讨论】:

  • @user3170725 我添加了一些散点图示例
【解决方案2】:

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

希望这会有所帮助。

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 找到匹配点的方式对点进行四舍五入)。

【讨论】: