环境
pip install matplotlib
pip install numpy
代码
import matplotlib.pyplot as plt
import numpy as np
# 中国国旗的标准尺寸比例是 3:2
width, height = 300, 200 # 这里可以调整为任何满足3:2比例的尺寸
# 创建一个新图形
fig, ax = plt.subplots(figsize=(width/100, height/100), dpi=100)
ax.set_aspect('equal')
# 绘制红色背景
rect = plt.Rectangle((0, 0), width, height, color='red')
ax.add_patch(rect)
# 定义五角星的顶点
def star_polygon(radius, ratio, rotation=0):
angles = np.linspace(0, 2 * np.pi, 5, endpoint=False) + np.pi / 2 + np.radians(rotation)
outer_points = [(radius * np.cos(angle), radius * np.sin(angle)) for angle in angles]
inner_radius = radius * ratio
inner_angles = angles + (np.pi / 5)
inner_points = [(inner_radius * np.cos(angle), inner_radius * np.sin(angle)) for angle in inner_angles]
points = [None] * 10
points[::2] = outer_points
points[1::2] = inner_points
return points
# 大五角星
big_star_radius = 0.1875 * height # 大五角星半径占旗帜高度的比例
big_star_ratio = 0.38196601 # 内外半径比
big_star_x = 0.15 * width # 大五角星中心点X坐标
big_star_y = 0.72 * height # 大五角星中心点Y坐标
big_star_points = star_polygon(big_star_radius, big_star_ratio)
big_star_polygon = plt.Polygon([(x+big_star_x, y+big_star_y) for x, y in big_star_points], closed=True, fill=True, facecolor='yellow', edgecolor='none')
ax.add_patch(big_star_polygon)
# 小五角星
small_star_radius = 0.06 * height # 小五角星半径占旗帜高度的比例
small_star_ratio = 0.38196601 # 内外半径比
arc_radius = 80 # 半圆弧半径
angle_step = 180 / 6 # 每个小星之间的角度差
rotation_angle = angle_step # 每个小星的旋转角度
# 开始角度设置为向右偏移一点,使得小星星从大星星右侧开始分布
start_angle = -90 # 右侧开始的角度
for i in range(5):
if 0 == i:
continue;
angle = start_angle + i * angle_step
small_star_x = big_star_x + arc_radius * np.cos(np.radians(angle))
small_star_y = big_star_y + arc_radius * np.sin(np.radians(angle))
small_star_points = star_polygon(small_star_radius, small_star_ratio, rotation=rotation_angle * i)
small_star_polygon = plt.Polygon([(x+small_star_x, y+small_star_y) for x, y in small_star_points], closed=True, fill=True, facecolor='yellow', edgecolor='none')
ax.add_patch(small_star_polygon)
# 设置坐标轴范围
ax.set_xlim(0, width)
ax.set_ylim(0, height)
# 隐藏坐标轴
ax.axis('off')
# 显示或保存图形
#plt.show()
plt.savefig("five_star_flag.png", dpi=300, bbox_inches='tight', pad_inches=0)
敬礼🙋♂️
参考
【国庆阅兵全程视频】庆祝中华人民共和国成立70周年大会/阅兵式/群众游行特别报道】
国歌