Как нарисовать квадрат с пурпурной пунктирной линией и пятиугольными маркерами в Matplotlib Python

Чтобы нарисовать квадрат с пурпурной пунктирной линией и пятиугольными маркерами с помощью библиотеки Python Matplotlib, вы можете использовать следующие методы:

Метод 1: использование функции plt.plot()

import matplotlib.pyplot as plt
# Define the square coordinates
x = [0, 1, 1, 0, 0]
y = [0, 0, 1, 1, 0]
# Plot the square
plt.plot(x, y, linestyle=':', color='m', marker='p')
# Set the axis limits
plt.xlim(-0.5, 1.5)
plt.ylim(-0.5, 1.5)
# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Square with Magenta Dotted Line and Pentagon Markers')
# Show the plot
plt.show()

Метод 2: использование функции plt.plot()отдельно для каждой стороны

import matplotlib.pyplot as plt
# Define the square coordinates
x = [0, 1, 1, 0, 0]
y = [0, 0, 1, 1, 0]
# Plot the square sides
plt.plot(x[:2], y[:2], linestyle=':', color='m', marker='p')
plt.plot(x[1:3], y[1:3], linestyle=':', color='m', marker='p')
plt.plot(x[2:4], y[2:4], linestyle=':', color='m', marker='p')
plt.plot([x[3], x[0]], [y[3], y[0]], linestyle=':', color='m', marker='p')
# Set the axis limits
plt.xlim(-0.5, 1.5)
plt.ylim(-0.5, 1.5)
# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Square with Magenta Dotted Line and Pentagon Markers')
# Show the plot
plt.show()

Метод 3: использование функции plt.fill()

import matplotlib.pyplot as plt
# Define the square coordinates
x = [0, 1, 1, 0]
y = [0, 0, 1, 1]
# Plot the square
plt.fill(x, y, linestyle=':', color='m', marker='p', fill=False)
# Set the axis limits
plt.xlim(-0.5, 1.5)
plt.ylim(-0.5, 1.5)
# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Square with Magenta Dotted Line and Pentagon Markers')
# Show the plot
plt.show()