python通过astral,matpotlin库实现计算日出日落并绘制折线图
from astral import LocationInfo #引用库
import datetime
from astral.sun import sun
import numpy as np
import matplotlib.pyplot as plt
#import re
换源加速下载pip -i http://pypi.douban.com/simple install Flask -- trusted-host pypi.douban.com
安装astral库文件pip install astral
安装matpotlib库pip install matplotlib
city = LocationInfo('Yuxi', 'China','Asia/Shanghai',24.350461, 102.543907) #定义城市信息
print((
f"Information for {city.name}/{city.region}\n"
f"Timezone: {city.timezone}\n"
f"Latitude: {city.latitude:.02f}; Longitude: {city.longitude:.02f}\n"
)) #输出城市信息
sunrise_a=[] #创建日出元组
sunset_a=[]
x=[]
i=1
print(' date sunrise-date sunaet-date')
while(i<32):
s = sun(city.observer, date=datetime.date(2021, 7, i),tzinfo=city.timezone)
#null_1=re.search('\d\d\S\d\d\S\d\d\S',s["sunrise"]) #使用正则匹配,类型出错
sunrise_a.append(s['sunrise'].strftime("%H:%M:%S")) #信息导入到元组内
#null_2=re.search('\d\d\s\d\d\S\d\d\S',s["sunset"])
sunset_a.append(s['sunset'].strftime("%H:%M:%S"))
print(( #输出列表
'-----------------------------------------------------\n'
f'|{s["sunrise"].strftime("%Y-%m-%d")}|sunise: {s["sunrise"].strftime("%H:%M:%S")} | '
f' sunset: {s["sunset"].strftime("%H:%M:%S")} |'
))
x.append(i)
i+=1
print('-----------------------------------------------------')
plt.plot(x, sunrise_a, marker='o', markersize=3) # 绘制折线图,添加数据点,设置点的大小
plt.plot(x, sunset_a, marker='o', markersize=3)
plt.savefig("easyplot.jpg") #保存折线图
plt.show() #展示折线图