프로젝트
서울시 불법주정차 분포
import pymysql
import matplotlib.pyplot as plt
import seaborn as sns
import re
plt.rcParams['font.family'] = 'Malgun Gothic'
def extract_gu_from_address(address):
if address is None:
return None
match = re.search(r'\b(\w+구)\b', address)
if match:
return match.group(1)
else:
return None
def classify_district(gu):
target_districts = ['강남구', '강동구', '강북구', '강서구', '관악구', '광진구', '구로구', '금천구', '노원구',
'도봉구', '동대문구', '동작구', '마포구', '서대문구', '서초구', '성동구', '성북구',
'송파구', '양천구', '영등포구', '용산구', '은평구', '종로구', '중구', '중랑구']
if gu in target_districts:
return gu
else:
return '기타'
db = pymysql.connect(host='localhost',
port=3306,
user='root',
passwd='****',
db='programmers_da',
charset='utf8')
cursor = db.cursor()
Crackdown_address = []
Crackdown_latitude = []
Crackdown_longitude = []
Report_address = []
Report_latitude = []
Report_longitude = []
CCTV_address = []
CCTV_latitude = []
CCTV_longitude = []
sql = "SELECT 구주소, 위도, 경도 FROM Crackdown_location;"
cursor.execute(sql)
for line in cursor:
gu = extract_gu_from_address(line[0])
if gu is not None:
Crackdown_address.append(classify_district(gu))
Crackdown_latitude.append(line[1])
Crackdown_longitude.append(line[2])
sql = "SELECT 주소, 위도, 경도 FROM report_status;"
cursor.execute(sql)
for line in cursor:
gu = extract_gu_from_address(line[0])
if gu is not None:
Report_address.append(classify_district(gu))
Report_latitude.append(line[1])
Report_longitude.append(line[2])
sql = "SELECT 자치구, 위도, 경도 FROM seoul_cctv_location_information;"
cursor.execute(sql)
for line in cursor:
gu = extract_gu_from_address(line[0])
if gu is not None:
CCTV_address.append(classify_district(gu))
CCTV_latitude.append(line[1])
CCTV_longitude.append(line[2])
import pandas as pd
df = pd.DataFrame({
'Address': Crackdown_address + Report_address + CCTV_address,
'Latitude': Crackdown_latitude + Report_latitude + CCTV_latitude,
'Longitude': Crackdown_longitude + Report_longitude + CCTV_longitude,
'Source': ['단속'] * len(Crackdown_address) + ['신고'] * len(Report_address) + ['CCTV'] * len(CCTV_address)
})
plt.figure(figsize=(10, 6))
sns.scatterplot(x='Longitude', y='Latitude', hue='Address', style='Source', data=df, s=10, palette={'강남구': 'red', '강동구': 'blue',
'강북구': 'green', '강서구': 'purple',
'관악구': 'orange', '광진구': 'pink',
'구로구': 'brown', '금천구': 'olive',
'노원구': 'cyan', '도봉구': 'lime',
'동대문구': 'teal', '동작구': 'lavender',
'마포구': 'maroon', '서대문구': 'navy',
'서초구': 'yellow', '성동구': 'sienna',
'성북구': 'tan', '송파구': 'plum',
'양천구': 'darkcyan', '영등포구': 'darkred',
'용산구': 'darkorange', '은평구': 'darkblue',
'종로구': 'darkgreen', '중구': 'purple',
'중랑구': 'darkgray', '기타': 'gray'}, alpha=0.7)
plt.title('Scatter Plot of Latitude and Longitude')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.xlim(126.734086, 127.269311)
plt.ylim(37.413294, 37.715133)
plt.grid(True)
plt.show()
plt.figure(figsize=(10, 6))
sns.countplot(x='Address', data=df[df['Source'] != 'CCTV'], palette={'강남구': 'red', '강동구': 'blue',
'강북구': 'green', '강서구': 'purple',
'관악구': 'orange', '광진구': 'pink',
'구로구': 'brown', '금천구': 'olive',
'노원구': 'cyan', '도봉구': 'lime',
'동대문구': 'teal', '동작구': 'lavender',
'마포구': 'maroon', '서대문구': 'navy',
'서초구': 'yellow', '성동구': 'sienna',
'성북구': 'tan', '송파구': 'plum',
'양천구': 'darkcyan', '영등포구': 'darkred',
'용산구': 'darkorange', '은평구': 'darkblue',
'종로구': 'darkgreen', '중구': 'purple',
'중랑구': 'darkgray', '기타': 'gray'})
plt.title('지역별 불법주정차 및 신고 현황')
plt.xlabel('지역')
plt.ylabel('건수')
plt.grid(True)
plt.show()
db.close()