24節氣動畫

      在〈24節氣動畫〉中尚無留言

底下是 24 節氣地球所在的位置

完整代碼如下

地球圖檔太陽圖檔

import matplotlib
import pylab as plt
import cv2
import numpy as np
from PIL import Image
matplotlib.rc('font', family='Microsoft JhengHei')
fig=plt.figure(figsize=(6,6))

season=['冬至','小寒','大寒','立春','雨水','驚蟄',
        '春分','清明','殼雨','立夏','小滿','芒種',
        '夏至','小暑','大暑','立秋','處暑','白露',
        '秋分','寒露','霜降','立冬','小雪','大雪',]
img=Image.open("earth.png")
img=img.transpose(Image.FLIP_TOP_BOTTOM)
img=img.rotate(23.5)
img_sun=Image.open("sun.png")

#地球軌道線
r=300
ax = fig.add_axes([0, 0, 1, 1])
#ax.axis("off")
ax.set_xlim(-380, 380)
ax.set_ylim(-380, 380)
a = np.linspace(0, 360, 360)
x = r * np.cos(np.pi * a / 180)
y = r * np.sin(np.pi * a / 180)
ax.plot(x, y, c="b", linewidth=0.5)

#24節氣線
for a in range(0, 360, 15):
    x1 = r * np.cos(np.pi * a / 180)
    y1 = r * np.sin(np.pi * a / 180)
    x2 = r * np.cos(np.pi * (a + 180) / 180)
    y2 = r * np.sin(np.pi * (a + 180) / 180)

    x = (r + 20) * np.cos(np.pi * a / 180)
    y = (r + 20) * np.sin(np.pi * a / 180)
    ax.plot([x1, x2], [y1, y2], c='k', linewidth=0.2)
    ax.text(x - 15, y - 10, season[int(a / 15)])

#45度線
for a in range(0, 360, 45):
    x1 = r * np.cos(np.pi * a / 180)
    y1 = r * np.sin(np.pi * a / 180)
    x2 = r * np.cos(np.pi * (a + 180) / 180)
    y2 = r * np.sin(np.pi * (a + 180) / 180)
    ax.plot([x1, x2], [y1, y2], c='b', linewidth=0.5)

#太陽圖示
sun = fig.add_axes([0.5-0.05, 0.5-0.05, 0.1, 0.1])
sun.axis("off")
sun.imshow(img_sun)

#地球動畫
earth=fig.add_axes([x+0.5-0.05,y+0.5-0.05,0.1,0.1])
a = np.sin(np.pi * 66.5 / 180) / np.cos(np.pi * 66.5 / 180)
earth_x = np.array([170, 460])
earth_y = (a * earth_x - 380).astype(int)
for trace in range(0,360):
    plt.delaxes(earth)
    x = 0.39 * np.cos(np.pi * trace / 180)
    y = 0.39 * np.sin(np.pi * trace / 180)
    earth=fig.add_axes([x+0.5-0.05,y+0.5-0.05,0.1,0.1])
    earth.set_ylim(0, 700)
    earth.axis("off")
    earth.imshow(img)
    earth.plot(earth_x, earth_y, c='r')

    plt.pause(0.01)
plt.show()

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *