import matplotlib.pyplot as plt
ratio = [34, 32, 16, 18]
labels = ['Apple', 'Banana', 'Melon', 'Grapes']
colors = ['silver', 'gold', 'whitesmoke', 'lightgray']
plt.pie(ratio, labels=labels, autopct='%.1f%%', startangle=260, counterclock=False, explode = [0, 0.10, 0, 0.10], shadow=True, colors=colors)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
# nrows=1, ncols=2, index=1
# plt.subplot(121)로도 표현 가능
plt.subplot(1, 2, 1)
plt.plot(x1, y1, 'o-')
plt.title('1st Graph')
plt.xlabel('time (s)')
plt.ylabel('Damped oscillation')
# nrows=1, ncols=2, index=2
plt.subplot(1, 2, 2)
plt.plot(x2, y2, '.-')
plt.title('2nd Graph')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
plt.show()
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
fig, ax = plt.subplots(1, 2, figsize=(18, 8))
sns.countplot(x='sex', data=titanic, ax=ax[0])
ax[0].set_title('Count plot - sex')
ax[0].set_ylabel('')
sns.countplot(x='sex', data=titanic, ax=ax[1], hue='survived')
ax[1].set_title('Sex: survived and sex')
# 서브플롯간 간격 조정
plt.subplots_adjust(top=1, bottom=0.1, left=0.1, right=1, hspace=0.5, wspace=0.5)
plt.show()
--------------------------------------------------------
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12,6))
women = titanic[titanic['sex']=='female']
men = titanic[titanic['sex']=='male']
ax = sns.histplot(women[women['survived']==1]['age'], bins=20, label='survived', ax=axes[0], kde=False)
ax = sns.histplot(women[women['survived']==0]['age'], bins=40, label='not_survived', ax=axes[0], kde=False)
ax.legend(); ax.set_title('Female')
ax = sns.histplot(men[men['survived']==1]['age'], bins=20, label='survived', ax=axes[1], kde=False)
ax = sns.histplot(men[men['survived']==0]['age'], bins=40, label='not_survived', ax=axes[1], kde=False)
plt.hist
(히스토그램), plt.regplot
(산점도)grid = sns.FacetGrid(titanic, row='pclass', col='sex', height=4, aspect=2)
# bins: 히스토그램의 가로축 구간의 개수, color: 컬러 설정, alpha: 투명도
grid.map(plt.hist, 'age', alpha=0.8, bins=20)
# 범례 추가
grid.add_legend();
# xlabel, ylabel 추가
grid.set_axis_labels()
# x, y축 경계 설정
grid.set(xlim=(N, N), ylim=(N, N))
px.line( 데이터 프레임, x축에 들어갈 column명, y축에 들어갈 column명 )
color
hue와 비슷한 역할, 컬럼명 넣기markers
= bool: 마커 생성, symbol
=column 명: 마커 다르게import plotly.express as px
fig = px.line(df, x='year', y='lifeExp', color='country', symbol="country", labels={"year":"년도","pop":"총 인구수"}, title='오세아니아의 연도별 인구 변화')
fig = px.histogram(titanic, x='age', width = 500, height = 300)
fig.show()
pd.crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, margins_name='All', dropna=True, normalize=False)
# margins 합계
pd.crosstab(titanic['pclass'], titanic['survived'], margins=True)
Reference
1) https://wikidocs.net/92114
2) https://wikidocs.net/141537
3) matplotlib subplots
4) https://steadiness-193.tistory.com/201
5) https://wikidocs.net/180789
6) https://suy379.tistory.com/149