%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
plt.plot([1, 4, 3, 1, 5])
plt.show()
x = np.linspace(0, 10, 101)
y = x**2-6*x-3
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
x = np.random.rand(20, 2)
p = plt.plot(x)
plt.legend(p, ('a', 'b'))
x = np.linspace(0, 10, 21)
plt.plot(x, x, 'r^')
plt.plot(x, x**2, '--')
plt.plot(x, x**3, 'go')
plt.plot?
x = np.arange(10)
y = np.array([5, 9, 4, 8, 7, 4, 3, 1, 2, 9])
plt.bar(x, y)
x = np.random.rand(30)
y = np.random.rand(30)
plt.scatter(x, y)
x = np.linspace(0, 5, 21)
plt.figure()
plt.subplot(231)
plt.plot(x, x)
plt.subplot(232)
plt.plot(x, x**2)
plt.subplot(233)
plt.plot(x, x**3)
plt.subplot(234)
plt.plot(x, x**0.5)
plt.subplot(235)
plt.plot(x, x**0.25)
plt.subplot(236)
plt.plot(x, x*2)
x = np.linspace(-10, 10, 201)
y = 5*(x**2)-3*x+4
plt.plot(x, y)
from datetime import datetime
x = datetime.strptime('2020-06-14', '%Y-%m-%d')
print(type(x))
print(x)
datetime.now() # 當前時間
s = datetime.strftime(datetime.now(), '%Y-%m-%d')
print(s)
import matplotlib.dates as mdates
s = '''2016-06-01 782
2016-07-01 679
2016-08-01 791
2016-09-01 111
2016-10-01 1177
2016-11-01 885
2016-12-01 2054
2017-01-01 867
2017-02-01 776
2017-03-01 961
2017-04-01 926
2017-05-01 792
2017-06-01 884
2017-07-01 700
2017-08-01 872
2017-09-01 1193
2017-10-01 1063
2017-11-01 1012
2017-12-01 230
2018-01-01 859
2018-02-01 899
2018-03-01 895
2018-04-01 875
2018-05-01 979
2018-06-01 711
2018-07-01 697
2018-08-01 970
2018-09-01 1122'''
s = s.split('\n')
date = []
num = []
for i in range(len(s)):
s[i] = s[i].split()
num.append(int(s[i][1]))
date.append(datetime.strptime(s[i][0], '%Y-%m-%d'))
fig, ax = plt.subplots()
ax.xaxis.set_major_locator(mdates.YearLocator()) # 設定大刻度
ax.xaxis.set_minor_locator(mdates.MonthLocator()) # 設定小刻度
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y')) # 設定大刻度標示
plt.bar(date, num, width=10)
plt.show()