그럴 수도 있지

발전의 의지/파이썬

stackplot

OnlyMyStuff 2024. 6. 19. 09:15

 

 

작년 용역했던 연구과제의 리포트 원고를 적으면서

 

만들었던 stackplot

 

언젠간 써먹겠지

 

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import seaborn as sns
import matplotlib.ticker as ticker

# 컬러지정

col =sns.color_palette('Blues_r')

# 값 입력

normal = [16511610,5968567,1585225,0,0,0]
step1 = [6212709,7024670,3684951,587520,221130,80910]
step2 = [2958040,9729254,9091282,4409280,1258457,347624]
step3 = [351860,4792785,13315531,20865600,21219975,21300003]
step4 = [781,184725,573010,2390400,3994515,3702320]
step5 = [0,0,0,547200,1655924,2469143]
x = [2025,2030,2035,2040,2045,2050]
list_k = []
for i in [normal,step1,step2,step3,step4,step5]:
    i = [int(j / 1000) for j in i]
    list_k.append(i)

# label 지정

labels = ['Lv5','Lv4','Lv3','Lv2','Lv1','Normal']

# fig 설정

plt.style.use('seaborn-whitegrid')
plt.figure(figsize=(20,4.4))
plt.xlim(2025,2050)

# plot

plt.stackplot(x,list_k[5],list_k[4],list_k[3],list_k[2],list_k[1],list_k[0],labels=labels,colors=col,edgecolor='none',alpha=0.75)

# 제목

plt.title('Autonomous Vehicles',fontsize=30)

# legend order

handles, labels = plt.gca().get_legend_handles_labels()
order = [5,4,3,2,1,0]
plt.legend([handles[idx] for idx in order],[labels[idx] for idx in order],loc='best', fontsize=22,frameon=True,facecolor='white',framealpha=0.9,bbox_to_anchor=(0.999,0.94))

# 글씨크기

plt.tick_params(axis='both',which='major',labelsize=20)
plt.tick_params(axis='x',which='major',pad=20)
plt.ylabel('Number of vehilce(1,000veh)',fontsize=20)
plt.xlabel('Year',fontsize=20)

# , 표시

def comma_formatter(x, pos):
    return '{:,}'.format(int(x))
plt.gca().yaxis.set_major_formatter(ticker.FuncFormatter(comma_formatter))

# 출력

plt.show()



#%% 친환경차

# 값 입력

hybrid = [2198951,3993879,5561191,6682238,7398837,7828981]
electric = [1187881,3601678,5776335,7063424,7695186,7981096]
hydrogen = [305910,949913,1488720,1779049,1909280,1963522]
x = [2025,2030,2035,2040,2045,2050]
list_k = []
for i in [hydrogen, electric, hybrid]:
    i = [int(j / 1000) for j in i]
    list_k.append(i)

# label 지정

labels = ['Hybrid','Electric','Hydrogen']

# fig 설정

plt.style.use('seaborn-whitegrid')
plt.figure(figsize=(20,4.4))
plt.xlim(2025,2050)

# plot

plt.stackplot(x,list_k[2],list_k[1],list_k[0],labels=labels,colors=col,edgecolor='none',alpha=0.75)\

handles, labels = plt.gca().get_legend_handles_labels()
order = [2,1,0]
plt.legend([handles[idx] for idx in order],[labels[idx] for idx in order],loc='best', fontsize=22,frameon=True,facecolor='white',framealpha=0.9,bbox_to_anchor=(0.999,0.94))

# title

plt.title('Eco-friendly Vehicles', fontsize=30)

# legend

plt.legend(loc='best', fontsize=22,frameon=True,facecolor='white',framealpha=0.9,bbox_to_anchor=(0.999,0.7))

# 글씨 크기

plt.tick_params(axis='both',which='major',labelsize=20)
plt.tick_params(axis='x',which='major',pad=20)
plt.ylabel('Number of vehilce(1,000veh)',fontsize=20)
plt.xlabel('Year',fontsize=20)


def comma_formatter(x, pos):
    return '{:,}'.format(int(x))
plt.gca().yaxis.set_major_formatter(ticker.FuncFormatter(comma_formatter))


plt.show()

'발전의 의지 > 파이썬' 카테고리의 다른 글

SUMO 맛보기  (0) 2025.01.09
해볼만한 것  (0) 2024.08.13
청렴소통포탈 워드클라우드  (0) 2024.01.08
교통사고 등급 클러스터링  (1) 2023.12.21
빅데이터분석기사 취득  (0) 2023.12.18