如何在 Matplotlib 中顯示所有標籤值?
要顯示所有標籤值,我們可以使用set_xticklabels() 和set_yticklabels() 方法。
步驟
建立一個可以用於標記軸的數字列表 (x)。
使用 subplot() 獲取軸,該方法有助於將子圖新增到當前圖形。
使用 set_xticks 和 set_yticks 方法分別在 X 和 Y 軸上設定刻度,並列出步驟 1 中的 x。
使用set_xticklabels()和set_yticklabels()設定刻度標籤,其中標籤列表為 (["one", "two", "three", "four"]),旋轉角度為 45 度。
為了在座標軸和刻度標籤之間新增間距,我們可以使用帶有pad 引數的tick_params()方法,該引數有助於新增間距。引數方向 (in) 有助於將刻度標籤放在座標軸內,而軸 (both) 將引數應用於兩個座標軸。
要顯示圖形,請使用 plt.show()方法。
示例
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot() ax1.set_xticks(x) ax1.set_yticks(x) ax1.set_xticklabels(["one", "two", "three", "four"], rotation=45) ax1.set_yticklabels(["one", "two", "three", "four"], rotation=45) ax1.tick_params(axis="both", direction="in", pad=15) plt.show()
輸出
廣告