

boxPlotEx1
import numpy as np
import matplotlib.pyplot as plt
#가장 기본적인 형태
# data = np.random.randn(10000) #randn종모양 형태의 그래프
# plt.boxplot(data)
# plt.show()
N = 500
normal1 = np.random.normal(loc=100, scale=15, size=N)
normal2 = np.random.normal(loc=88, scale=30, size=N)
fig=plt.figure(figsize=(8,8))
ax1 = fig.add_subplot(111)
ax1.boxplot([normal1, normal2],
labels=['normal1', 'normal2'], vert=False,
showmeans=True)
#showmeans = 평균값 세모모양으로 표시, vert = 가로로 그래프 변경
plt.show()

boxPlotEx2
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
frame = pd.read_csv('tips.csv', index_col=0)
print(frame)
print(frame.time.unique())
dinner = frame.loc[frame['time'] == 'Dinner', 'total_bill']
dinner.index.name = 'Dinner'
print(dinner)
lunch = frame.loc[frame['time'] == 'Lunch', 'total_bill']
lunch.index.name = 'Lunch'
fig, axes = plt.subplots(1,2, figsize=(11,6))
print(axes)
axes[0].boxplot(dinner, vert=True)
axes[0].set_title('vertical boxplot(dinner_total_bill)')
axes[1].boxplot(lunch, vert=True)
axes[1].set_title('vertical boxplot(lunch_total_bill')
#최대치 이상의 데이터 하나당 점 하나라고 보면됨
plt.show()

quiz1 확인하기



histoggramEx
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# plt.rc('font', family='Malgun Gothic')
x = np.random.randn(10000)
plt.hist(x, bins=100, density=True)
plt.show()

human = pd.read_csv('human_height.csv')
human.info()
man = human['man']
woman = human['woman']
plt.figure(figsize=(10,7))
plt.hist(man, bins=20, alpha=0.5, label='man', rwidth=0.8)
#rwidth = 그래프 간격 줄이기
plt.hist(woman, bins=20, alpha=0.5, label='man', rwidth=0.8)
plt.xlabel('height')
plt.ylabel('frequency')
plt.title('man height distribution')
plt.show()

multiViewEx1
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.rc('font', family='Malgun Gothic')
mpg = pd.read_csv('mpg.csv')
fig = plt.figure(figsize=(12,9))
grid = plt.GridSpec(4,4, hspace=0.7, wspace=0.4)
ax_main = fig.add_subplot(grid[:-1, :-1])
ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
ax_bottom = fig.add_subplot(grid[-1, :-1], xticklabels=[], yticklabels=[])
ax_main.scatter('displ', 'hwy', data=mpg, edgecolor='k')
ax_bottom.hist(mpg.displ, bins=40, color='orange')
ax_bottom.invert_yaxis()
#y축기준으로 뒤집기
ax_right.hist(mpg.hwy, bins=40, color='darkblue', orientation='horizontal')
ax_main.yaxis.tick_right()
ax_main.set_xlabel('엔진크기')
ax_main.set_ylabel('마일 수')
ax_main.set_title('grid Spec Use', fontsize=17)
plt.show()

multiViewEx2
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10,6))
grid = plt.GridSpec(2,2, width_ratios=[3,1], height_ratios=[1,3])
ax1 = fig.add_subplot(grid[0])
ax1.plot(np.random.randn(50))
ax2 = fig.add_subplot(grid[1])
ax2.plot(np.random.randn(50))
ax3 = fig.add_subplot(grid[2])
ax3.plot(np.random.randn(50))
ax4 = fig.add_subplot(grid[3])
ax4.plot(np.random.randn(50))
plt.show()

quiz2 확인하기
'Pyhton > matplotlib' 카테고리의 다른 글
| Day38_Python(14)_Pm (0) | 2025.10.02 |
|---|---|
| Day37_Python(13)_Pm (0) | 2025.10.01 |
| Day37_Python(13)_Am (0) | 2025.10.01 |
| Day36_Python(12)_Pm (0) | 2025.09.30 |
| Day36_Python(12)_Am (0) | 2025.09.30 |