Phyllotaxis: draw flowers using mathematics
📖 Background
Mathematics can describe many phenomena of the natural world. Excellent examples are the shape of snowflakes, the fractal geometry of Romanesco broccoli, and how self-similarity rules the growth of plants. In this competition, you will design floral patterns using scatter plots!
You don't need any mathematical background to participate in this competition, but if you want to know more about phyllotaxis, you can check out this article on Wikipedia.
💾 The (sample) data
In this competition, you will create scatter plots in two dimensions, representing flower petals. You'll generate a dataset with two variables; let's call them x and y.
To get you started, Here are 44 points in a circle with radius 1. As every (x, y) point should be in the unit circle, it follows that x² + y² = 1. You can get this using the super famous Pythagorean trigonometric identity which states that sin²(θ) + cos²(θ) = 1 for any real number θ.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Create circle data to plot
t = np.arange(1, 45)
x = np.sin(t)
y = np.cos(t)
df = pd.DataFrame({'t': t, 'x': x, 'y': y})
# Make a scatter plot of points in a circle
plt.figure(figsize=(6, 6)) # Set plot size to 6x6 inches
plt.scatter(df['x'], df['y']) # Create scatter plot
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Plants can arrange their leaves in spirals. A spiral is a curve which starts from the origin and moves away from the origin as it revolves around it. In the plot above all our points are the same distance from the origin. A simple way to arrange them in a spiral is to multiply x and y by a factor which increases for each point. You could use t as that factor, as it meets these conditions, but you could do something more harmonious, e.g. the Golden Angle:
Golden Angle = π(3 − √5)
Both the Golden Ratio and the Golden Angle appear in unexpected places in nature. Apart of flower petals and plant leaves, you'll find them in seed heads, pine cones, sunflower seeds, shells, spiral galaxies, hurricanes, etc.
# Define the number of points
points = 500
# Define the Golden Angle
angle = np.pi * (3 - np.sqrt(5))
# Generate values
t = np.arange(1, points + 1) * angle
x = np.sin(t)
y = np.cos(t)
df = pd.DataFrame({'t': t, 'x': x, 'y': y})
# Make a scatter plot of points in a spiral
plt.figure(figsize=(6, 6))
plt.scatter(df['x'] * df['t'], df['y'] * df['t'])
plt.xlabel('x')
plt.ylabel('y')
plt.show()
💪 Challenge
Now it's your turn to design flowers 🌸 The code above gives you a good starting point, but the scatter plots still don't look like flowers! Explore different graphing options to bring your floral designs to life. Some ideas to get you started:
- Remove inessential plot attributes such as axes, legends, and labels.
- Add some color and transparency.
- Vary the angle of the spirals.
- Experiment with different shapes and sizes of the points.
Feel free to re-use the sample code, or come up with your own choice of libraries!
✅ Checklist before publishing into the competition
- Rename your workspace to make it descriptive of your work. N.B. you should leave the notebook name as notebook.ipynb.
- Remove redundant cells like the judging criteria, so the workbook is focused on your story.
- Include your best design at the top of the workspace.
- Check that all the cells run without error.
To create or draw our flower we need some step: 1- we define function plot_flower when we called draw our flower. 2- we need to define the variables of flower such as numbers of petales , length and width of petals. 3- Create flower petals use variables we are define such as numbers petals and points additional radius r for each point on the petal. 4- Create or calculate center of petals by scattering points within a small radius, randomized slightly to give a more organic look. Last visualization points use color cmap and so on.
import numpy as np
import matplotlib.pyplot as plt
def plot_flower():
num_patels = 5
points_patels = 500
patel_length = 0.6
patel_width = 0.5
# Create flower petals
theta = np.linspace(0, 2 * np.pi, num_patels * points_patels + 1)
r = (1 - patel_length) + patel_length * np.cos(num_patels * theta)
x = r * np.cos(theta)
y = r * np.sin(theta)
# Plot petals
plt.figure(figsize=(6, 6))
for i in range(num_patels):
start = i * points_patels
end = (i + 1) * points_patels
plt.fill(x[start:end], y[start:end], color='red', alpha=0.75)
# Create center of flower
center_theta = np.linspace(0, 2 * np.pi, 300)
center_r = 0.1 * np.random.rand(300)
center_x = center_r * np.cos(center_theta)
center_y = center_r * np.sin(center_theta)
# Plot center
plt.scatter(center_x, center_y, color='yellow', s=100, alpha=0.75)
# Adjustments
plt.axis('off')
plt.axis('equal')
plt.show()
plot_flower()import numpy as np
import matplotlib.pyplot as plt
def plot_flower():
num_patels = 5
points_patels = 500
patel_length = 0.6
patel_width = 0.5
# Create flower petals
theta = np.linspace(0, 2 * np.pi, num_patels * points_patels + 1)
r = (1 - patel_length) + patel_length * np.cos(num_patels * theta)
x = r * np.cos(theta)
y = r * np.sin(theta)
# Plot petals
plt.figure(figsize=(6, 6))
for i in range(num_patels):
start = i * points_patels
end = (i + 1) * points_patels
plt.fill(x[start:end], y[start:end], color='pink', alpha=0.75)
# Create center of flower
center_theta = np.linspace(0, 2 * np.pi, 300)
center_r = 0.1 * np.random.rand(300)
center_x = center_r * np.cos(center_theta)
center_y = center_r * np.sin(center_theta)
# Plot center
plt.scatter(center_x, center_y, color='yellow', s=100, alpha=0.75)
# Adjustments
plt.axis('off')
plt.axis('equal')
plt.show()
plot_flower()import matplotlib.pyplot as plt
def plot_flower():
num_patels = 8
points_patels = 500
patel_length = 0.8
patel_width = 0.6
# Create flower petals
theta = np.linspace(0, 2 * np.pi, num_patels * points_patels + 1)
r = (1 - patel_length) + patel_length * np.cos(num_patels * theta)
x = r * np.cos(theta)
y = r * np.sin(theta)
# normalize theta for color
normalize_theta = theta / (2 * np.pi)
# Create center of flower
center_theta = np.linspace(0, 2 * np.pi, 600)
center_r = 0.1 * np.random.rand(600)
center_x = center_r * np.cos(center_theta)
center_y = center_r * np.sin(center_theta)
# Plot petals
plt.figure(figsize=(6, 6))
plt.scatter(x, y, c=normalize_theta, cmap='plasma', s=10 * patel_width / patel_length, alpha=0.75)
plt.fill(x, y, color='purple')
# Plot center
plt.scatter(center_x, center_y, color='yellow', s=100, alpha=0.75)
# Adjustments
plt.axis('off')
plt.axis('equal')
plt.show()
plot_flower()import matplotlib.pyplot as plt # Corrected from 'plot' to 'plt' to match common convention
def plot_flower():
num_patels = 10
points_patels = 500
patel_length = 5
patel_width = 6
# Create flower petals
theta = np.linspace(0, 2 * np.pi, num_patels * points_patels + 1)
r = (1 - patel_length) + patel_length * np.cos(num_patels * theta)
x = r * np.cos(theta)
y = r * np.sin(theta)
# normalize theta for color
normalize_theta = theta / (2 * np.pi)
# Create center of flower
center_theta = np.linspace(0, 2 * np.pi, 300)
center_r = 0.1 * np.random.rand(300)
center_x = center_r * np.cos(center_theta)
center_y = center_r * np.sin(center_theta)
# Plot petals
plt.figure(figsize=(6, 6)) # Corrected from 'Figure' to 'figure' to correctly call the function
plt.scatter(x, y, c=normalize_theta, cmap='plasma', s=10 * patel_width / patel_length, alpha=0.75)
plt.fill(x, y, color='orange')
# Plot center
plt.scatter(center_x, center_y, color='purple', s=100, alpha=0.75) # Corrected alpha value to be less than 1
# Adjustments
plt.axis('off')
plt.axis('equal')
plt.show()
plot_flower()**In order to change the shapes of the flowers, the values of length and width must be changed and some changes in the center of flower. **
import matplotlib.pyplot as plt # Corrected from 'plot' to 'plt' to match common convention
import numpy as np # Added missing import statement for numpy
def plot_flower():
num_patels = 12
points_patels = 1000
patel_length = 0.5
patel_width = 4
# Create flower petals
theta = np.linspace(0, 2 * np.pi, num_patels * points_patels + 1)
r = (1 - patel_length) + patel_length * np.cos(num_patels * theta)
x = r * np.cos(theta)
y = r * np.sin(theta)
# normalize theta for color
normalize_theta = theta / (2 * np.pi)
# Create center of flower
center_theta = np.linspace(0, 2 * np.pi, 700)
center_r = 0.1 * np.random.rand(700)
center_x = center_r * np.cos(center_theta)
center_y = center_r * np.sin(center_theta)
# Plot petals
plt.figure(figsize=(6, 6))
plt.scatter(x, y, c=normalize_theta, cmap='plasma', s=10 * patel_width / patel_length, alpha=0.75)
plt.fill(x, y, color='maroon') # Corrected color name from 'Agua' to 'aqua'
# Plot center
plt.scatter(center_x, center_y, color='green', s=100, alpha=0.75)
# Adjustments
plt.axis('off')
plt.axis('equal')
plt.show()
plot_flower()