【數據分析與可視化】matplotlib簡單繪圖之subplot

# 畫子圖

# 導入庫
import pandas as pd
import numpy as np
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
# 等差數列50個值
x = np.linspace(0.0, 5.0)
# 生成兩個y軸座標
y1 = np.sin(np.pi*x)
y2 = np.sin(np.pi*x*2)
# 畫線
plt.plot(x, y1, 'b--', label='sin(pi*x)')
plt.ylabel('y1 value')
plt.plot(x, y2, 'r--', label='sin(pi*2x)')
plt.ylabel('y2 value')
plt.xlabel('x value')
plt.title('this is x-y value')
# 顯示線的label
plt.legend()
<matplotlib.legend.Legend at 0x116526d90>

在這裏插入圖片描述

# 法1:子圖
# 兩行兩列-切分 
plt.subplot(2,2,1)#也可以直接plt.subplot(221)
plt.plot(x, y1, 'b--')
plt.ylabel('y1')
plt.subplot(2,2,2)
plt.plot(x,y2,'r--')
plt.ylabel('y2')
plt.xlabel('x')
plt.subplot(2,2,3)
plt.plot(x, y1, 'b*')

在這裏插入圖片描述
[<matplotlib.lines.Line2D at 0x116769150>]

# 法2:子圖
# a[0]畫布
a = plt.subplots()

在這裏插入圖片描述

type(a)
tuple
figure, ax = plt.subplots()

在這裏插入圖片描述

ax.plot([1,2,3,4])
[<matplotlib.lines.Line2D at 0x118595a10>]
plt.show()
# 建議放在一起執行,容易重複切割 
f,a = plt.subplots(2,2)
a[0][0].plot(x,y1)
a[0][1].plot(x,y2)
plt.show()

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章