Skip to content

Commit d32fa83

Browse files
committed
correct day18 homework and upload day19 homework
1 parent 4518cf6 commit d32fa83

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

D18_matplotlib/homework.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@
1010
#Correction
1111
#畫出完整的 cos 圖形
1212
x = np.arange(0, 3 * np.pi, 0.1)
13-
y_cos = np.cos(x)
14-
plt.plot(x, y_cos)
13+
y = np.cos(x)
14+
# or
15+
x = np.arange(0,180)
16+
y = np.cos(2*x*np.pi/180.0)
17+
#角度轉弧度
18+
x = np.deg2rad(x)
19+
20+
plt.plot(x, y)
1521

1622
# 照需要寫入x 軸和y軸的 label 以及title
1723
plt.xlabel("x-axis")
@@ -27,12 +33,16 @@
2733
plt.title("Scatter plot")
2834
plt.scatter(X, Y, color = 'gold')
2935
#correction
36+
#這邊是做三角函數的運算,把資料分成四個值, 用arc tangent把Y X 轉換成0~1的浮點數,剛好這些浮點數會對應到scatter裡面的色碼錶
3037
T = np.arctan2(Y,X)
31-
plt.scatter(X,Y, s=75, c=T, alpha=.5)
38+
#https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.pyplot.scatter.html
39+
#alpha: 範圍從 0 - 1,數字越大代表越透明
40+
plt.scatter(X,Y, s=75, c=T, alpha=.5)
41+
#設定各個軸的極限
3242
plt.xlim(-1.5,1.5)
3343
plt.ylim(-1.5,1.5)
3444

35-
# 如果要存成圖形檔:
45+
# 如果要存成圖形檔:
3646
# 把 pyplot.show() 換成下面這行:
3747
plt.savefig("Scatter.png",dpi=300,format="png")
3848
plt.show()

D19_differentFig/homework.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pandas as pd
2+
import matplotlib.pyplot as plt #https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.html
3+
import numpy as np
4+
5+
# 條型圖:Bar Plots
6+
7+
# 長條圖主要用來呈現兩個維度的資料,一個為X軸另一個則為Y軸(當然這邊指的是二維的狀況,較為常見)
8+
# 主要用來呈現兩個維度的資料
9+
# 問題:嘗試通過添加紅色條形標籤重現右側的圖形。
10+
plt.axes([0.1,0.1,.5,.5], facecolor='red')
11+
# Set locations and labels
12+
plt.xticks([]), plt.yticks([])
13+
plt.text(0.1,0.1, 'axes([0.1,0.1,.5,.5])',ha='left',va='center',size=16,alpha=.5)
14+
plt.show()
15+
16+
# 軸圖進階
17+
18+
# 但是可以將圖放置在圖中的任何位置。因此,如果要在較大的圖中放置較小的圖,則可以使用軸。
19+
# 特別提醒:tick 刻度線定位器
20+
# 問題:使用 tick
21+
22+
23+
#配置12 組 Bar
24+
n = 12
25+
X = np.arange(n)
26+
27+
#給定數學運算式
28+
Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
29+
Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
30+
31+
#指定上半部繪製區域, 給定 Bar 顏色, 邊界顏色, https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.bar.html
32+
plt.bar(X, +Y1, facecolor='red', edgecolor='white')
33+
# +Y 指的是 XY 四象限的第一象限
34+
plt.bar(X, -Y2, facecolor='#1111ff', edgecolor='white')
35+
36+
37+
#設定繪圖圖示區間
38+
for x,y in zip(X,Y1):
39+
plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
40+
41+
#設定Y軸區間
42+
plt.ylim(-1.25,+1.25)
43+
plt.show()

0 commit comments

Comments
 (0)