Embedded_game/模块一/画图/008_六边形.py
2025-01-02 12:48:11 +08:00

38 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding:utf-8 -*-
# @Author len
# @Create 2023/12/31 11:28
import matplotlib.pyplot as plt
import numpy as np
def draw_hexagon(color='blue', point = 5):
# 创建一个新的图形和轴,设置背景为透明
fig, ax = plt.subplots(figsize=(5, 5), dpi=80)
fig.patch.set_alpha(0.0)
# 定义六边形的六个顶点
hexagon_points = []
for i in range(point):
angle = 2 * np.pi * i / point
hexagon_points.append((0.5 + 0.4 * np.cos(angle), 0.5 + 0.4 * np.sin(angle)))
# 绘制六边形,颜色为指定颜色
hexagon = plt.Polygon(hexagon_points, closed=True, color=color)
ax.add_patch(hexagon)
# 设置坐标轴的范围
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# 移除坐标轴
ax.axis('off')
# 保存图像为PNG格式背景设为透明
plt.savefig('hexagon.png', transparent=True, bbox_inches='tight', pad_inches=0)
# 显示图像
plt.show()
# 调用函数并传入您选择的颜色
draw_hexagon(color='green', point=5) # 您可以替换为任何喜欢的颜色