[Python] Scatterplot chart labels overlap

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have a json file which has 500 data points. Below is an example of that the json file looks like.

    {
    "annotations": [
    {
    "userOptions": {
    "id": "Point A",
    "labels": [
    {
    "point": {"x": 1, "y": 2},
    "x": 1,
    "y": 2
    }
    ]
    }
    }
    ]
    }


    This is how i extract the data from the json file.

    # Initialize lists to collect all data
    text_labels = []
    points_x = []
    points_y = []
    original_ann_x = []
    original_ann_y = []

    # Extract data from JSON structure
    for annotation in data["annotations"]:
    user_options = annotation.get("userOptions", {})
    text_id = user_options.get("id")
    labels = user_options.get("labels", [])

    if labels and len(labels) > 0:
    label = labels[0]
    point = label.get("point")

    if point and text_id:
    # Collect the data
    text_labels.append(text_id)
    points_x.append(point.get("x"))
    points_y.append(point.get("y"))
    original_ann_x.append(label.get("x"))
    original_ann_y.append(label.get("y"))


    When there are alot of data points on a scatterplot chart that also has labels to each point, there comes with problem:

    1. There will be areas in the chart where there they share the same point, therefore the text labels will overlap each other.
    2. Points that are located very close to each other also would have the text labels overlap each other especially when the text labels length are very long.

    The outcome that I want to achieve:

    1. For text labels that share the same point (point[x] and point[y]), I want the text labels (labels[x] and labels[y]) to move, it can be ordered alphabetically but having it move away from one another and having some gap
    2. For text labels that overlap each other because their points are very close to each other and the text labels are really long, I want the text labels to move and having the next point of text label to move away from the point on the left.

    I have tried using adjustText and any libraries using Matplotlib but nothing works. Hope someone is able to assist on this. Much appreciated.

    FYI, the default font size is 14.

    Continue reading...

Compartilhe esta Página