• Python图形复刻——绘制母亲节花束


    各位小伙伴,好久不见,今天学习用Python绘制花束。

    有一种爱,不求回报,有一种情,无私奉献,这就是母爱。祝天下妈妈节日快乐,幸福永远!


    图形展示:

    代码展示:

    1. import matplotlib.pyplot as plt
    2. import numpy as np
    3. from matplotlib.colors import LinearSegmentedColormap as lsc
    4. from scipy.spatial.transform import Rotation as R
    5. # @author : slandarer
    6. # 生成花朵数据
    7. t1 = np.array(range(25))/24
    8. t2 = np.arange(0, 575.5, 0.5)/575*20*np.pi + 4*np.pi
    9. [xr, tr] = np.meshgrid(t1, t2)
    10. pr = (np.pi/2)*np.exp(-tr/(8*np.pi))
    11. ur = 1 - (1 - np.mod(3.6*tr, 2*np.pi)/np.pi)**4/2 + np.sin(15*tr)/150 + np.sin(15*tr)/150
    12. yr = 2*(xr**2 - xr)**2*np.sin(pr)
    13. rr = ur*(xr*np.sin(pr) + yr*np.cos(pr))
    14. hr = ur*(xr*np.cos(pr) - yr*np.sin(pr))
    15. tb = np.resize(np.linspace(0, 2, 151), (1,151))
    16. rb = np.resize(np.linspace(0, 1, 101), (101,1)) @ ((abs((1-np.mod(tb*5,2))))/2 + .3)/2.5
    17. xb = rb*np.cos(tb*np.pi)
    18. yb = rb*np.sin(tb*np.pi)
    19. hb = np.power(-np.cos(rb*1.2*np.pi)+1, .2)
    20. cL = np.array([[.33,.33,.69], [.68,.42,.63], [.78,.42,.57], [.96,.73,.44]])
    21. cL = np.array([[.02,.04,.39], [.02,.06,.69], [.01,.26,.99], [.17,.69,1]])
    22. cMpr = lsc.from_list('slandarer', cL)
    23. cMpb = lsc.from_list('slandarer', cL*.4 + .6)
    24. # 绕轴旋转数据点
    25. def rT(X, Y, Z, T):
    26. SZ = X.shape
    27. XYZ = np.hstack((X.reshape(-1, 1), Y.reshape(-1, 1), Z.reshape(-1, 1)))
    28. RMat = R.from_euler('xyz', T, degrees = True); XYZ = RMat.apply(XYZ)
    29. return XYZ[:,0].reshape(SZ), XYZ[:,1].reshape(SZ), XYZ[:,2].reshape(SZ)
    30. # 贝塞尔函数插值生成花杆并绘制
    31. def dS(X, Y, Z):
    32. MN = np.where(Z == np.min(Z)); M = MN[0][0]; N = MN[1][0]
    33. x1 = X[M, N]; y1 = Y[M, N]; z1 = Z[M, N] + .03
    34. x = np.array([x1, 0, (x1*np.cos(np.pi/3) - y1*np.sin(np.pi/3))/3]).reshape((3,1))
    35. y = np.array([y1, 0, (y1*np.cos(np.pi/3) + x1*np.sin(np.pi/3))/3]).reshape((3,1))
    36. z = np.array([z1, -.7, -1.5]).reshape((3,1))
    37. P = np.hstack((x,y,z)).T
    38. t = (np.array(range(50)) + 1)/50
    39. c1 = np.array([1, 2, 1]).reshape(3,1)
    40. c2 = np.power(t, np.array(range(3)).reshape(3,1))
    41. c3 = np.power(1 - t, np.array(range(2, -1, -1)).reshape(3,1))
    42. P = (P @ (c1*c2*c3))
    43. ax.plot(P[0], P[1], P[2], color = '#800080')
    44. # 创建figure窗口及axis坐标区域
    45. fig = plt.figure()
    46. ax = fig.add_subplot(111, projection='3d')
    47. # 绘制花束
    48. ax.plot_surface(rr*np.cos(tr), rr*np.sin(tr), hr + .35, rstride = 1, cstride = 1,
    49. facecolors = cMpr(hr), antialiased = True, shade = False)
    50. U, V, W = rT(rr*np.cos(tr), rr*np.sin(tr), hr + .35, [180/8, 0, 0]); V = V - .4
    51. for i in range(5):
    52. U, V, W = rT(U, V, W, [0, 0, 72])
    53. ax.plot_surface(U, V, W - .1, rstride = 1, cstride = 1,
    54. facecolors = cMpr(hr), antialiased = True, shade = False)
    55. dS(U, V, W - .1)
    56. u1, v1, w1=rT(xb, yb, hb/2.5 + .32, [180/9, 0, 0])
    57. v1 = v1 - 1.35
    58. u2, v2, w2 = rT(u1, v1, w1, [0, 0, 36])
    59. u3, v3, w3 = rT(u1, v1, w1, [0, 0, 24])
    60. u4, v4, w4 = rT(u3, v3, w3, [0, 0, 24])
    61. for i in range(5):
    62. u1, v1, w1 = rT(u1, v1, w1, [0, 0, 72])
    63. u2, v2, w2 = rT(u2, v2, w2, [0, 0, 72])
    64. u3, v3, w3 = rT(u3, v3, w3, [0, 0, 72])
    65. u4, v4, w4 = rT(u4, v4, w4, [0, 0, 72])
    66. ax.plot_surface(u1, v1, w1, rstride = 1, cstride = 1,
    67. facecolors = cMpb(hb), antialiased = True, shade = False)
    68. ax.plot_surface(u2, v2, w2, rstride = 1, cstride = 1,
    69. facecolors = cMpb(hb), antialiased = True, shade = False)
    70. ax.plot_surface(u3, v3, w3, rstride = 1, cstride = 1,
    71. facecolors = cMpb(hb), antialiased = True, shade = False)
    72. ax.plot_surface(u4, v4, w4, rstride = 1, cstride = 1,
    73. facecolors = cMpb(hb), antialiased = True, shade = False)
    74. dS(u1, v1, w1)
    75. dS(u2, v2, w2)
    76. dS(u3, v3, w3)
    77. dS(u4, v4, w4)
    78. ax.set_position((-.215, -.3, 1.43, 1.43))
    79. ax.set_box_aspect((1, 1, .8))
    80. ax.view_init(elev = 50, azim = 2)
    81. ax.axis('off')
    82. plt.show()

    参考文献:

    作者slandarer

    网址:https://mp.weixin.qq.com/s/a3QukoBQhoK46aY8ZinBrQ

  • 相关阅读:
    库存管理与策略模式
    Springboot整合HBase
    红黑树详解
    电商通用型商品中心设计
    【c++】Lambda表达式
    GZ038 物联网应用开发赛题第5套
    MiniNK WEB 选拔题 by F12
    一、Java语言简介
    Mask Transfiner for High-Quality Instance Segmentation
    工厂设计模式
  • 原文地址:https://blog.csdn.net/2202_75971130/article/details/138681392