# Trực quan hoá dữ liệu¶

In [3]:
import pylab as plt
mySamples = []
myLinear = []
myCubic = []
myExponential = []

for i in range(0, 30):
mySamples.append(i)
myLinear.append(i)
myCubic.append(i**3)
myExponential.append(1.5**i)

plt.plot(mySamples, myLinear)

Out[3]:
[<matplotlib.lines.Line2D at 0x112343550>]
In [4]:
plt.plot(mySamples, myLinear)
plt.plot(mySamples, myCubic)
plt.plot(mySamples, myExponential)

Out[4]:
[<matplotlib.lines.Line2D at 0x11253e7f0>]

## Tách plot¶

In [5]:
plt.figure('lin')
plt.plot(mySamples, myLinear)
plt.figure('cube')
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.plot(mySamples, myExponential)

Out[5]:
[<matplotlib.lines.Line2D at 0x112ec7e10>]

## Thêm nhãn - label¶

In [7]:
plt.figure('lin')
plt.xlabel('sample points')
plt.ylabel('linear function')
plt.plot(mySamples, myLinear)
plt.figure('cube')
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.plot(mySamples, myExponential)

Out[7]:
Text(0,0.5,'quadratic function')

## Thêm tiêu đề - Title¶

In [10]:
plt.figure('lin')
plt.xlabel('sample points')
plt.ylabel('linear function')
plt.plot(mySamples, myLinear)
plt.figure('cube')
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.plot(mySamples, myExponential)

plt.figure('lin')
plt.title('Linear')
plt.figure('cube')
plt.title('Cubic')
plt.figure('expo')
plt.title('Exponential')

Out[10]:
Text(0.5,1,'Exponential')
In [11]:
plt.figure('lin')
plt.clf()
plt.xlabel('sample points')
plt.ylabel('linear function')
plt.plot(mySamples, myLinear)
plt.clf()
plt.figure('cube')
plt.clf()
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.clf()
plt.plot(mySamples, myExponential)

plt.figure('lin')
plt.title('Linear')
plt.figure('cube')
plt.title('Cubic')
plt.figure('expo')
plt.title('Exponential')

Out[11]:
Text(0.5,1,'Exponential')

## Hiển thị hỗ trợ so sánh kết quả giữa các đồ thị¶

In [12]:
plt.figure('lin')
plt.clf()
plt.ylim(0, 1000)
plt.plot(mySamples, myLinear)
plt.clf()
plt.ylim(0, 1000)
plt.figure('lin')
plt.title('Linear')

Out[12]:
Text(0.5,1,'Quadratic')
In [13]:
plt.figure('lin quad')
plt.clf()
plt.plot(mySamples, myLinear)

plt.figure('cube expo')
plt.clf()
plt.plot(mySamples, myCubic)
plt.plot(mySamples, myExponential)

plt.figure('cube expo')
plt.title('Cubic vs. Exponential')

Out[13]:
Text(0.5,1,'Cubic vs. Exponential')

## Thêm chú thích¶

In [15]:
plt.figure('lin quad')
plt.clf()
plt.plot(mySamples, myLinear, label = 'linear')
plt.legend(loc = 'upper right')

plt.figure('cube expo')
plt.clf()
plt.plot(mySamples, myCubic, label = 'cubic')
plt.plot(mySamples, myExponential, label = 'exponential')
plt.legend() # use best location
plt.title('Cubic vs. Exponential')

Out[15]:
Text(0.5,1,'Cubic vs. Exponential')

## Các tuỳ chỉnh khác¶

### Màu và kiểu đường¶

In [16]:
plt.figure('lin quad')
plt.clf()
plt.plot(mySamples, myLinear, 'b-', label = 'linear')
plt.legend(loc = 'upper right')

plt.figure('cube expo')
plt.clf()
plt.plot(mySamples, myCubic, 'g^', label = 'cubic')
plt.plot(mySamples, myExponential, 'r--', label = 'exponential')
plt.legend() # use best location
plt.title('Cubic vs. Exponential')

Out[16]:
Text(0.5,1,'Cubic vs. Exponential')

### Kích thước đường¶

In [17]:
plt.figure('lin quad')
plt.clf()
plt.plot(mySamples, myLinear, 'b-', label = 'linear', linewidth = 2.0)
plt.legend(loc = 'upper right')

plt.figure('cube expo')
plt.clf()
plt.plot(mySamples, myCubic, 'g^', label = 'cubic', linewidth = 4.0)
plt.plot(mySamples, myExponential, 'r--', label = 'exponential', linewidth = 5.0)
plt.legend() # use best location
plt.title('Cubic vs. Exponential')

Out[17]:
Text(0.5,1,'Cubic vs. Exponential')

### Subplot¶

In [21]:
plt.figure('lin quad')
plt.clf()
plt.subplot(211)
plt.ylim(0, 900)
plt.plot(mySamples, myLinear, 'b-', label = 'linear', linewidth = 2.0)
plt.subplot(212)
plt.ylim(0, 900)
plt.legend(loc = 'upper right')

plt.figure('cube expo')
plt.clf()
plt.subplot(121)
plt.ylim(0, 140000)
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth = 4.0)
plt.subplot(122)
plt.ylim(0, 140000)
plt.plot(mySamples, myExponential, 'r', label = 'exponential', linewidth = 5.0)
plt.legend() # use best location
plt.title('Cubic vs. Exponential')

Out[21]:
Text(0.5,1,'Cubic vs. Exponential')

### Thay đổi scale¶

In [20]:
plt.figure('cube exp log')
plt.clf()
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth = 2.0)
plt.plot(mySamples, myExponential, 'r', label = 'exponential', linewidth = 4.0)
plt.yscale('log')
plt.legend()
plt.title('Cubic vs. Exponential')

plt.figure('cube exp linear')
plt.clf()
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth = 2.0)
plt.plot(mySamples, myExponential, 'r', label = 'exponential', linewidth = 4.0)
plt.legend() # use best location
plt.title('Cubic vs. Exponential')

Out[20]:
Text(0.5,1,'Cubic vs. Exponential')

## Ví dụ¶

In [22]:
def retire(monthly, rate, terms):
savings = [0]
base = [0]
mRate = rate/12
for i in range(terms):
base += [i]
savings += [savings[-1]*(1+mRate) + monthly]
return base, savings

def displayRetireWMonthlies(monthlies, rate, terms):
plt.figure('retireMonth')
plt.clf()
for monthly in monthlies:
xvals, yvals = retire(monthly, rate, terms)
plt.plot(xvals, yvals, label = 'retire:'+str(monthly))
plt.legend(loc='upper left')

displayRetireWMonthlies([500, 600, 700, 800, 900, 1000, 1100], .05, 40*12)

In [23]:
def displayRetireWRates(month, rates, terms):
plt.figure('retireRate')
plt.clf()
for rate in rates:
xvals, yvals = retire(month, rate, terms)
plt.plot(xvals, yvals, label = 'retire:'+str(month)+':'+str(int(rate*100)))
plt.legend(loc = 'upper left')

displayRetireWRates(800, [.03, .05, .07], 40*12)

In [24]:
def displayRetireWMonthsAndRates(monthlies, rates, terms):
plt.figure('retireBoth')
plt.clf()
plt.xlim(30*12, 40*12)
for monthly in monthlies:
for rate in rates:
xvals, yvals = retire(monthly, rate, terms)
plt.plot(xvals, yvals, label = 'retire:'+str(monthly)+':'+str(int(rate*100)))
plt.legend(loc = 'upper left')

displayRetireWMonthsAndRates([500, 700, 900, 1100],
[.03, .05, .07],
40*12)

In [25]:
def displayRetireWMonthsAndRates(monthlies, rates, terms):
plt.figure('retireBoth')
plt.clf()
plt.xlim(30*12, 40*12)
monthLabels = ['r', 'b', 'g', 'k']
rateLabels = ['-', 'o', '-']
for i in range(len(monthlies)):
monthly = monthlies[i]
monthLabel = monthLabels[i%len(monthLabels)]
for j in range(len(rates)):
rate = rates[j]
rateLabel = rateLabels[j%len(rateLabels)]

xvals, yvals = retire(monthly, rate, terms)
plt.plot(xvals, yvals, monthLabel+rateLabel, label = 'retire:'+str(monthly)+':'+str(int(rate*100)))
plt.legend(loc = 'upper left')

displayRetireWMonthsAndRates([500, 700, 900, 1100],
[.03, .05, .07],
40*12)