Как создать линейный график с сегментами разных цветов в Matplotlib?Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Как создать линейный график с сегментами разных цветов в Matplotlib?

Сообщение Anonymous »

Я пытаюсь использовать Matplotlib для создания сложного геометрического рисунка, который выглядит как прикрепленная фотография. Сначала я нарисую шесть больших черных кругов в круговом рисунке. Затем, чтобы создать эффект «вырез», я нарисую на них меньшие белые круги, которые немного смещены.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()


Подробнее здесь: https://stackoverflow.com/questions/796 ... matplotlib
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Как создать линейный график с сегментами разных цветов в Matplotlib?
    Anonymous » » в форуме Python
    0 Ответы
    4 Просмотры
    Последнее сообщение Anonymous
  • Как создать линейный график с сегментами разных цветов в Matplotlib?
    Anonymous » » в форуме Python
    0 Ответы
    2 Просмотры
    Последнее сообщение Anonymous
  • Как создать линейный график с сегментами разных цветов в Matplotlib?
    Anonymous » » в форуме Python
    0 Ответы
    4 Просмотры
    Последнее сообщение Anonymous
  • Как создать линейный график с сегментами разных цветов в Matplotlib?
    Anonymous » » в форуме Python
    0 Ответы
    2 Просмотры
    Последнее сообщение Anonymous
  • Как создать линейный график с сегментами разных цветов в Matplotlib?
    Anonymous » » в форуме Python
    0 Ответы
    4 Просмотры
    Последнее сообщение Anonymous

Вернуться в «Python»