Matplotlib 中重疊的 Y 軸刻度標籤和 X 軸刻度標籤
為了減少在 matplotlib 中重疊的可能性 x 和 y 刻度標籤,我們可以採取以下步驟 -
使用 numpy 建立 x 和 y 資料點。
在索引 1 處(nrows=1 和 ncols=2)向當前圖形新增子圖。
將 x 和 y 邊距設定為 0。
繪製 x 和 y 資料點,併為此子圖新增一個標題,即“重疊”。
在索引 2 處(nrows=1 和 ncols=2)向當前圖形新增子圖。
將 x 和 y 邊距設定為 0。
繪製 x 和 y 資料點,併為此子圖新增一個標題,即“不重疊”。
MaxNLocator 和 prune ="lower" 的目標是最小的刻度將被移除。
要顯示圖形,請使用 show() 方法。
範例
import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True xs = np.linspace(0, 5, 10) ys = np.linspace(0, 5, 10) plt.subplot(121) plt.margins(x=0, y=0) plt.plot(xs, ys) plt.title("Overlapping") plt.subplot(122) plt.margins(x=0, y=0) plt.plot(xs, ys) plt.title("Non overlapping") plt.gca().xaxis.set_major_locator(MaxNLocator(prune='lower')) plt.gca().yaxis.set_major_locator(MaxNLocator(prune='lower')) plt.show()
輸出
廣告