<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          用 Python 畫個(gè)月餅,給大家助助興

          共 6055字,需瀏覽 13分鐘

           ·

          2021-09-19 15:14

          中秋節(jié)到了,祝大家中秋節(jié)快樂!用 Python 畫個(gè)月餅,給大家助助興。

          效果圖

          依賴項(xiàng)

          pip install numpy
          pip install matplotlib

          源代碼

          需要下載字體文件 Alibaba-PuHuiTi-Medium.ttf,回復(fù)「月餅」獲取。


          from numpy import sin, cos, pi
          import matplotlib.pyplot as plt

          from matplotlib.patches import Arc, Circle, Wedge
          from matplotlib.font_manager import FontProperties

          length = 20
          R = 3**0.5*length/(3**0.5*cos(pi/12)-sin(pi/12))
          r = 2*sin(pi/12)*R/3**0.5

          arc1 = Arc([0, length], width=2*r, height=2*r,
                     angle=0, theta1=30, theta2=150, ec='orange', linewidth=4)  ##ec為線條顏色,可以自由替換

          arc2 = Arc([-length/2, length/2*3**0.5], width=2*r, height=2*r,
                     angle=0, theta1=60, theta2=180, ec='orange', linewidth=4)
          arc3 = Arc([-length/2*3**0.5, length/2], width=2*r, height=2*r,
                     angle=0, theta1=90, theta2=210, ec='orange', linewidth=4)
          arc4 = Arc([-length, 0], width=2*r, height=2*r, angle=0, theta1=120, theta2=240, ec='orange', linewidth=4)
          arc5 = Arc([-length/2*3**0.5, -length/2], width=2*r, height=2*r,
                     angle=0, theta1=150, theta2=270, ec='orange', linewidth=4)
          arc6 = Arc([-length/2, -length/2*3**0.5], width=2*r, height=2*r,
                     angle=0, theta1=180, theta2=300, ec='orange', linewidth=4)
          arc7 = Arc([0, -length], width=2*r, height=2*r, angle=0, theta1=210, theta2=330, ec='orange', linewidth=4)
          arc8 = Arc([length/2, -length/2*3**0.5], width=2*r, height=2*r,
                     angle=0, theta1=240, theta2=360, ec='orange', linewidth=4)
          arc9 = Arc([length/2*3**0.5, -length/2], width=2*r, height=2*r,
                     angle=0, theta1=270, theta2=390, ec='orange', linewidth=4)
          arc10 = Arc([length, 0], width=2*r, height=2*r, angle=0, theta1=300, theta2=420, ec='orange', linewidth=4)
          arc11 = Arc([length/2*3**0.5, length/2], width=2*r, height=2*r,
                      angle=0, theta1=330, theta2=450, ec='orange', linewidth=4)
          arc12 = Arc([length/2, length/2*3**0.5], width=2*r, height=2*r,
                      angle=0, theta1=0, theta2=120, ec='orange', linewidth=4)

          circle = Circle((0,0), R, ec='orange', fc='white', linewidth=4##ec為線條顏色,fc為填充顏色,可以自由替換

          wedge1 = Wedge([-22], R-590180,
                         ec='orange', fc=r'white', linewidth=4##ec為線條顏色,fc為填充顏色,可以自由替換

          wedge2 = Wedge([-55], R-1290180, ec='orange',
                         fc=r'white', linewidth=4)
          wedge3 = Wedge([-2-2], R-5180270, ec='orange',
                         fc=r'white', linewidth=4)
          wedge4 = Wedge([-5-5], R-12180270, ec='orange',
                         fc=r'white', linewidth=4)
          wedge5 = Wedge([2-2], R-5270360, ec='orange',
                         fc=r'white', linewidth=4)
          wedge6 = Wedge([5-5], R-12270360, ec='orange',
                         fc=r'white', linewidth=4)
          wedge7 = Wedge([22], R-5090, ec='orange',
                         fc=r'white', linewidth=4)
          wedge8 = Wedge([55], R-12090, ec='orange',
                         fc=r'white', linewidth=4)

          art_list = [arc1, arc2, arc3, arc4, arc5, arc6, arc7, arc8, arc9, arc10, arc11, arc12]
          art_list.extend([circle, wedge1, wedge2, wedge3, wedge4, wedge5, wedge6, wedge7, wedge8])
          fig, ax = plt.subplots(figsize=(88))
          ax.set_aspect('equal')
          for a in art_list:
              ax.add_patch(a)

          plt.axis('off')
          font_set = FontProperties(fname=r"Alibaba-PuHuiTi-Medium.ttf", size=12)  ##可以自由下載字體使用
          plt.text(-15.5-2.5'中秋快樂', bbox=dict(boxstyle='square', fc="w", ec='orange', linewidth=4), fontproperties=font_set,fontsize=50,
                   color='orange')  ##ec為線條顏色,color為字體顏色,可以自由替換
          plt.text(-25-33'但愿人長(zhǎng)久,千里共嬋娟', fontproperties=font_set, fontsize=30, color='#aa4a30')
          plt.ylim([-3535])
          plt.xlim([-3535])

          plt.show()

          最后

          畫圖其實(shí)就是各種數(shù)學(xué)計(jì)算、圓形曲線的計(jì)算。如果想看更多月餅的畫法,公眾號(hào)「Python七號(hào)」回復(fù)「月餅」。



          瀏覽 81
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  无码国精品一区二区免费蜜桃 | 国产一级免费网站 | 91乱伦精品| 青青操网站 | 插菊花中文网久久久 |