데이터분석/Python matplotlib

Python matplotlib 사용하기 2. 막대 그래프 [가로로 눕히기]

창조적생각 2021. 8. 10. 15:53

1. 막대 그래프를 사용할 때 x축에 글자 수가 많다면 서로 겹쳐져서 가시성이 크게 떨어지는 경우가 있습니다.

 

x값들이 서로 겹쳐 제대로 보이지 않는다.

 

이럴때는 x,y축을 바꿔서 바를 눕힌 가로바를 이용하면 좀 더 가시성을 회복할 수 있습니다.

 

기존은 세로 바 그래프를 만들기 위해서 plt.bar()를 사용했다면 가로로 눕히기 위해서는 plt.barh()를 사용해 주시면 됩니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from matplotlib import pyplot as plt
 
language = ['JavaScript''HTML/CSS''SQL''Python''Java''Bash/Shell/PowerShell''C#''PHP''C++''TypeScript''C''Other(s):''Ruby''Go''Assembly']
popularity = [59218554654754436442359163199127097230302052418523180177920733172015833]
 
plt.barh(language, popularity)
 
plt.title("Most popular Language")
 
plt.ylabel("Number of people who use")
 
plt.tight_layout
 
plt.show()
cs

 

[실행결과]

 

 

만약 가장 큰 값부터 아래로 내리고 싶다면 먼저 리스트를 거꾸로 뒤집는 list이름.reverse()를 사용해주시면 됩니다.

 

주의하실 점은 reverse를 실행해 줘야 하기때문에 ()를 반드시 붙여주셔야합니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from matplotlib import pyplot as plt
 
language = ['JavaScript''HTML/CSS''SQL''Python''Java''Bash/Shell/PowerShell''C#''PHP''C++''TypeScript''C''Other(s):''Ruby''Go''Assembly']
popularity = [59218554654754436442359163199127097230302052418523180177920733172015833]
 
language.reverse()
popularity.reverse()
 
plt.barh(language, popularity)
 
plt.title("Most popular Language")
 
plt.ylabel("Number of people who use")
 
plt.tight_layout
 
plt.show()
cs

 

[실행결과]

 

 

728x90