본문 바로가기

Data Science/Data Visualization

[04. Area Chart] 004. Stream Graph

728x90

스트림 그래프는 중심 축 주위로 유기적인 유동 형태로 구성된 누적된 면적차트 유형이다.

 

import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import numpy as np

if __name__ == '__main__':
    def gaussian_smooth(x, y, sd):
        weights = np.array([stats.norm.pdf(x, m, sd) for m in x])
        weights = weights / weights.sum(1)

        return (weights * y).sum(1)

    flights = sns.load_dataset('flights')
    flights = flights.pivot('year', 'month', 'passengers')
    flights_smoothed = [gaussian_smooth(flights.index, y_, 1) for y_ in np.array(flights)]
    print(flights)
    print(flights_smoothed)

    plt.stackplot(flights.index, flights_smoothed, labels=flights.columns, baseline="sym")
    plt.axhline(0, color='black', ls='--')
    plt.title('Passengers')
    plt.legend(loc='upper left')
    plt.show()


결과 값
month  January  February  March  April  ...  September  October  November  December
year                                    ...                                        
1949       112       118    132    129  ...        136      119       104       118
1950       115       126    141    135  ...        158      133       114       140
1951       145       150    178    163  ...        184      162       146       166
1952       171       180    193    181  ...        209      191       172       194
1953       196       196    236    235  ...        237      211       180       201
1954       204       188    235    227  ...        259      229       203       229
1955       242       233    267    269  ...        312      274       237       278
1956       284       277    317    313  ...        355      306       271       306
1957       315       301    356    348  ...        404      347       305       336
1958       340       318    362    348  ...        404      359       310       337
1959       360       342    406    396  ...        463      407       362       405
1960       417       391    419    461  ...        508      461       390       432

[[101.95524068 128.35487671 130.24529853 128.27724606 128.55796442
  135.127506   142.53247207 142.57674559 134.3191589  125.10770011
  121.8372025  101.10858842]
 [106.23775649 135.31408337 137.87299677 135.16098937 136.80799684
  148.6563562  161.51569866 163.58596463 153.78684927 141.59813335
  138.37687409 117.08630095]
 [131.65399713 166.58042538 170.63187225 169.96416287 173.17489703
  181.98056585 191.32371022 191.97092443 181.92776519 171.44242669
  169.51711712 141.83213584]
 [155.08859544 192.95973773 191.4924507  187.64202232 194.42561973
  211.58046943 225.93308204 226.58814258 212.87775601 200.42312149
  198.81501919 166.17398333]
 [176.0374585  221.96383807 230.43489203 233.35147157 236.27924056
  245.76792072 257.13641459 256.24423536 238.62627412 219.58499017
  211.13772266 173.43554165]
 [178.45507707 220.69121442 227.02756733 232.02323386 243.47664162
  265.34667538 283.96477826 281.38582319 260.50286442 241.50077892
  236.23187752 196.39346804]
 [213.62097671 263.1187316  266.69363686 271.12772874 285.8919058
  314.9792911  339.3075534  336.28603446 311.49342908 288.07985699
  281.63860521 235.76225005]
 [251.79802771 311.043914   314.7909666  318.65215451 335.80109827
  367.91937258 392.42899919 386.99264682 355.50398877 325.56734057
  316.09668763 262.40480334]
 [277.92281139 343.4785982  349.67002196 355.45330045 375.96984638
  414.24126317 444.78223762 441.31762226 405.18854764 368.49594679
  353.77646807 290.70333608]
 [296.87740893 360.82652628 358.86680734 360.80308776 384.28944543
  429.73698421 467.98215873 464.18062987 419.05595182 376.83615379
  359.33006572 293.21478012]
 [317.05983521 391.46049294 399.07842817 408.20351838 433.42635601
  478.05736608 519.44456436 518.91211119 473.99322571 432.13853176
  419.98969879 348.23587139]
 [363.16401156 438.85173576 440.10919394 456.7878335  490.30217629
  539.99825647 581.35493684 573.16488851 522.50551265 476.82514046
456.96968532 373.96662872]]

 

 

728x90