GitHub Viewer
"""
plot 集合 1
"""
print("------------------------------------------------------------") # 60個
# 共同
import os
import sys
import time
import math
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
font_filename = "D:/_git/vcs/_1.data/______test_files1/_font/msch.ttf"
# 設定中文字型及負號正確顯示
# 設定中文字型檔
plt.rcParams["font.sans-serif"] = "Microsoft JhengHei" # 將字體換成 Microsoft JhengHei
# 設定負號
plt.rcParams["axes.unicode_minus"] = False # 讓負號可正常顯示
plt.rcParams["font.size"] = 12 # 設定字型大小
print("------------------------------------------------------------") # 60個
plt.figure(
num="plot 集合 1",
figsize=(12, 8),
dpi=100,
facecolor="whitesmoke",
edgecolor="r",
linewidth=1,
frameon=True,
)
print("------------------------------") # 30個
plt.subplot(231)
x = np.linspace(0, 10, 101)
def random_walk(xy0=(0.0, 0.0), nsteps=100, std=1.0):
xy = np.zeros((nsteps + 1, 2))
xy[0, :] = xy0
deltas = np.random.normal(loc=0.0, scale=std, size=(nsteps, 2))
xy[1:, :] = xy[0, :] + np.cumsum(deltas, axis=0)
return xy
for cnt in range(3):
traj = random_walk()
plt.plot(traj[:, 0], traj[:, 1], label="軌跡 : {c}".format(c=cnt))
plt.legend()
plt.title("Random Walk")
print("------------------------------") # 30個
plt.subplot(232)
N = 20
x = np.linspace(0, 6.4, N)
A = 0.8 # 雜訊的振幅
y = np.sin(x) + np.random.rand(1, len(x)) * A - A / 2 # 加入雜訊的點集 # y是numpy.ndarray格式
y = y.tolist()[0] # y 由 numpy.ndarray格式 轉成list格式, 畫圖要用list格式
plt.plot(x, np.sin(x), "black") # 無雜訊
plt.plot(x, y, "r") # 有雜訊
plt.grid()
plt.title("畫雜訊範例 " + str(N) + " 點")
print("------------------------------") # 30個
plt.subplot(233)
filename = "_data/python_ReadWrite_CSV6_temperature.csv"
# 讀入氣溫資料
dat = pd.read_csv(filename, encoding="UTF-8")
n = len(dat) # 資料筆數
x = range(1, n + 1) # x軸的值(1~資料筆數)
# 氣溫
y = dat["平均氣溫"] # y軸的值(平均氣溫)
plt.plot(x, y) # 繪圖
# 區間大小:9 的移動平均
v = np.ones(9) / 9.0
y2 = np.convolve(y, v, mode="same") # 計算移動平均
plt.plot(x[4 : n - 4], y2[4 : n - 4]) # 繪圖
plt.title("繪製移動平均圖")
print("------------------------------") # 30個
plt.subplot(234)
# 描點畫圓
# 角度
th = np.arange(0, 360)
# 圓周上的點P座標
x = np.cos(np.radians(th))
y = np.sin(np.radians(th))
plt.plot(x, y, "red")
plt.axis("equal") # 軸比例
print("------------------------------") # 30個
plt.subplot(235)
# 繪製半徑300的圓(y >= 0)
# 圓的方程式
r = 300 # 半徑
x = np.arange(-r, r + 1) # x: -300~300
y = np.sqrt(r**2 - x**2) # y
plt.plot(x, y)
plt.axis("equal") # 軸比例
print("------------------------------") # 30個
plt.subplot(236)
print("畫點")
plt.plot(0, 0, "-o") # 在 (0, 1) 上 畫一點
plt.plot(1.5, 1.5, "r-o")
plt.plot(2, -2, "g-o")
plt.plot(-2, -2, "b-o")
radius = 5
degrees = np.arange(0, 360)
x = radius * np.cos(np.radians(degrees))
y = radius * np.sin(np.radians(degrees))
plt.plot(x, y)
plt.axis("equal")
plt.title("畫點 畫圓")
plt.show()
print("------------------------------------------------------------") # 60個
plt.figure(
num="plot 集合 2",
figsize=(12, 8),
dpi=100,
facecolor="whitesmoke",
edgecolor="r",
linewidth=1,
frameon=True,
)
print("------------------------------") # 30個
plt.subplot(231)
x = np.linspace(-2 * np.pi, 2 * np.pi, 100)
y = np.sinc(x)
plt.plot(x, y)
plt.margins(0.2, 0.2)
plt.title("有 margins設定 ")
print("------------------------------") # 30個
plt.subplot(232)
# 畫個函數, 標出正的部份!
# 把這個函數大於 0 的地方標示出來。
N = 30 + 1
x = np.linspace(-5, 5, N) # 含頭尾 分成N個
y = np.sinc(x)
# print(x)
# print(y)
plt.plot(x, y, "black")
plt.plot(x[y > 0], y[y > 0], "ro")
# plt.scatter(x[y>0], y[y>0], c='r')
# print(plt.axis())
plt.title("只標出正的部分")
print("------------------------------") # 30個
plt.subplot(233)
# 畫出y=a^x的函數圖形 0