matplotlib import matplotlib pyplot as plt plotLx Ly
使用matplotlib畫圖
import matplotlib. pyplot as plt. plot(Lx, Ly) plt. plot(Lx 2, Ly 2, 'ro') plt. savefig('sinx. png')
import matplotlib. pyplot as plt N=10; dx=2/N S 1=0; S 2=0; Lx=[]; Ly=[] for i in range(0, N+1): x 1=dx*i; x 2=dx*(i+1) y 1=x 1**2; y 2=x 2**2 if(i < N): S 1=S 1+dx*y 1 if(i < N): S 2=S 2+dx*y 2 Lx. append(x 1); Ly. append(y 1) print (N, ' area=', S 1, S 2) plt. plot(Lx, Ly, 'b-o') plt. plot([0, 2. 5], [0, 0], 'k-') for i in range(N+1): plt. plot([Lx[i], Lx[i]], [0, Ly[i]], 'k-') for i in range(N): plt. plot([Lx[i], Lx[i]], [Ly[i], Ly[i+1]], 'g-') for i in range(N): plt. plot([Lx[i], Lx[i+1]], [Ly[i], Ly[i]], 'k-') plt. plot([Lx[i], Lx[i+1]], [Ly[i+1], Ly[i+1]], 'g-') plt. savefig("plot-1. png") print("plot is done")
import matplotlib. pyplot as plt N=10; dx=5/N S 1=0; S 2=0; Lx=[]; Ly=[] for i in range(0, N+1): x 1=dx*i; x 2=dx*(i+1) y 1=(25 -x 1**2)**0. 5; y 2=(25 -x 2**2)**0. 5 if(i < N): S 1=S 1+dx*y 1 if(i < N): S 2=S 2+dx*y 2 Lx. append(x 1); Ly. append(y 1) print (N, ' area=', S 1, S 2) plt. plot(Lx, Ly, 'b-o') plt. plot([0, 5. 5], [0, 0], 'k-') for i in range(N+1): plt. plot([Lx[i], Lx[i]], [0, Ly[i-1]], 'k-') for i in range(N): plt. plot([Lx[i], Lx[i]], [0, Ly[i]], 'g-') for i in range(N): plt. plot([Lx[i], Lx[i+1]], [Ly[i], Ly[i]], 'k-') plt. plot([Lx[i], Lx[i+1]], [Ly[i+1], Ly[i+1]], 'g-') plt. axis("square") plt. savefig("plot-1. png") print("plot is done")
import matplotlib. pyplot as plt N=20; dx=2/N Lx=[]; Ly=[] for i in range(N+1): x=dx*i y=2**x Lx. append(x); Ly. append(y) plt. plot(Lx, Ly) plt. title("y=2**x") plt. savefig('plot-2. png') print('plot done') import matplotlib. pyplot as plt N=20; dx=2/N Lx=[]; Ly=[] for i in range(N+1): x=dx*i y=2**x Lx. append(x); Ly. append(y) plt. plot(Lx, Ly, 'ro') plt. title("y=2**x") plt. savefig('plot-2. png') print('plot done')
import matplotlib. pyplot as plt N=20; dx=2/N Lx=[]; Ly=[] for i in range(N+1): x=dx*i y=2**x Lx. append(x); Ly. append(y) plt. plot(Lx, Ly) plt. title("y=2**x") plt. savefig('plot-2. png') print('plot done') import matplotlib. pyplot as plt N=20; dx=2/N Lx=[]; Ly=[]; Lx 2=[]; Ly 2=[] for i in range(N+1): x=dx*i y=2**x Lx. append(x); Ly. append(y) Lx 2. append(x); Ly 2. append(2**(-x)) plt. plot(Lx, Ly, 'r-', label="2**x") plt. plot(Lx 2, Ly 2, 'b--', label=r"$2^{-x}$") plt. legend() plt. savefig('plot-2. png') print('plot done')
import matplotlib. pyplot as plt N=5; dx=2/N Lx=[]; Ly=[] for i in range(N+1): x=dx*i y=x**3 -2*x**2 Lx. append(x); Ly. append(y) plt. title(r"$x^2$") plt. plot(Lx, Ly, 'r-', label="line") plt. plot(Lx, Ly, 'ko', label="dot") plt. legend() plt. savefig('plot-2. png') import matplotlib. pyplot as plt N=5; dx=2/N; Lx=[]; Ly=[] for i in range(N+1): x=dx*i y=x**3 -2*x**2 Lx. append(x); Ly. append(y) plt. plot(Lx, Ly, 'r-', label=r"$x^3 -2 x^2$") for i in range(1, N+1): vx=Lx[i]-Lx[i-1] vy=Ly[i]-Ly[i-1] plt. plot([0, vx], [0, vy], label=str(i)) plt. plot([-0. 1, 2. 1], [0, 0], 'g-') plt. legend() plt. savefig('plot-3. png') print('plot done')
import matplotlib. pyplot as plt places = ['Taipei', 'Taichung', 'Tainan', 'East', 'Oversea'] students = [13, 22, 18, 5, 3] #dx=0. 3 #x=[0+dx, 1+dx, 2+dx, 3+dx, 4+dx] N=sum(students) print('N=', N) plt. bar(places, students, 0. 2) #plt. bar(x, students, 0. 1, color='r') plt. savefig('bar-plot-0. png') print('plot done') dx=0. 5 plt. bar(x, students, 0. 2, color='g')
import matplotlib. pyplot as plt N=5; dx=2/N; S=0. Lx=[]; Ly 2=[0]; Lh=[0] for i in range(N+1): x=dx*i; y=x**3 -2*x**2 Lx. append(x); Ly. append(y) for i in range(1, N+1): d=((Lx[i]-Lx[i-1])**2 +(Ly[i]-Ly[i-1])**2)**0. 5 S+=d Ly 2. append(S); Lh. append(d) plt. plot(Lx, Ly, 'r-', label=r"$x^3 -2 x^2$") plt. plot(Lx, Ly 2, 'ko', label="S") plt. plot([-0. 1, 2. 1], [0, 0], 'g-') plt. bar(Lx, Lh, 0. 1) plt. legend() plt. savefig('plot-4. png') print('plot done')
- Slides: 10