python

[python] bar / barh 그래프

독립성이 강한 ISFP 2022. 5. 19. 02:44
728x90
반응형

bar 그래프

fig=plt.figure(figsize=(8,4),dpi=100)
ax=fig.subplots(1,1)

X=np.array([5,25,50,20])
ax.bar(range(len(X)),X)

bar: various width

fig=plt.figure(figsize=(8,4),dpi=100)
axs=fig.subplots(1,3)

def bar_width(ax,data,w):
    ax.bar(range(len(data)),data,width=w)
    
data=np.array([5,25,50,20])
bar_width(axs[0],data,0.5)
bar_width(axs[1],data,0.8)
bar_width(axs[2],data,1)

barh : 수평막대

fig=plt.figure(figsize=(8,4),dpi=100)
ax=fig.subplots()

data=np.array([5,25,50,20])
ax.barh(range(len(data)),data)

다중막대차트 그리기

#### 1번째 방법
fig=plt.figure(figsize=(8,4),dpi=100)
ax=fig.subplots()

data=np.array([[5,25,50,20],[4,23,51,17],[6,22,52,19]])

X=np.arange(4)

_=ax.bar(X+0.00,data[0],width=0.25)
_=ax.bar(X+0.25,data[1],width=0.25)
_=ax.bar(X+0.50,data[2],width=0.25)

#### 2번째 방법 (for loop 사용)
fig=plt.figure(figsize=(8,4),dpi=100)
ax=fig.subplots()

data=np.array([[5,25,50,20],[4,23,51,17],[6,22,52,19]])

X=np.arange(4)

w1=0.25
for i,j in enumerate(data):
    _=ax.bar(X+i*w1,data[i],width=w1)

실습

fig=plt.figure(figsize=(15,5),dpi=100)
ax=fig.subplots(1,3)

data=pd.read_table('dat_bar.txt',sep='\t',index_col=0)
data

X=np.array([0,13])

## 1   loc 사용
for i,k in enumerate(data.index):
    _=ax[0].bar(X+i ,data.loc[k,:], width=1)
_=ax[0].set_title('loc')

## 2   iloc 사용
for ii in range(len(data)):
    _=ax[1].bar(X+ii, data.iloc[ii,:] , width=1)
_=ax[1].set_title('iloc')

## 3 (위에꺼 확인하는 코드)
_=ax[2].bar(X, data.loc['jan',:]) # ([0, 13], [76, 54])
_=ax[2].bar(X+1, data.loc['feb',:])
_=ax[2].bar(X+2,data.loc['mar',:])

분할 막대차트 그리기

fig=plt.figure(figsize=(15,5),dpi=100)
ax=fig.subplots(1,2)

data=np.array([[20,10,35,20],[10,12,9,10],[5,6,6,5]])

X=np.arange(4)

### 1번째 방법
ax[0].bar(X,data[0],color='b')
ax[0].bar(X,data[1],bottom=data[0],color='g')
ax[0].bar(X,data[2],bottom=data[0]+data[1],color='r')

### 2번째 방법 (for loop 사용)
color_list=['b','g','r']

for i in range(len(data)):
    ax[1].bar(X, data[i], bottom = np.sum(data[:i], axis=0), color=color_list[i])

실습2

fig=plt.figure(figsize=(3,5),dpi=100)
ax=fig.subplots()

data=pd.read_table('dat_bar.txt',sep='\t',index_col=0)
data

X=np.array([0,1])
for ii in range(len(data)):
    _=ax.bar(X, data.iloc[ii], bottom = data.iloc[:ii].apply(np.sum))

실습3

fig=plt.figure(figsize=(10,5),dpi=100)
ax=fig.subplots(1,3)

women_pop=np.array([5,30,45,22])
men_pop=np.array([5,25,50,20])

X=np.arange(4)

_=ax[0].barh(X,women_pop,color='r')
_=ax[0].barh(X,-men_pop,color='b')

_=ax[1].bar(X,women_pop,width=0.25)
_=ax[1].bar(X+0.25,men_pop,0.25)

_=ax[2].bar(X,women_pop ,width=0.25)
_=ax[2].bar(X,men_pop, bottom=women_pop ,width=0.25)

다중 막대 그래프 ver2

fig=plt.figure(figsize=(10,5),dpi=100)
ax=fig.subplots(1,2)

women_pop=np.array([5,30,45,22])
men_pop=np.array([5,25,50,20])

X=np.arange(4)

## 1번째 방법
_=ax[0].bar([0.0,1.0],[women_pop[0],men_pop[0]],width=0.2)
_=ax[0].bar([0.2,1.2],[women_pop[1],men_pop[1]],width=0.2)
_=ax[0].bar([0.4,1.4],[women_pop[2],men_pop[2]],width=0.2)
_=ax[0].bar([0.6,1.6],[women_pop[3],men_pop[3]],width=0.2)

#---------------------------------------------------------

# 0*w, 1+w*0
# 1*w, 1+w*1
# 2*w, 1+w*2
# 3*w, 1+w*3

### 2번째 방법 (for loop 사용)
w=0.2
for i in range(len(women_pop)):
    _=ax[1].bar([i*w,1+w*i],[women_pop[i],men_pop[i]],width=0.2)

728x90
반응형