11.3 matplotlib ¹«ÀÛÁ¤ »ç¿ëÇØ º¸±â
import matplotlib.pyplot as plt
# ¿ì¸®³ª¶óÀÇ ¿¬°£ 1ÀÎ´ç ±¹¹Î¼ÒµæÀ» °¢°¢ years, gdp¿¡ ÀúÀå
years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
gdp = [67.0, 80.0, 257.0, 1686.0, 6505, 11865.3, 22105.3]
# ¼± ±×·¡ÇÁ¸¦ ±×¸°´Ù. xÃà¿¡´Â years°ª, yÃà¿¡´Â gdp °ªÀÌ Ç¥½ÃµÈ´Ù.
plt.plot(years, gdp, color='green', marker='o', linestyle='solid')
# Á¦¸ñÀ» ¼³Á¤ÇÑ´Ù.
plt.title("GDP per capita")
# yÃà¿¡ ·¹À̺íÀ» ºÙÀδÙ.
plt.ylabel("dollars")
plt.savefig("gdp_per_capita.png", dpi=600) # png À̹ÌÁö·Î ÀúÀå °¡´É
plt.show()
import matplotlib.pyplot as plt
years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
gdp = [67.0, 80.0, 257.0, 1686.0, 6505, 11865.3, 22105.3]
plt.plot(years, gdp, color='green', marker='o', linestyle='solid')
plt.title("GDP per capita")
plt.ylabel("dollars")
plt.savefig("gdp_per_capita.png", dpi=600)
plt.show()
plt.plot(gdp, years, color='red', marker='o', linestyle='solid')
LAB11-1 ¼öÇÐ ÇÔ¼öµµ ½±°Ô ±×·Áº¸ÀÚ
import matplotlib.pyplot as plt
x = [x for x in range(-10, 10)]
y = [2*t for t in x] # 2*x¸¦ ¿ø¼Ò·Î °¡Áö´Â y ÇÔ¼ö
plt.plot(x, y, marker='o') # ¼± ±×·¡ÇÁ¿¡ µ¿±×¶ó¹Ì Ç¥½ÄÀ» Ãâ·Â
plt.axis([-20, 20, -20, 20]) # ±×¸²À» ±×¸± ¿µ¿ªÀ» ÁöÁ¤ÇÔ
plt.show()
import matplotlib.pyplot as plt # È¿À²À» À§Çؼ ¾ÕÀ¸·Î ÀÌ ÁÙÀº »ý·«ÇÔ
x = [x for x in range(-20, 20)] # -20¿¡¼ 20»çÀÌÀÇ ¼ö¸¦ 1ÀÇ °£°ÝÀ¸·Î »ý¼º
y1 = [2*t for t in x] # 2*x¸¦ ¿ø¼Ò·Î °¡Áö´Â y1 ÇÔ¼ö
y2 = [t**2 + 5 for t in x] # x**2 + 5¸¦ ¿ø¼Ò·Î °¡Áö´Â y2 ÇÔ¼ö
y3 = [-t**2 - 5 for t in x] # -x**2 - 5¸¦ ¿ø¼Ò·Î °¡Áö´Â y3 ÇÔ¼ö
# »¡°»ö Á¡¼±, ³ì»ö ½Ç¼±°ú ¼¼¸ð±âÈ£, ÆĶû»ö º°Ç¥¿Í Á¡¼±À¸·Î °¢°¢ÀÇ ÇÔ¼ö¸¦ Ç¥Çö
plt.plot(x, y1, 'r--', x, y2, 'g^-', x, y3, 'b*:')
plt.axis([-30, 30, -30, 30]) # ±×¸²À» ±×¸± ¿µ¿ªÀ» ÁöÁ¤ÇÔ
plt.show()
import matplotlib.pyplot as plt
x = [x for x in range(20)] # 0¿¡¼ 20±îÁöÀÇ Á¤¼ö¸¦ »ý¼º
y = [x**2 for x in range(20)] # 0¿¡¼ 20±îÁöÀÇ Á¤¼ö x¿¡ ´ëÇØ x Á¦°ö°ªÀ» »ý¼º
z = [x**3 for x in range(20)] # 0¿¡¼ 20±îÁöÀÇ Á¤¼ö x¿¡ ´ëÇØ x ¼¼Á¦°ö°ªÀ» »ý¼º
plt.plot(x, x, label='linear') # °¢ ¼±¿¡ ´ëÇÑ ·¹À̺í
plt.plot(x, y, label='quadratic')
plt.plot(x, z, label='qubic')
plt.xlabel('x label') # x ÃàÀÇ ·¹À̺í
plt.ylabel('y label') # y ÃàÀÇ ·¹À̺í
plt.title("My Plot")
plt.legend()
plt.show()
import math
import matplotlib.pyplot as plt
x = []
y = []
for angle in range(360):
x.append(angle)
y.append(math.sin(math.radians(angle)))
plt.plot(x, y)
plt.title("SINE WAVE")
plt.show()
from matplotlib import pyplot as plt
# 1ÀÎ´ç ±¹¹Î¼Òµæ
years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
gdp = [67.0, 80.0, 257.0, 1686.0, 6505, 11865.3, 22105.3]
plt.bar(range(len(years)), gdp)
plt.title("GDP per capita") # Á¦¸ñÀ» ¼³Á¤ÇÑ´Ù.
plt.ylabel("dollars") # yÃà¿¡ ·¹ÀÌºí¸¦ ºÙÀδÙ.
# yÃà¿¡ ƽÀ» ºÙÀδÙ.
plt.xticks(range(len(years)), years)
plt.show()
# 1ÀÎ´ç ±¹¹Î¼Òµæ
years = [1965, 1975, 1985, 1995, 2005, 2015]
ko = [130, 650, 2450, 11600, 17790, 27250]
jp = [890, 5120, 11500, 42130, 40560, 38780]
ch = [100, 200, 290, 540, 1760, 7940]
x_range = range(len(years))
plt.bar(x_range, ko, width = 0.25)
plt.bar(x_range, jp, width = 0.25)
plt.bar(x_range, ch, width = 0.25)
import numpy as np
x_range = np.arange(len(years))
plt.bar(x_range + 0.0, ko, width = 0.25)
plt.bar(x_range + 0.3, jp, width = 0.25)
plt.bar(x_range + 0.6, ch, width = 0.25)
import matplotlib.pyplot as plt
import numpy as np
xData = np.arange(20, 50)
yData = xData + 2*np.random.randn(30) # xData¿¡ randn() ÇÔ¼ö·Î ÀâÀ½À» ¼¯´Â´Ù.
# ÀâÀ½Àº Á¤±ÔºÐÆ÷·Î ¸¸µé¾î Áú °ÍÀÌ´Ù.
plt.scatter(xData, yData)
plt.title('Real Age vs Physical Age')
plt.xlabel('Real Age')
plt.ylabel('Physical Age')
plt.savefig("kkk.png", dpi=600);
plt.show()
import matplotlib.pyplot as plt
times = [8, 14, 2]
timelabels = ["Sleep", "Study", "Play"]
# autopct·Î ¹éºÐÀ²À» Ç¥½ÃÇÒ ¶§ ¼Ò¼öÁ¡ 2¹ø° ÀÚ¸®±îÁö Ç¥½ÃÇÏ°Ô ÇÑ´Ù.
# labels ¸Å°³ º¯¼ö¿¡ timelabels ¸®½ºÆ®¸¦ Àü´ÞÇÑ´Ù.
plt.pie(times, labels = timelabels, autopct = "%.2f")
plt.show()
import matplotlib.pyplot as plt
books = [ 1, 6, 2, 3, 1, 2, 0, 2 ]
# 6°³ÀÇ ºóÀ» ÀÌ¿ëÇÏ¿© books ¾È¿¡ ÀúÀåµÈ ÀÚ·áÀÇ È÷½ºÅä±×·¥ ±×¸®±â
plt.hist(books, bins = 6)
plt.xlabel("books")
plt.ylabel("frequency")
plt.show()
books = [ 1, 6, 2, 3, 1, 2, 0, 2 ]
import matplotlib.pyplot as plt
books = [ 1, 6, 2, 3, 1, 2, 0, 2 ]
# 6°³ÀÇ ºóÀ» ÀÌ¿ëÇÏ¿© books ¾È¿¡ ÀúÀåµÈ ÀÚ·áÀÇ È÷½ºÅä±×·¥ ±×¸®±â
plt.hist(books, bins = 6)
plt.xlabel("books")
plt.ylabel("frequency")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
n_bins = 10
x = np.random.randn(1000)
y = np.random.randn(1000)
plt.hist(x, n_bins, histtype='bar', color= "red");
plt.hist(y, n_bins, histtype='bar', color= "blue", alpha=0.3);
plt.show()
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
plt.hist(x)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
mu1, sigma1 = 10, 2
mu2, sigma2 = -6, 3
standard_Gauss = np.random.randn(10000)
Gauss1 = mu1 + sigma1 * np.random.randn(10000)
Gauss2 = mu2 + sigma2 * np.random.randn(10000)
plt.figure(figsize=(10,6))
plt.hist(standard_Gauss, bins=50, alpha=0.4)
plt.hist(Gauss1, bins=50, alpha=0.4)
plt.hist(Gauss2, bins=50, alpha=0.4)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
random_data = np.random.randn(100)
plt.boxplot(random_data)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
data1 = [1, 2, 3, 4, 5]
data2 = [2, 3, 4, 5, 6]
plt.boxplot([ data1, data2 ] )
plt.boxplot(np.array([ data1, data2 ]) )
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2, figsize=(5, 5))
ax[0, 0].plot(range(10), 'r') #row=0, col=0
ax[1, 0].plot(range(10), 'b') #row=1, col=0
ax[0, 1].plot(range(10), 'g') #row=0, col=1
ax[1, 1].plot(range(10), 'k') #row=1, col=1
plt.show()
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
data = np.random.randn(2, 100)
fig, axs = plt.subplots(2, 2, figsize=(5, 5))
axs[0, 0].hist(data[0])
axs[1, 0].scatter(data[0], data[1])
axs[0, 1].plot(data[0], data[1])
axs[1, 1].hist2d(data[0], data[1])
plt.show()