Embedded_game/模块一/画图/005_长方形.py
2025-01-02 12:48:11 +08:00

32 lines
942 B
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:24
import matplotlib.pyplot as plt
def draw_rectangle(color='blue'):
# 创建一个新的图形和轴,设置背景为透明
fig, ax = plt.subplots(figsize=(6, 4), dpi=80) # 修改了图形的大小以更好地适应长方形
fig.patch.set_alpha(0.0)
# 绘制一个长方形,颜色为指定颜色
# Rectangle参数为(x, y, width, height)
rectangle = plt.Rectangle((0.1, 0.1), 0.8, 0.4, fill=True, color=color)
ax.add_patch(rectangle)
# 设置坐标轴的范围
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# 移除坐标轴
ax.axis('off')
# 保存图像为PNG格式背景设为透明
plt.savefig('rectangle.png', transparent=True, bbox_inches='tight', pad_inches=0)
# 显示图像
plt.show()
# 调用函数并传入您选择的颜色
draw_rectangle(color='green') # 您可以替换为任何喜欢的颜色