import matplotlib.pyplot as plt
import numpy as np
# ipython 畫圖
# 簡單線圖
# 生成測試數據
x = np.linspace(-1, 1, 50)
y = np.cos(x)
# figure創建一個繪圖對象 figsize 圖片大小
plt.figure(figsize=(10, 5))
# 畫圖
plt.plot(x, y) # 默認
# 所有繪圖對象
plt.show()
# 畫多個子圖
# 生成測試數據
x = np.linspace(-3, 3, 50)
y = np.cos(x)
y1 = 2 * x + 1
y2 = np.sin(x)
# figure創建一個繪圖對象 figsize 圖片大小
plt.figure(figsize=(10, 5))
plt.subplot(221) # 第一行的左圖
plt.plot(x, y)
plt.subplot(222) # 第一行的右圖
plt.plot(x, y1)
plt.subplot(212) # 第二整行
plt.plot(x, y2)
# 所有繪圖對象
plt.show()
# 顯示標題和X、Y軸的文字和範圍
# 生成測試數據
x = np.linspace(-1, 1, 50)
y = np.cos(x)
# figure創建一個繪圖對象 figsize 圖片大小
plt.figure(figsize=(10, 5))
# 畫圖
plt.plot(x, y) # 默認
plt.xlabel("x label") # X軸的文字
plt.ylabel("y label") # Y軸的文字
plt.title("this is a title") # 圖表的標題
plt.xlim(-1.2, 1.2) # 設置Y軸的範圍
plt.ylim(-1.2, 1.2) # 設置Y軸的範圍
# 所有繪圖對象
plt.show()
# 設置X、Y軸座標轉換成字符座標
# 生成測試數據
x = np.linspace(-3, 3, 50)
y = np.cos(x)
# figure創建一個繪圖對象 figsize 圖片大小
plt.figure(figsize=(10, 5))
# 畫圖
plt.plot(x, y) # 默認
# 根據相應座標數 轉換成字符座標
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi], ['$-\pi$', '$-\pi/2$', '$0$', '$\pi/2$', '$\pi$'])
plt.yticks([-1, 0, 1], ['-one', 'zero', '+one'])
# 所有繪圖對象
plt.show()
# 在同一figure對象中畫多條線圖
# 生成測試數據
x = np.linspace(-1, 1, 50)
y = np.cos(x)
y2 = np.sin(x)
# figure創建一個繪圖對象 figsize 圖片大小
plt.figure(figsize=(10, 5))
# 畫圖
plt.plot(x, y) # 默認
plt.plot(x, y2) # 默認
# 所有繪圖對象
plt.show()
# 帶有圖示的線圖
# 生成測試數據
x = np.linspace(-1, 1, 50)
y = np.cos(x)
y2 = np.sin(x)
# label 在圖示中顯示 color 線的顏色 linewidth線的寬度 linestyle線的樣式
plt.plot(x, y, label='$cos(x)$', color='red', linewidth=1.0, linestyle='--') # 前後添加"$"符號 表示數學公式
plt.plot(x, y2, label='$sin(x)$')
# 顯示圖例 plt.plot()中label和顏色及線型
# legend 的位置參數 best/upper right/upper left/lower left/lower right/right/center left
# /center right/lower center/upper center/center
plt.legend(loc='best')
# 所有繪圖對象
plt.show()
# 設置X、Y軸的顯示隱藏和移動 字體背景的透明度
# 生成測試數據
x = np.linspace(-1, 1, 50)
y = np.cos(x)
y2 = np.sin(x)
# label 在圖示中顯示 color 線的顏色 linewidth線的寬度 linestyle線的樣式
plt.plot(x, y, label='$cos(x)$', color='red', linewidth=1.0, linestyle='--') # 前後添加"$"符號 表示數學公式
plt.plot(x, y2, label='$sin(x)$')
# 先把右邊和上邊的邊界設置爲不可見
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# 然後把下邊界和左邊界移動到0點
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
# 設置座標軸字體的透明度
ax = plt.gca()
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
label.set_bbox(dict(facecolor='blue', edgecolor='none', alpha=0.7)) # facecolor 前面顏色 edgecolor邊框線 alpha透明度
# 所有繪圖對象
plt.show()
# 給X、Y軸的座標添加標註
# 生成測試數據
x = np.linspace(0, np.pi, 20)
y = np.sin(x)
# label 在圖示中顯示 color 線的顏色 linewidth線的寬度 linestyle線的樣式
plt.plot(x, y, label='$cos(x)$', color='red', linewidth=1.0, linestyle='--') # 前後添加"$"符號 表示數學公式
# 添加標註
x0 = np.pi / 2
y0 = np.sin(x0)
plt.scatter(x0, y0, color='black') # 顯示一個點
plt.plot([x0, x0], [0, y0], 'b--') # 在點到x軸畫出垂直線
# 標註方法1
plt.annotate('y = sin(x)' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30), textcoords='offset points',
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=-0.2'))
# 標註方法2
plt.text(x0 + 0.1, y0, 'this is a sin(x)line')
# 所有繪圖對象
plt.show()
# 全部設置完成
# 複雜線形圖
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c, s = np.cos(x), np.sin(x)
# 這裏`b-`是`color="blue",linestyle="-"`的簡寫
# `lw`=`linewidth` 的簡寫
fig = plt.figure(figsize=(8, 6), dpi=80)
plt.plot(x, c, 'b-', lw=1.5, label='cosine')
plt.plot(x, s, 'r-', lw=1, label='sine')
# 顯示圖例 plt.plot()中label和顏色及線型 loc設置位置
plt.legend(loc='upper left')
# legend 的位置參數
'''
best
upper right
upper left
lower left
lower right
right
center left
center right
lower center
upper center
center
'''
plt.xlim(-3.2, 3.2) # 設置Y軸的範圍 plt.xlim(x.min()*1.1, x.max()*1.1)
plt.ylim(-1.2, 1.2) # 設置Y軸的範圍 plt.ylim(c.min()*1.1, s.max()*1.1)
# 根據相應座標數 轉換成字符座標
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi], ['$-\pi$', '$-\pi/2$', '$0$', '$\pi/2$', '$\pi$'])
# plt.yticks([-1,0,1],['-one','zero','+one'])
# 設置座標軸的位置
# 先把右邊和上邊的邊界設置爲不可見
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# 然後把下邊界和左邊界移動到0點
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
# 添加標註
x0 = np.pi / 2
y0 = np.sin(x0)
plt.scatter(x0, y0, color='black') # 顯示一個點
plt.plot([x0, x0], [0, y0], 'b--') # 在點到x軸畫出垂直線
# 標註方法1
plt.annotate('y = sin(x)' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30), textcoords='offset points',
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=-0.2'))
# 標註方法2
plt.text(x0 + 0.1, y0, 'this is a sin(x)line')
# 設置座標軸字體的透明度
ax = plt.gca()
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
label.set_bbox(dict(facecolor='white', edgecolor='none', alpha=0.7))
plt.show()
show1:
show2:
show3:
show4:
show5:
show 6:
show7:
show8:
show 9: