pyplot
方法 vs subplot
方法tight_layout
figure
函數¶plt.figure(num, figsize, facecolor)
'''
num = 數字, 指定第幾張圖表
figsize = 圖表寬高
facecolor = 圖表背景顏色
'''
# 範例 - 設定畫布大小
import matplotlib.pyplot as plt
import numpy as np
y = np.random.randint(10, 20, 10)
print(y)
plt.figure(figsize=(10, 5))
plt.plot(y)
plt.show()
[14 12 18 11 15 17 16 16 13 17]
# 範例 - 設定背景顏色
import matplotlib.pyplot as plt
import numpy as np
y = np.random.randint(10, 20, 10)
print(y)
plt.figure(figsize=(10, 5), facecolor='#aaa')
plt.plot(y, color='#666')
plt.show()
[18 11 12 14 19 15 16 12 11 12]
#範例 - 繪製多張圖表
import matplotlib.pyplot as plt
import numpy as np
chart1_y = np.random.randint(10, 20, 10)
print(chart1_y)
plt.figure(num=1, figsize=(5,5))
plt.plot(chart1_y)
plt.title('Chart 1')
plt.show()
chart2_y = np.random.randint(10, 20, 10)
print(chart2_y)
plt.figure(num=2, figsize=(10,5))
plt.plot(chart2_y)
plt.title('Chart 2')
plt.show()
[18 18 12 13 17 11 19 11 13 14]
[10 18 15 15 13 12 12 13 15 12]
## 範例 - 1*2的子圖設定
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
plt.subplot(1, 2, 1)
plt.plot(num)
plt.subplot(1, 2, 2)
plt.scatter(range(0, 10), num)
plt.show()
[9 4 8 5 1 8 8 3 7 3]
## 範例 - 加大畫布
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
plt.figure(figsize=(20, 10))
plt.subplot(1, 2, 1)
plt.plot(num)
plt.subplot(1, 2, 2)
plt.scatter(range(0, 10), num)
plt.show()
[6 1 9 2 7 1 8 6 4 1]
## 範例 - 2*2的子圖設定
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
plt.subplot(2, 2, 1)
plt.plot(num)
plt.subplot(2, 2, 2)
plt.plot(num)
plt.subplot(2, 2, 3)
plt.plot(num)
plt.show()
[8 4 7 7 4 4 4 9 5 4]
## 範例 - 2*2的子圖設定
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
plt.subplot(2, 2, 1)
plt.plot(num)
plt.subplot(2, 2, 2)
plt.plot(num)
plt.subplot(2, 1, 2) #注意設定方式
plt.plot(num)
plt.show()
[6 1 5 5 7 5 5 8 7 6]
10
個介於1~10
之間的數, 繪製圖片如下
plt.suptitle('text', fontsize=number, c='color')
## 範例
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
plt.figure(figsize=(10, 5))
plt.subplot(2, 2, 1)
plt.plot(num)
plt.title('Chart one')
plt.subplot(2, 2, 2)
plt.plot(num)
plt.title('Chart two')
plt.subplot(2, 1, 2)
plt.plot(num)
plt.title('Chart three')
plt.suptitle('main title', fontsize=20, c='r')
plt.show()
[2 6 7 5 5 9 5 3 7 6]
plt.subplot
時, 則matplotlib
會幫我們產生subplot
的物件. 我們可透過此物件進行繪圖## 範例
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
chart1 = plt.subplot(1, 2, 1) #注意寫法
chart1.plot(num)
chart2 = plt.subplot(1, 2, 2) #注意寫法
chart2.scatter(range(0, 10), num)
plt.show()
[6 8 2 3 4 5 9 4 5 6]
pyplot 方法 vs subplot方法
¶#pyplot #subplot
xlabel = set_xlabel
ylabel = set_ylabel
axis = axis
xlim = set_xlim
ylim = set_ylim
title = set_title
xticks = xaxis.set_ticks
yticks = yaxis.set_ticks
## 範例
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
chart1 = plt.subplot(1, 2, 1)
chart1.plot(num)
chart1.set_title('Chart1') #object API
chart1.axis([1, 10, 1, 10]) #object API
chart1.set_xlabel('value') #object API
chart2 = plt.subplot(1, 2, 2)
chart2.scatter(range(0, 10), num)
plt.title('Chart2') #plot API
plt.show()
[3 5 7 3 7 5 3 1 8 4]
subplot
物件方法plt.subplot(row, column, index, sharex=subplot_object, sharey=subplot_object)
# 範例 - subplot縮寫/共享y軸
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
chart1 = plt.subplot(1, 2, 1)
chart1.plot(num)
chart1.set_title('Chart1')
chart1.axis([1, 10, 1, 10])
chart2 = plt.subplot(122, sharey=chart1)
chart2.scatter(range(0, 10), num)
chart2.set_title('Chart2')
plt.show()
[9 9 2 4 8 5 2 6 4 7]
# 範例 - subplot縮寫/共享x軸
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
plt.figure(figsize=(10, 5))
chart1 = plt.subplot(211)
chart1.plot(num)
chart1.set_title('Chart1')
chart1.set_xlim(1, 10)
chart1.tick_params(axis='x', labelbottom=False) #注意寫法
chart2 = plt.subplot(212, sharex=chart1)
chart2.plot(range(0, 10), num)
chart2.set_title('Chart2')
plt.show()
[5 6 9 6 6 9 8 8 4 9]
sin, cos
圖形如下
# 範例
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
#plt.figure(figsize=(10, 5))
chart1 = plt.subplot(211)
plt.plot(num)
plt.title('Chart1')
chart2 = plt.subplot(212, sharex=chart1)
plt.plot(range(0, 10), num)
plt.title('Chart2')
plt.tight_layout() ## 設定layout
plt.show()
[5 4 2 4 4 2 5 7 2 1]
## 範例 - rcParams
import matplotlib.pyplot as plt
import numpy as np
num = np.random.randint(1, 10, 10)
print(num)
plt.rcParams["figure.autolayout"] = True #等同設定figure_layout效果
#plt.figure(figsize=(10, 5))
chart1 = plt.subplot(211)
plt.plot(num)
plt.title('Chart1')
chart2 = plt.subplot(212, sharex=chart1)
plt.plot(range(0, 10), num)
plt.title('Chart2')
plt.show()
[5 3 5 6 9 6 4 1 5 9]
figure_layout
方法