[Python] How to create a line plot with segments of different colors in Matplotlib?

Discussão em 'Python' iniciado por Stack, Junho 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use Matplotlib to create a complex geometric pattern that looks like the attached photo.

    My current approach works by "faking" the curved, clipped shapes. I first draw six large black circles in a circular pattern. Then, to create the "cutout" effect, I draw smaller white circles on top of them, which are slightly offset.

    Here is the code I've wirtten (with ChatGPT comments):

    import matplotlib.pyplot as plt
    import numpy as np

    def create_circle(x, y, r):
    t = np.linspace(0, 2*np.pi, 100)
    circle_x = r * np.cos(t) + x
    circle_y = r * np.sin(t) + y
    return circle_x, circle_y

    plt.figure(figsize=(8, 8))
    ax = plt.gca()
    ax.set_aspect('equal')

    # Parameter settings
    R = 1.5 # Radius of each circle
    center_distance = 1.0 # Distance from the center
    num_circles = 6 # Number of circles

    # Arrange the 6 circles by rotating them 60 degrees at a time
    for i in range(num_circles):
    angle = i * (2 * np.pi / num_circles) # Rotate by 60 degrees each time

    # Center position of the circle
    center_x = center_distance * np.cos(angle)
    center_y = center_distance * np.sin(angle)

    # Create the coordinates for the circle
    circle_x, circle_y = create_circle(center_x, center_y, R)

    # Draw the black circle
    plt.fill(circle_x, circle_y, color='black')

    # Create white circles for cutting out (slightly smaller and slightly offset)
    for i in range(num_circles):
    angle = i * (2 * np.pi / num_circles)

    # Position of the white cutout (offset slightly inward)
    offset_distance = 0.4
    offset_angle = angle + np.pi * 0.3 # Rotate a little

    white_center_x = center_distance * np.cos(angle) + offset_distance * np.cos(offset_angle)
    white_center_y = center_distance * np.sin(angle) + offset_distance * np.sin(offset_angle)

    # White circle (for cutting out)
    white_circle_x, white_circle_y = create_circle(white_center_x, white_center_y, R * 0.5)

    # Use numpy.concatenate to combine multiple white circles
    if i == 0:
    all_white_x = white_circle_x
    all_white_y = white_circle_y
    else:
    all_white_x = np.concatenate([all_white_x, white_circle_x])
    all_white_y = np.concatenate([all_white_y, white_circle_y])

    plt.fill(white_circle_x, white_circle_y, color='white')

    # Add a small white circle in the center
    center_circle_x, center_circle_y = create_circle(0, 0, 0)
    # Adjust the coordinates of the central circle using numpy.append
    center_circle_x = np.append(center_circle_x, center_circle_x[0])
    center_circle_y = np.append(center_circle_y, center_circle_y[0])
    plt.fill(center_circle_x, center_circle_y, color='white')

    # Settings
    plt.xlim([-3, 3])
    plt.ylim([-3, 3])
    plt.axis('off')
    plt.tight_layout()
    plt.show()

    Continue reading...

Compartilhe esta Página