How to Plot Rose and Dahlia Flower with Python
In October 2022, MathWorks held a contest for their members to plot interesting and beautiful images. In that contest, I got to know Mr. Eric Ludlam, who was one of the participants (In my opinion, one of the best). As a small weekend challenge, I tried to convert his codes for plotting rose and dahlia flowers from MATLAB to Python using Matplotlib. Although, as he mentioned in his codes, the main idea of these codes comes from other people (here and here). In this short post, I have compared the codes and presented the Python code for plotting the rose and dahlia flowers.
Rose
Ludlam defined the parameters of plotting the rose flower in the first part of his code. For converting them to Python’s format, adding a comma to separate items in the list is necessary. Actually, using the comma in defining the linear vectors in MATLAB is optional, but we need that in Python. In addition, I used two popular and powerful modules in Python, NumPy and Matplotlib, for mathematical calculation and plotting. It is common to import the NumPy module as np, so I changed the name of one of the parameters in my Python code.
MATLAB:
Python:
You can change these parameters to reach different rose flowers.
In the mathematical part, NumPy walks like an angel along your Python code! Most of the functions in MATLAB have been defined in the NumPy module, and others can be easily replaced (trust Google), as you see below codes.
MATLAB:
Python:
And finally, we reach the plotting section. Honestly, I am not a big fan of Matplotlib, and I believe that when you have “Mat…” in your name, you have to be more similar to MATLAB! Anyway, with some more coding, I could reach the same result.
MATLAB:
Python:
And our beautiful red rose will be born by just running the code! Which one is more beautiful?
If you import colormap (cm) from Matplotlib, you can plot a rose with different colors, similar to the cover of this post. A list of defined colormaps exists in the Matplotlib documentation. Here’s the full Python code with a hot color map:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
ppr= 3.6 # petals per 1 revolution
nr = 30 # radius resolution
pr = 30 # petal resolution
pn = 40 # total number of petals
pf = 2 # How much the ends of the petals tilt up or down
ps = 5/4 # Separation between petals
ol = [.2, 1.02] # How open is it? [inner outer]
pt = (1/ppr) * np.pi * 2
theta = np.linspace(0, pn*pt,pn*pr+1)
R,THETA = np.meshgrid(np.linspace(0,1,nr), theta, indexing='ij')
x = 1-((ps*((1-np.mod(ppr*THETA, 2*np.pi)/np.pi)**2)-1/4)**2)/2
phi = (np.pi/2)*(np.linspace(ol[0],ol[1],pn*pr+1))**2
y = pf*(R**2)*((1.28*R-1)**2)*np.sin(phi)
R2 = (x*(R*np.sin(phi))) + (y*np.cos(phi))
X = R2*np.sin(THETA)
Y = R2*np.cos(THETA)
Z = x*((R*np.cos(phi))-(y*np.sin(phi)))
C = np.hypot(np.hypot(X,Y), Z)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
vals = np.zeros((256, 3))
vals[:, 0] = np.linspace(0,1,256)
newcmp = colors.ListedColormap(vals)
surf = ax.plot_surface(X, Y, Z, cmap = newcmp, rstride=1,
cstride=1, linewidth=0, antialiased=False)
ax.axis('equal')
ax.axis('off')
plt.show()
# Using a colormap from matplotlib reference
from matplotlib import cm
fig2 = plt.figure()
ax2 = fig2.add_subplot(projection='3d')
my_col = cm.hot_r(C)
surf = ax2.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors = my_col,
linewidth=0, antialiased=False)
ax2.axis('equal')
ax2.axis('off')
plt.show()
dahlia
The procedure for plotting the dahlia is the same, and I just put my final code and result.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
ppr= 12.6 # petals per 1 revolution
nr = 30 # radius resolution
pr = 10 # petal resolution
pn = 140 # total number of petals
pf = -1.2 # How much the ends of the petals tilt up or down
ol = [.11, 1.1] # How open is it? [inner outer]
pt = (1/ppr) * np.pi * 2
theta = np.linspace(0, pn*pt,pn*pr+1)
R,THETA = np.meshgrid(np.linspace(0,1,nr), theta, indexing='ij')
x = 1-((1-np.mod(ppr*THETA, 2*np.pi)/np.pi)**2)*0.7
phi = (np.pi/2)*((np.linspace(ol[0],ol[1],pn*pr+1))**2)
y = pf*(R**2)*((1.28*R-1)**2)*np.sin(phi)
R2 = (x*(R*np.sin(phi))) + (y*np.cos(phi))
X = R2*np.sin(THETA)
Y = R2*np.cos(THETA)
Z = x*((R*np.cos(phi))-(y*np.sin(phi)))
C = np.hypot(np.hypot(X,Y), Z)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
vals = np.zeros((256, 3))
vals[:, 0] = np.linspace(.6,1,256)
vals[:, 1] = np.linspace(.1,.8,256)
vals[:, 2] = np.linspace(.7,1,256)
newcmp = colors.ListedColormap(vals)
surf = ax.plot_surface(X, Y, Z, cmap = newcmp, rstride=1,
cstride=1, linewidth=0, antialiased=False)
ax.axis('equal')
ax.axis('off')
plt.show()
On Ludlam’s GitHub, you can see the main MATLAB code and more beautiful ideas.
Many Thanks for your supports.