Embedded_game/模块一/画图/006_菱形.py

31 lines
852 B
Python
Raw Normal View History

2025-01-02 12:48:11 +08:00
import matplotlib.pyplot as plt
def draw_diamond(color='blue'):
# 创建一个新的图形和轴,设置背景为透明
fig, ax = plt.subplots(figsize=(6, 4), dpi=80)
fig.patch.set_alpha(0.0)
# 定义菱形的四个顶点
diamond_points = [[0.5, 0.3], [0.8, 0.5], [0.5, 0.7], [0.2, 0.5]]
# 绘制菱形,颜色为指定颜色
diamond = plt.Polygon(diamond_points, closed=True, color=color)
ax.add_patch(diamond)
# 设置坐标轴的范围
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# 移除坐标轴
ax.axis('off')
# 保存图像为PNG格式背景设为透明
plt.savefig('diamond.png', transparent=True, bbox_inches='tight', pad_inches=0)
# 显示图像
plt.show()
# 调用函数并传入您选择的颜色
draw_diamond(color='red') # 您可以替换为任何喜欢的颜色