반응형
matplotlib 그래프¶
In [1]:
!pip install --upgrade --force-reinstall seaborn
Defaulting to user installation because normal site-packages is not writeable Collecting seaborn Downloading seaborn-0.13.2-py3-none-any.whl (294 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 294.9/294.9 kB 6.4 MB/s eta 0:00:00a 0:00:01 Collecting numpy!=1.24.0,>=1.20 Downloading numpy-2.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (19.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 19.3/19.3 MB 56.4 MB/s eta 0:00:0000:0100:01 Collecting matplotlib!=3.6.1,>=3.4 Downloading matplotlib-3.9.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.3/8.3 MB 56.5 MB/s eta 0:00:0000:0100:01 Collecting pandas>=1.2 Downloading pandas-2.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13.1/13.1 MB 82.4 MB/s eta 0:00:0000:0100:01 Collecting contourpy>=1.0.1 Downloading contourpy-1.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (304 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 305.0/305.0 kB 40.2 MB/s eta 0:00:00 Collecting python-dateutil>=2.7 Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 229.9/229.9 kB 32.2 MB/s eta 0:00:00 Collecting pillow>=8 Downloading pillow-10.3.0-cp39-cp39-manylinux_2_28_x86_64.whl (4.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.5/4.5 MB 47.4 MB/s eta 0:00:0000:0100:01 Collecting importlib-resources>=3.2.0 Downloading importlib_resources-6.4.0-py3-none-any.whl (38 kB) Collecting kiwisolver>=1.3.1 Downloading kiwisolver-1.4.5-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 63.8 MB/s eta 0:00:00 Collecting packaging>=20.0 Downloading packaging-24.1-py3-none-any.whl (53 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 54.0/54.0 kB 5.4 MB/s eta 0:00:00 Collecting cycler>=0.10 Downloading cycler-0.12.1-py3-none-any.whl (8.3 kB) Collecting fonttools>=4.22.0 Downloading fonttools-4.53.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.6/4.6 MB 90.9 MB/s eta 0:00:00:00:01 Collecting pyparsing>=2.3.1 Downloading pyparsing-3.1.2-py3-none-any.whl (103 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 103.2/103.2 kB 11.6 MB/s eta 0:00:00 Collecting pytz>=2020.1 Downloading pytz-2024.1-py2.py3-none-any.whl (505 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 505.5/505.5 kB 34.0 MB/s eta 0:00:00 Collecting tzdata>=2022.7 Downloading tzdata-2024.1-py2.py3-none-any.whl (345 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 345.4/345.4 kB 31.2 MB/s eta 0:00:00 Collecting zipp>=3.1.0 Downloading zipp-3.19.2-py3-none-any.whl (9.0 kB) Collecting six>=1.5 Downloading six-1.16.0-py2.py3-none-any.whl (11 kB) Installing collected packages: pytz, zipp, tzdata, six, pyparsing, pillow, packaging, numpy, kiwisolver, fonttools, cycler, python-dateutil, importlib-resources, contourpy, pandas, matplotlib, seaborn Attempting uninstall: pytz Found existing installation: pytz 2022.5 Uninstalling pytz-2022.5: Successfully uninstalled pytz-2022.5 Attempting uninstall: zipp Found existing installation: zipp 3.10.0 Uninstalling zipp-3.10.0: Successfully uninstalled zipp-3.10.0 Attempting uninstall: six Found existing installation: six 1.16.0 Uninstalling six-1.16.0: Successfully uninstalled six-1.16.0 Attempting uninstall: pyparsing Found existing installation: pyparsing 3.0.9 Uninstalling pyparsing-3.0.9: Successfully uninstalled pyparsing-3.0.9 Attempting uninstall: pillow Found existing installation: Pillow 9.3.0 Uninstalling Pillow-9.3.0: Successfully uninstalled Pillow-9.3.0 Attempting uninstall: packaging Found existing installation: packaging 21.3 Uninstalling packaging-21.3: Successfully uninstalled packaging-21.3 Attempting uninstall: numpy Found existing installation: numpy 1.23.3 Uninstalling numpy-1.23.3: Successfully uninstalled numpy-1.23.3 Attempting uninstall: kiwisolver Found existing installation: kiwisolver 1.4.4 Uninstalling kiwisolver-1.4.4: Successfully uninstalled kiwisolver-1.4.4 Attempting uninstall: fonttools Found existing installation: fonttools 4.38.0 Uninstalling fonttools-4.38.0: Successfully uninstalled fonttools-4.38.0 Attempting uninstall: cycler Found existing installation: cycler 0.11.0 Uninstalling cycler-0.11.0: Successfully uninstalled cycler-0.11.0 Attempting uninstall: python-dateutil Found existing installation: python-dateutil 2.8.2 Uninstalling python-dateutil-2.8.2: Successfully uninstalled python-dateutil-2.8.2 Attempting uninstall: contourpy Found existing installation: contourpy 1.0.6 Uninstalling contourpy-1.0.6: Successfully uninstalled contourpy-1.0.6 Attempting uninstall: pandas Found existing installation: pandas 1.4.2 Uninstalling pandas-1.4.2: Successfully uninstalled pandas-1.4.2 Attempting uninstall: matplotlib Found existing installation: matplotlib 3.6.0 Uninstalling matplotlib-3.6.0: Successfully uninstalled matplotlib-3.6.0 Attempting uninstall: seaborn Found existing installation: seaborn 0.12.0 Uninstalling seaborn-0.12.0: Successfully uninstalled seaborn-0.12.0 ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. scipy 1.8.1 requires numpy<1.25.0,>=1.17.3, but you have numpy 2.0.0 which is incompatible. Successfully installed contourpy-1.2.1 cycler-0.12.1 fonttools-4.53.0 importlib-resources-6.4.0 kiwisolver-1.4.5 matplotlib-3.9.0 numpy-2.0.0 packaging-24.1 pandas-2.2.2 pillow-10.3.0 pyparsing-3.1.2 python-dateutil-2.9.0.post0 pytz-2024.1 seaborn-0.13.2 six-1.16.0 tzdata-2024.1 zipp-3.19.2 [notice] A new release of pip available: 22.2.2 -> 24.1.1 [notice] To update, run: pip install --upgrade pip
모듈 Import¶
In [2]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
# Unicode warning 제거 (폰트 관련 경고메시지)
plt.rcParams['axes.unicode_minus']=False
# 한글 폰트 설정
plt.rcParams['font.family'] = "NanumGothic"
# 그래프 출력 사이즈 설정
plt.rcParams["figure.figsize"] = (12, 10)
Scatterplot¶
0 ~ 1 사이의 임의의 랜덤한 값을 생성합니다.
In [3]:
np.random.rand(50)
Out[3]:
array([0.62881031, 0.42937313, 0.78624726, 0.58871169, 0.24828663, 0.03681471, 0.85600288, 0.50050205, 0.260603 , 0.69850733, 0.56113708, 0.860106 , 0.79483302, 0.45692983, 0.54384138, 0.47542397, 0.09426981, 0.3702961 , 0.21580396, 0.51580194, 0.45960071, 0.91801268, 0.91253519, 0.75383199, 0.89420131, 0.0890961 , 0.64751513, 0.09491463, 0.97345499, 0.18921112, 0.55480593, 0.68921671, 0.9243705 , 0.5243626 , 0.781508 , 0.70847555, 0.99692983, 0.50728519, 0.00788625, 0.25956128, 0.50683897, 0.92157855, 0.00652356, 0.65054316, 0.76185927, 0.70391341, 0.31149907, 0.02614032, 0.27916092, 0.9953763 ])
x, y, colors, area 설정하기¶
- colors 는 임의 값을 color 값으로 변환합니다.
- area는 점의 넓이를 나타냅니다. 값이 커지면 당연히 넓이도 커집니다.
In [4]:
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.arange(50)
area = x * y * 250
In [5]:
plt.scatter(x, y, s=area, c=colors)
plt.show()
color, alpha¶
- color에 컬러를 지정하면, 컬러 값을 모두 같게 가져갈 수도 있습니다.
- alpha값은 투명도를 나타내며 0 ~ 1 사이의 값을 지정해 줄 수 있으며, 0에 가까울 수록 투명한 값을 가집니다.
In [6]:
np.random.rand(50)
Out[6]:
array([0.24557946, 0.44399065, 0.31220244, 0.93847529, 0.04239565, 0.37289896, 0.40686842, 0.94386493, 0.94232699, 0.93225503, 0.25688768, 0.91783008, 0.57507472, 0.13402612, 0.7316266 , 0.50171634, 0.70980674, 0.22579066, 0.73296916, 0.87460534, 0.61589997, 0.91675269, 0.14489578, 0.54333542, 0.64616292, 0.37452708, 0.85776254, 0.15532672, 0.2286975 , 0.58649138, 0.31110777, 0.9368848 , 0.76920667, 0.57317576, 0.71403262, 0.20433079, 0.25853246, 0.53995399, 0.14023826, 0.07321291, 0.56570372, 0.28921533, 0.79367733, 0.10096392, 0.31030015, 0.17688359, 0.16222511, 0.6619724 , 0.54803703, 0.73946328])
In [7]:
plt.figure(figsize=(12, 6))
plt.subplot(131)
plt.scatter(x, y, s=area, color='purple', alpha=0.1)
plt.title('alpha=0.1')
plt.subplot(132)
plt.title('alpha=0.5')
plt.scatter(x, y, s=area, color='purple', alpha=0.5)
plt.subplot(133)
plt.title('alpha=1.0')
plt.scatter(x, y, s=area, color='purple', alpha=1.0)
plt.show()
Barplot, Barhplot¶
1개의 canvas 안에 다중 그래프 그리기
Barplot 생성¶
In [8]:
x = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
y = [66, 80, 60, 50, 80, 10]
plt.figure(figsize=(6, 3))
# plt.bar(x, y)
plt.bar(x, y, align='center', alpha=0.7, color='red')
plt.xticks(x)
plt.ylabel('Number of Students')
plt.title('Subjects')
plt.show()
Barhplot (축 변환)¶
barh 함수에서는 xticks로 설정했던 부분을 yticks로 변경합니다.
In [9]:
x = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
y = [66, 80, 60, 50, 80, 10]
plt.barh(x, y, align='center', alpha=0.7, color='green')
plt.yticks(x)
plt.xlabel('Number of Students')
plt.title('Subjects')
plt.show()
Barplot에서 비교 그래프 그리기¶
In [10]:
x_label = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
x = np.arange(len(x_label))
y_1 = [66, 80, 60, 50, 80, 10]
y_2 = [55, 90, 40, 60, 70, 20]
# 넓이 지정
width = 0.35
# subplots 생성
fig, axes = plt.subplots()
# 넓이 설정
axes.bar(x - width/2, y_1, width, align='center', alpha=0.5)
axes.bar(x + width/2, y_2, width, align='center', alpha=0.8)
# xtick 설정
plt.xticks(x)
axes.set_xticklabels(x_label)
plt.ylabel('Number of Students')
plt.title('Subjects')
plt.legend(['john', 'peter'])
plt.show()
In [11]:
x_label = ['Math', 'Programming', 'Data Science', 'Art', 'English', 'Physics']
x = np.arange(len(x_label))
y_1 = [66, 80, 60, 50, 80, 10]
y_2 = [55, 90, 40, 60, 70, 20]
# 넓이 지정
width = 0.35
# subplots 생성
fig, axes = plt.subplots()
# 넓이 설정
axes.barh(x - width/2, y_1, width, align='center', alpha=0.5, color='green')
axes.barh(x + width/2, y_2, width, align='center', alpha=0.8, color='red')
# xtick 설정
plt.yticks(x)
axes.set_yticklabels(x_label)
plt.xlabel('Number of Students')
plt.title('Subjects')
plt.legend(['john', 'peter'])
plt.show()
Line Plot¶
기본 lineplot 그리기¶
In [12]:
x = np.arange(0, 10, 0.1)
y = 1 + np.sin(x)
plt.plot(x, y)
plt.xlabel('x value', fontsize=15)
plt.ylabel('y value', fontsize=15)
plt.title('sin graph', fontsize=18)
plt.grid()
plt.show()
2개 이상의 그래프 그리기¶
- color: 컬러 옵션
- alpha: 투명도 옵션
In [13]:
x = np.arange(0, 10, 0.1)
y_1 = 1 + np.sin(x)
y_2 = 1 + np.cos(x)
plt.plot(x, y_1, label='1+sin', color='blue', alpha=0.3)
plt.plot(x, y_2, label='1+cos', color='red', alpha=0.7)
plt.xlabel('x value', fontsize=15)
plt.ylabel('y value', fontsize=15)
plt.title('sin and cos graph', fontsize=18)
plt.grid()
plt.legend()
plt.show()
마커 스타일링¶
- marker: 마커 옵션
In [14]:
x = np.arange(0, 10, 0.1)
y_1 = 1 + np.sin(x)
y_2 = 1 + np.cos(x)
plt.plot(x, y_1, label='1+sin', color='blue', alpha=0.3, marker='o')
plt.plot(x, y_2, label='1+cos', color='red', alpha=0.7, marker='+')
plt.xlabel('x value', fontsize=15)
plt.ylabel('y value', fontsize=15)
plt.title('sin and cos graph', fontsize=18)
plt.grid()
plt.legend()
plt.show()