如何獲取要繪製的 matplotlib Axes 例項?
為了獲取軸例項,我們將使用 subplots() 方法。
步驟
製作一份年份列表。
製作一份該年份的人口列表。
使用 np.arrange(len(years)) 方法獲取標籤數。
設定條形的寬度。
使用 subplots() 方法建立 fig 和 ax 變數,其中預設 nrows 和 ncols 均為 1。
使用 set_ylabel() 設定圖的 Y 軸標籤。
使用 set_title() 方法設定圖的標題。
使用 set_xticks 方法,設定 x 刻度,使用步驟 3 中建立的 x。
使用 set_xticklabels 方法,設定 x 刻度標籤,使用 years 資料。
使用 plt.show() 方法顯示此圖。
示例
from matplotlib import pyplot as plt
import numpy as np
years = [1901, 1911, 1921, 1931, 1941, 1951, 1961, 1971, 1981, 1991, 2001, 2011]
population = [237.4, 238.4, 252.09, 251.31, 278.98, 318.66, 361.09, 439.23, 548.16, 683.33, 846.42, 1028.74]
x = np.arange(len(years)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots() # axes instance
ax.set_ylabel('Population(in million)')
ax.set_title('Years')
ax.set_xticks(x)
ax.set_xticklabels(years)
plt.show()輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP