Matplotlib 標籤和標題
建立圖表的標籤
使用 Pyplot,你可以使用 xlabel() 和 ylabel() 函式來設定 x 軸和 y 軸的標籤。
示例
為 x 軸和 y 軸新增標籤
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.xlabel("平均脈搏")
plt.ylabel("消耗卡路里")
plt.show()
結果

為圖表建立標題
使用 Pyplot,你可以使用 title() 函式來設定圖表的標題。
示例
新增圖表標題以及 x 軸和 y 軸的標籤
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.title("運動手錶資料")
plt.xlabel("平均脈搏")
plt.ylabel("消耗卡路里")
plt.show()
結果

設定標題和標籤的字型屬性
你可以使用 xlabel()、ylabel() 和 title() 中的 fontdict 引數來設定標題和標籤的字型屬性。
示例
設定標題和標籤的字型屬性
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
plt.title("運動手錶資料", fontdict = font1)
plt.xlabel("平均脈搏", fontdict = font2)
plt.ylabel("消耗卡路里", fontdict = font2)
plt.plot(x, y)
plt.show()
結果

定位標題
你可以使用 title() 中的 loc 引數來定位標題。
合法值是:'left'(左)、'right'(右)和 'center'(居中)。預設值為 'center'。
示例
將標題定位在左側
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.title("運動手錶資料", loc = 'left')
plt.xlabel("平均脈搏")
plt.ylabel("消耗卡路里")
plt.plot(x, y)
plt.show()
結果
