250x250
Link
๋์ GitHub Contribution ๊ทธ๋ํ
Loading data ...
Notice
Recent Posts
Recent Comments
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- ๋์ํ๋ณธ
- PCA
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- datascience
- ์ฃผ์ฑ๋ถ๋ถ์
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- Lambda
- ์ค๋ฒ์ํ๋ง
- ์๋ํด๋ผ์ฐ๋
- ๋น ๋ฐ์ดํฐ
- ํฌ๋กค๋ง
- ADsP
- ํ์ด์ฌ
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- dataframe
- ADP
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- iloc
- ๋ฐ์ดํฐ๋ถ์
- ์ธ๋์ํ๋ง
- ๊ตฐ์งํ
- t-test
- ๋ ๋ฆฝํ๋ณธ
- numpy
- opencv
- DBSCAN
- ํ ์คํธ๋ถ์
- LDA
- Python
- pandas
Archives
Data Science LAB
[Python] Matplotlib ์๊ฐํ ๋ณธ๋ฌธ
728x90
์ ๊ทธ๋ํ
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline
plt.title('Plot')
plt.plot([1,4,9,16])
plt.show()
์ ๋ชฉ ์ค์
plt.title('x ticks')
plt.plot([10,20,30,40], [1,4,9,16])
plt.show()
ํ๊ธ ์ค์ (Mac)
# ํฐํธ ์ค์
mpl.rc('font', family='NanumGothic')
#์ ๋์ฝ๋์์ ์์ ๋ถํธ ์ค์
mpl.rc('axes', unicode_minus=False)
์คํ์ผ ์ง์
# ์คํ์ผ ์ง์
plt.title('rs style')
plt.plot([10,20,30,40], [1,4,9,16], 'rs--')
plt.show()
- ์๊น : b, g, r, c, m, y, k, w
- ๋ง์ปค : ".", '," , o, v, ^, <, >, 1,2,3,4,s,o, *, h, H, +, D, d
- ์ ์คํ์ผ : -, --, -., :
- ์คํ์ผ ๋ฌธ์์ด : c(์ ์๊น), lw(์ ๊ตต๊ธฐ), ls(์ ์คํ์ผ),marker(๋ง์ปค์ข ๋ฅ), ms(๋ง์ปคํฌ๊ธฐ), mec(๋ง์ปค์ ์๊น), mew(๋ง์ปค ์ ๊ตต๊ธฐ), mfc(๋ง์ปค ๋ด๋ถ ์๊น)
plt.plot([10,20,30,40], [1,4,9,16], c='b', lw = 5,
ls='--', marker='o', ms=15, mec='g', mew=5, mfc='r')
plt.title('style example')
plt.show()
#๊ทธ๋ฆผ ๋ฒ์ ์ง์
plt.title('x, y lim')
plt.plot([10,20,30,40], [1,4,9,16],
c='b', lw=5,ls='--',marker='o',ms=15,mec='g',mew=5,mfc='r')
plt.xlim(0,50)
plt.ylim(-10,30)
plt.show()
# ํฑ ์ค์ : ์ถ์์ ์์น ํ์ ์ง์
import numpy as np
X = np.linspace(-np.pi, np.pi, 256)
C = np.cos(X)
plt.title('tick label')
plt.plot(X,C)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
plt.yticks([-1,0,1])
plt.show()
X = np.linspace(-np.pi, np.pi, 256)
C = np.cos(X)
plt.title('Latex')
plt.plot(X,C)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
plt.yticks([-1,0,1], ['low','zero','high'])
plt.show()
# ๊ทธ๋ฆฌ๋ ์ค์
X = np.linspace(-np.pi, np.pi, 256)
C = np.cos(X)
plt.title('Grid')
plt.plot(X,C)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
plt.yticks([-1,0,1], ['low','zero','high'])
plt.grid(True)
plt.show()
# ๊ฒน์ณ ๊ทธ๋ฆฌ๊ธฐ
plt.plot([1,4,9,16],
c='b',lw=5,ls='--',marker='o',ms=15,mec='g',mew=5,mfc='r')
plt.plot([9,16,4,1],
c='k',lw=3,ls=':',marker='s',ms=10,mec='m',mew=5,mfc='c')
plt.show()
# ๋ฒ๋ก ํ์
X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)
plt.title('legend')
plt.plot(X,C, ls='--', label='cosine')
plt.plot(X,S, ls=':', label='sine')
plt.legend(loc=2)
plt.show()
# x์ถ, y์ถ label, title
X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)
plt.plot(X,C, label='cosine')
plt.xlabel('time')
plt.ylabel('amplitude')
plt.title('Cosine Plot')
plt.show()
# Figure ๊ฐ์ฒด
np.random.seed(0)
f1 = plt.figure(figsize=(10,2))
plt.title('figsize : (10,2)')
plt.plot(np.random.randn(100))
plt.show()
f1 = plt.figure(1)
plt.title('now figure')
plt.plot([1,2,3,4], 'ro:')
f2 = plt.gcf()
print(f1, id(f1))
print(f2, id(f2))
plt.show()
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 5.0)
y1 = np.cos(2*np.pi*x1) * np.exp(-x1)
y2 = np.cos(2*np.pi*x2)
ax1 = plt.subplot(2,1,1)
plt.plot(x1,y1,'yo-')
plt.title('A tale of 2 subplots')
plt.ylabel("Damped oscillation")
ax2 = plt.subplot(2,1,2)
plt.plot(x2, y2, 'r.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
plt.tight_layout()
plt.show()
# 2 * 2 ํํ
np.random.seed(0)
plt.subplot(221)
plt.plot(np.random.rand(5))
plt.title('axes1')
plt.subplot(222)
plt.plot(np.random.rand(5))
plt.title('axes2')
plt.subplot(223)
plt.plot(np.random.rand(5))
plt.title('axes3')
plt.subplot(224)
plt.plot(np.random.rand(5))
plt.title('axes4')
plt.tight_layout()
plt.show()
fig, axes = plt.subplots(2,2)
np.random.seed(0)
axes[0,0].plot(np.random.rand(5))
axes[0,0].set_title('axes1')
axes[0,1].plot(np.random.rand(5))
axes[0,1].set_title('axes2')
axes[1,0].plot(np.random.rand(5))
axes[1,0].set_title('axes3')
axes[1,1].plot(np.random.rand(5))
axes[1,1].set_title('axes4')
plt.tight_layout()
plt.show()
# Axis ๊ฐ์ฒด์ ์ถ : ๋ณต์์ y์ถ ์์ฑ
fig, ax0 = plt.subplots()
ax1 = ax0.twinx()
ax0.set_title('2 Axis')
ax0.plot([10,5,2,9,7], 'r-', label='y0')
ax0.set_ylabel('y0')
ax0.grid(False)
ax1.plot([100,200,220, 120],'g:', label='y1')
ax1.set_ylabel('y1')
ax0.set_xlabel('X')
plt.show()
# Bar Chart
from matplotlib import rc
rc('font', family='AppleGothic')
plt.rcParams['axes.unicode_minus'] = False
y = [2,3,1]
x = np.arange(len(y))
xlabel = ['๊ฐ', '๋','๋ค']
plt.title("Bar chart")
plt.bar(x,y)
plt.xticks(x, xlabel)
plt.yticks(sorted(y))
plt.xlabel('๊ฐ๋๋ค')
plt.ylabel('๋น๋ ์')
plt.show()
# alpha : ํฌ๋ช
๋
people = ['๋ชฝ๋ฃก', '์ถํฅ', '๋ฐฉ์', 'ํฅ๋จ']
y_pos = np.arange(len(people))
performance = 3 + 10*np.random.rand(len(people))
error = np.random.rand(len(people))
plt.title('Bar Chart')
plt.barh(y_pos, performance, xerr=error, alpha=0.4)
plt.yticks(y_pos, people)
plt.xlabel('x ๋ผ๋ฒจ')
plt.show()
# ์คํ
ํ๋กฏ : ๋ฐ์ฐจํธ์ ์ ์ฌํ์ง๋ง ํญ์ด ์์
x = np.linspace(0.1, 2*np.pi, 10)
plt.title('Stem Plot')
plt.stem(x, np.cos(x), '-.')
plt.show()
# ํ์ด์ฐจํธ
labels = ['๊ฐ๊ตฌ๋ฆฌ', '๋ผ์ง', '๊ฐ', 'ํต๋๋ฌด']
sizes = [15, 30, 45, 10]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
explode = (0, 0.1, 0, 0)
plt.title('Pie Chart')
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f$', shadow=True, startangle=90)
plt.axis('equal') #์์ ํํ๋ฅผ ์ ์งํ๋ ๋ช
๋ น์ด
plt.show()
# ํ์คํ ๊ทธ๋จ
np.random.seed(0)
x = np.random.randn(1000)
plt.title('Histogram')
arrays, bins, patchs = plt.hist(x, bins=10)
plt.show()
# ์ค์บํฐ ํ๋กฏ
np.random.seed(0)
X = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
plt.title('Scatter Plot')
plt.scatter(X, y)
plt.show()
# ๋ฐ์ดํฐ๊ฐ 3์ฐจ์ ํน์ 4์ฐจ์์ธ ๊ฒฝ์ฐ์ ์ค์บํฐํ๋กฏ
N = 30
np.random.seed(0)
x = np.random.rand(N)
y1 = np.random.rand(N)
y2 = np.random.rand(N)
y3 = np.pi * (15*np.random.rand(N)) **2
plt.title('Bubble Chart')
plt.scatter(x, y1, c=y2, s=y3)
plt.show()
# Imshow
from sklearn.datasets import load_digits
digits = load_digits()
X = digits.images[0]
plt.title('Mnist digits; 0')
plt.imshow(X, interpolation='nearest', cmap=plt.cm.bone_r)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.subplots_adjust(left=0.35, right=0.65, bottom=0.35, top=0.65)
plt.show()
# ์ปฌ๋ฌ๋งต ํ์ธ
dir(plt.cm)[:10]
# ์ปจํฌ์ด ํ๋กฏ
def f(x, y):
return (1 - x/2 + x**5 + y**3) * np.exp(-x**2 -y**2)
n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
XX, YY = np.meshgrid(x, y)
ZZ = f(XX, YY)
plt.title('Contour Plot')
plt.contourf(XX, YY, ZZ, alpha=.75, cmap='jet')
plt.contour(XX, YY, ZZ, colors='black')
plt.show()
# 3D ์ํผ์ค ํ๋กฏ
from mpl_toolkits.mplot3d import Axes3D
X = np.arange(-4, 4, 0.25)
y = np.arange(-4, 4, 0.25)
XX, YY = np.meshgrid(X, y)
RR = np.sqrt(XX**2 + YY**2)
ZZ = np.sin(RR)
fig = plt.figure()
ax = Axes3D(fig)
ax.set_title('3D Surface Plot')
ax.plot_surface(XX, YY, ZZ, rstride=1, cstride=1, cmap='hot')
plt.show()
728x90
'๐ Python > ์๊ฐํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] Pandas๋ฅผ ์ด์ฉํ ์๊ฐํ (0) | 2022.09.21 |
---|---|
[Python] Mac์์ Matplotlib ํ๊ธ ๊นจ์ง ํ์ ํด๊ฒฐ ๋ฐฉ๋ฒ (0) | 2022.09.20 |
[Python] matplotlib ํ๊ธ ๊นจ์ง ๋ฐฉ์ง, ํ๊ธ ์ถ๋ ฅ ๋ฐฉ๋ฒ (0) | 2022.08.26 |
[Python] matplotlib subplots ์ฌ์ฉ(์ฌ๋ฌ๊ฐ์ ๊ทธ๋ํ๋ฅผ ํ๋ฒ์) (0) | 2022.08.23 |
Comments