Final answer:
To make shapes of random color and size in Python, you can use the turtle module, the random module, and the matplotlib module. You can use the turtle module to create a graphic window and draw shapes of random color and size. The random module can be used to generate random values for the color and size attributes. The matplotlib module can also be used to create shapes of random color and size, primarily for data visualization.
Step-by-step explanation:
To make shapes of random color and size in Python, you can use the turtle module, the random module, and the matplotlib module. Here's how you can do it:
- Using the turtle module: You can use the turtle module to create a graphic window and draw shapes of random color and size. You can generate random values for the color and size attributes using the random module. For example:
- import turtle
- import random
- def draw_random_shape():
- turtle.color(random.choice(['red', 'blue', 'green']))
- turtle.shapesize(random.randint(1, 5))
- turtle.shape('circle')
- turtle.stamp()
- def main():
- turtle.speed(0)
- for _ in range(10):
- draw_random_shape()
- turtle.done()
- if __name__ == '__main__':
- main()
- Using the random module: You can use the random module to generate random values for the color and size attributes. You can then use these values to draw shapes using any graphics library or framework. Here's an example using the PIL library:
- from PIL import Image, ImageDraw
- import random
- def draw_random_shape():
- width, height = 400, 400
- image = Image.new('RGB', (width, height), 'white')
- draw = ImageDraw.Draw(image)
- color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
- size = random.randint(10, 100)
- shape = random.choice(['rectangle', 'circle', 'triangle'])
- if shape == 'rectangle':
- x1, y1 = random.randint(0, width - size), random.randint(0, height - size)
- x2, y2 = x1 + size, y1 + size
- draw.rectangle([(x1, y1), (x2, y2)], fill=color)
- elif shape == 'circle':
- x, y = random.randint(0, width), random.randint(0, height)
- draw.ellipse([(x - size, y - size), (x + size, y + size)], fill=color)
- elif shape == 'triangle':
- points = [(random.randint(0, width), random.randint(0, height)) for _ in range(3)]
- draw.polygon(points, fill=color)
- image.show()
- def main():
- for _ in range(10):
- draw_random_shape()
- if __name__ == '__main__':
- main()
- Using the matplotlib module: The matplotlib module is primarily used for data visualization, but you can also use it to create shapes of random color and size. Here's an example:
- import matplotlib.pyplot as plt
- import numpy as np
- import random
- def draw_random_shape():
- fig, ax = plt.subplots()
- color = (random.random(), random.random(), random.random())
- size = random.randint(10, 100)
- shape = random.choice(['circle', 'rectangle', 'triangle'])
- if shape == 'circle':
- circle = plt.Circle((0.5, 0.5), size, color=color)
- ax.add_patch(circle)
- elif shape == 'rectangle':
- rect = plt.Rectangle((0.2, 0.2), size, size, color=color)
- ax.add_patch(rect)
- elif shape == 'triangle':
- x, y = 0.5, 0.5
- points = np.array([[(x - size, y), (x + size, y), (x, y - size), (x - size, y)]])
- polygon = plt.Polygon(points, color=color)
- ax.add_patch(polygon)
- ax.set_aspect('equal')
- ax.set_xlim([0, 1])
- ax.set_ylim([0, 1])
- plt.axis('off')
- plt.show()
- def main():
- for _ in range(10):
- draw_random_shape()
- if __name__ == '__main__':
- main()