How to fill colors after drawing shapes in Python?

In Python, you can use the matplotlib library to plot graphs and fill colors. Here is a simple example demonstrating how to use matplotlib to draw a rectangle and fill it with color.

import matplotlib.pyplot as plt

# 创建一个图形对象和一个子图
fig, ax = plt.subplots()

# 绘制一个矩形
rect = plt.Rectangle((0.2, 0.2), 0.6, 0.6, color='blue')

# 将矩形添加到子图中
ax.add_patch(rect)

# 设置坐标轴范围
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)

# 显示图形
plt.show()

In this example, we first create a figure object and a subplot. Then, we draw a rectangle using the Rectangle function and specify its position, width, and height. Next, we add the rectangle to the subplot and set the range of the axes. Finally, we use the plt.show() function to display the figure.

To fill the rectangle with color, you can specify the color using the color parameter in the Rectangle function. In this example, we set the color of the rectangle to blue. You can also set different colors as needed.

bannerAds