Use Python to draw a rose and give it to her special

foreword

Hello, hello, three days and nine hours countdown to New Year's Eve

The last time I posted an article about fireworks, it seems that many people still need code

Let’s make a unique flower today~ (Give it to the one you like)

Effect

Without further ado, let's look at the effect first, after all, I can't do anything if it doesn't look good

Let me tell you in advance that I will try my best

insert image description here

the code

module

Source code. Click to receive

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.font_manager import FontProperties
  • Change the perspective of drawing the image, that is, the position of the camera, azim rotates along the z axis, and elev along the y axis

The text content in the middle can be customized and modified

python学习交流Q群:309488165 ### 源码领取
fig = plt.figure(figsize=(6, 8))
ax = fig.gca(projection='3d')
elev = 22
azim = 2.5
ax.view_init(elev, azim)  # 改变绘制图像的视角,即相机的位置,azim沿着z轴旋转,elev沿着y轴
font_set = FontProperties(fname=r"C:\Windows\Fonts\simhei.TTF", size=20)
ax.text(1, -0.8, 0, '"唯一的花送给我爱的宝贝"', fontproperties=font_set)
[x, t] = np.meshgrid(np.array(range(30)) / 28.0, np.arange(0, 575.5, 0.5) / 575 * 17 * np.pi - 2 * np.pi)
p = (np.pi / 2) * np.exp(-t / (8 * np.pi))
u = 1 - (1 - np.mod(3.6 * t, 2 * np.pi) / np.pi) ** 4 / 2
y = 2 * (x ** 2 - x) ** 2 * np.sin(p)
r = u * (x * np.sin(p) + y * np.cos(p))
h = 4 + u * (x * np.cos(p) - y * np.sin(p)) * 3
c = cm.get_cmap('Reds')
surf = ax.plot_surface(r * np.cos(t), r * np.sin(t), h, rstride=1, cstride=1, color='r',  # cmap= c,可改变花朵颜色
                       alpha=0.8, linewidth=0, antialiased=True)  # rstride和cstride为横竖方向的绘图采样步长,越小绘图越精细。

flower stalk

  • Divide the circle into 50 equal parts
u2 = np.linspace(0, 2 * np.pi, 50)
  • Divide the height 1 into 20 parts, the height range of the rose stem
h2 = np.linspace(0, 4, 20)  
  • xy value repeated 20 times
x2 = np.outer(0.05 * np.sin(u2), np.ones(len(h2)))  
y2 = np.outer(0.05 * np.cos(u2), np.ones(len(h2)))  
  • height corresponding to x, y
z2 = np.outer(np.ones(len(u2)), h2)  

full code

Source code. Click to receive

insert image description here

At last

Today's sharing ends here

By the way, I would like to recommend some Python video tutorials for you, hoping to help you:

Python zero-based teaching collection

If you have any questions about the article, or other questions about python, you can leave a message in the comment area or private message me. If you
think the article I shared is good, you can follow me or give the article a thumbs up (/≧▽≦)/

Guess you like

Origin blog.csdn.net/yxczsz/article/details/128473683