그래프를 그려요 - matplotlib

2019. 11. 25. 18:27Python programming

(1) matplotlib 를 활용해요
 - 공식 문서는 여기 참조 

 - 한 그래프를 여러 조각으로 나눠서 그래프 동시에 그릴 때
    ex) fig = plt.figure()
        ax1 = fig.add_subplot(2,1,1)
        ax2 = fig.add_subplot(2,1,2) 
        ax1.plot(unrate[0:12]['DATE'], unrate[0:12]['VALUE'])
        ax2.plot(unrate[12:24]['DATE'], unrate[12:24]['VALUE'])
        plt.show()

 

(2) 바그래프 그리기

예제 ) 
num_cols = ['RT_user_norm', 'Metacritic_user_nom', 'IMDB_norm', 'Fandango_Ratingvalue', 'Fandango_Stars']
bar_heights = norm_reviews[num_cols].iloc[0].values
bar_positions = arange(5) + 0.75
tick_positions = range(1,6)

fig, ax = plt.subplots()

ax.bar(bar_positions, bar_heights, 0.5)
ax.set_xticks(tick_positions)
ax.set_xticklabels(num_cols, rotation=90)

ax.set_xlabel('Rating Source')
ax.set_ylabel('Average Rating')
ax.set_title('Average User Rating For Avengers: Age of Ultron (2015)')

plt.show()

 

(3) scatter 차트 

 

(4) 히스토그램

fig, ax = plt.subplots()
ax.hist(norm_reviews['Fandango_Ratingvalue'], range=(0, 5))

plt.show()

 

 

(5) boxplot

fig, ax = plt.subplots()
ax.boxplot(norm_reviews['RT_user_norm'])
ax.set_xticklabels(['Rotten Tomatoes'])
ax.set_ylim(0, 5)
plt.show()

 

 

(6) 이왕이면 예쁘게 그려요 

예제) 심플하게 그려봅시다 



fig, ax = plt.subplots()
ax.plot(women_degrees['Year'], women_degrees['Biology'], c='blue', label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green', label='Men')
ax.tick_params(bottom="off", top="off", left="off", right="off")
# Start solution code.
for key,spine in ax.spines.items():
    spine.set_visible(False)
# End solution code.
ax.set_title('Percentage of Biology Degrees Awarded By Gender')
ax.legend(loc='upper right')
plt.show()