応用?

散布図

import numpy as np
import matplotlib.pyplot as plt

prefectures = [
    {"name": "北海道", "population": 5381733, "area": 83424},
    {"name": "青森県", "population": 1279594, "area": 9646},
    {"name": "岩手県", "population": 1225684, "area": 15275},
    {"name": "宮城県", "population": 2333899, "area": 7282},
    {"name": "秋田県", "population": 1023119, "area": 11638},
    {"name": "山形県", "population": 1123891, "area": 9323},
    {"name": "福島県", "population": 1914039, "area": 13784},
    {"name": "茨城県", "population": 2916976, "area": 6097},
    {"name": "栃木県", "population": 1974255, "area": 6408},
    {"name": "群馬県", "population": 1973115, "area": 6362},
    {"name": "埼玉県", "population": 7266534, "area": 3798},
    {"name": "千葉県", "population": 6222666, "area": 5157},
    {"name": "東京都", "population": 13929286, "area": 2191},
    {"name": "神奈川県", "population": 9200166, "area": 2416},
    {"name": "新潟県", "population": 2304264, "area": 12584},
    {"name": "富山県", "population": 1066328, "area": 4248},
    {"name": "石川県", "population": 1154008, "area": 4186},
    {"name": "福井県", "population": 786740, "area": 4190},
    {"name": "山梨県", "population": 817620, "area": 4465},
    {"name": "長野県", "population": 2098804, "area": 13561},
    {"name": "岐阜県", "population": 2003145, "area": 10621},
    {"name": "静岡県", "population": 3700305, "area": 7777},
    {"name": "愛知県", "population": 7552873, "area": 5172},
    {"name": "三重県", "population": 1815865, "area": 5775},
    {"name": "滋賀県", "population": 1412916, "area": 4017},
    {"name": "京都府", "population": 2610353, "area": 4612},
    {"name": "大阪府", "population": 8839469, "area": 1905},
    {"name": "兵庫県", "population": 5534800, "area": 8401},
    {"name": "奈良県", "population": 1340078, "area": 3691},
    {"name": "和歌山県", "population": 963579, "area": 4725},
    {"name": "鳥取県", "population": 573441, "area": 3507},
    {"name": "島根県", "population": 694352, "area": 6708},
    {"name": "岡山県", "population": 1921525, "area": 7114},
    {"name": "広島県", "population": 2843990, "area": 8479},
    {"name": "山口県", "population": 1404729, "area": 6112},
    {"name": "徳島県", "population": 755733, "area": 4147},
    {"name": "香川県", "population": 976263, "area": 1877},
    {"name": "愛媛県", "population": 1385262, "area": 5676},
    {"name": "高知県", "population": 728276, "area": 7104},
    {"name": "福岡県", "population": 5101556, "area": 4986},
    {"name": "佐賀県", "population": 832832, "area": 2441},
    {"name": "長崎県", "population": 1377187, "area": 4132},
    {"name": "熊本県", "population": 1786170, "area": 7409},
    {"name": "大分県", "population": 1139646, "area": 6341},
    {"name": "宮崎県", "population": 1104069, "area": 7735},
    {"name": "鹿児島県", "population": 1648177, "area": 9187},
    {"name": "沖縄県", "population": 1453313, "area": 2281}
]

print('都道府県数', len(prefectures))

x=[]
y=[]
for pref in prefectures:
#    print(pref)
    x.append(pref['population']/10000)
    y.append(pref['area'])

ax=plt.subplot()

ax.scatter(x,y)

ax.set_title('scatter plot')
ax.set_xlabel('population')
ax.set_ylabel('area')
ax.set_xlim(0, 1500)                     # Y軸の表示範囲を指定
ax.set_ylim(0, 90000)                    # Y軸の表示範囲を指定
plt.grid(visible=True)

plt.show()
都道府県数 47

WebAPI: 天気予報

import requests
import json

# 気象庁予報データの取得:福岡県:40000, 東京都:130000, 大阪府:270000
# 参照:https://www.jma.go.jp/bosai/common/const/area.json

jma_url = "https://www.jma.go.jp/bosai/forecast/data/forecast/270000.json"
forecast_json = requests.get(jma_url).json()

area = 0 # 0:福岡県の最初の地域:福岡地方
forecast_office = forecast_json[0]["publishingOffice"]
forecast_major = forecast_json[0]["timeSeries"][0] # 参照データセットの選択
forecast_area = forecast_major["areas"][area]

print('==========')
print(forecast_office)
print(forecast_area["area"]["name"])

for period in range(0,len(forecast_major["timeDefines"])):
    forecast_date = forecast_major["timeDefines"][period]
    forecast_weather = forecast_area["weathers"][period]
    forecast_wind = forecast_area["winds"][period]
    if "waves" in forecast_area:
        forecast_waves = forecast_area["waves"][period]
    else:
        forecast_waves = None

    print('-----')
    print(forecast_date[:10])
    print("天気:"+forecast_weather.replace(' ', '')) # 全角スペースを削除して表示
    print(" 風:"+forecast_wind.replace(' ', ''))
    if forecast_waves != None:
        print("波浪:"+forecast_waves.replace(' ', ''))
        
==========
大阪管区気象台
大阪府
-----
2024-10-18
天気:くもり所により夕方雨
 風:北東の風
波浪:0.5メートル
-----
2024-10-19
天気:くもり昼過ぎから雨所により昼過ぎから夕方雷を伴い激しく降る
 風:南西の風後北の風やや強く海上では後北の風強く
波浪:0.5メートル後1.5メートル
-----
2024-10-20
天気:晴れ時々くもり
 風:北の風やや強く
波浪:1メートル