---
# System prepended metadata

title: Python 期末報告

---

# Python 期末報告

```python=
! pip install gradio

# Colab 進行matplotlib繪圖時顯示繁體中文
# 下載台北思源黑體並命名taipei_sans_tc_beta.ttf，移至指定路徑
!wget -O TaipeiSansTCBeta-Regular.ttf https://drive.google.com
/ucid=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.font_manager import fontManager

# 改style要在改font之前
# plt.style.use('seaborn')
fontManager.addfont('TaipeiSansTCBeta-Regular.ttf')
mpl.rc('font', family='Taipei Sans TC Beta')

import numpy as np
import matplotlib.pyplot as plt
import gradio as gr

def data(a):

    a = int(a)
    b = 112-a

    x = [122.9, 123, 122.4, 121.5, 120.7, 121.5,123.1,122.3,
         121.8,121.1,121.9,121,121.4,121.3,122.1,122.7,119.5,
         123.3,121.95,121.05,119.5,119.8]
    y = [26.1, 26.5, 26.3, 25.1, 23.3, 23.2, 25.6,25.8,25.5,
         24.7,24.4,24.3,23.8,22.3,23,24.2,24.5,26.55,26,23.9,
         25.4,26.2]
    sizes = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    fig = plt.figure(figsize=(20,18))
    ax = fig.add_subplot(111)
    ax.set_xlim([119, 124])
    ax.set_ylim([21, 27])

    filepath = 'https://raw.githubusercontent.com/fsiofjsd/
                computer-class/main/midu.csv'
        
    row_index = b
    data = np.genfromtxt(
        filepath, delimiter=',', 
        skip_header=1,          
        dtype=float, 
        usecols=(row_index,), 
        unpack=True, 
        encoding='UTF-8')
    
    for i in range(22):
      if 0< data[i] <= 100:
        sizes[i] = 500
      elif 100< data[i] <=1000:
        sizes[i] = 2000
      elif 1000< data[i] <=3000:
        sizes[i] = 5000
      elif 3000< data[i] <=5000:
        sizes[i] = 7500
      elif 5000< data[i] <=7000:
        sizes[i] = 12000
      elif 7000< data[i] <=10000:
        sizes[i] = 15000

    for i in range(len(x)):
      plt.text(x[i], y[i], 
               data[i], 
               fontsize=20,
               horizontalalignment='center',                            
               verticalalignment='center')

    from skimage import io
    picture = 'https://raw.githubusercontent.com/fsiofjsd/
               computer-class/main/Taiwan.png'
        
    img = io.imread(picture)  


    ax.imshow(img, extent=[119, 124, 21, 27], alpha=0.5)
    ax.scatter(x, y, s=sizes,alpha=0.15)
    ax.set_title('台灣人口密度地圖')

    return fig

inputs = gr.Text(label="請輸入97至111間任意年份")
outputs = gr.Plot()
demo = gr.Interface(
    fn=data,
    inputs=inputs,
    outputs=outputs,
    title="台灣人口密度點陣圖",
    cache_examples=True,)
demo.launch()

```