[英]Changing axis ticks in Matplotlib with multiple connected Boxplots
我正在繪制一個收斂圖並顯示與我使用連接箱線圖的平均值的偏差:
出於某種原因,Matplotlib 為每個箱線圖強制打勾,我似乎無法將它們刪除。 我當前 plot 的代碼如下所示:
label = ["" for i in range(160)]
no_labels = int(np.floor(len(label)/20))
for i in range(no_labels):
label[i*20] = str(i*no_samples/no_labels)
# Weird behaviour for the last label so adding it manually
label[-1] = no_samples
fig = plt.figure(figsize=(10,5))
ax = fig.add_axes([0,0,1,1])
ax.set_xlabel("Samples", labelpad=10)
ax.set_ylabel("Error (MSE)", labelpad=10)
ax.set_ylim(0, 0.11)
ax.boxplot(data, flierprops=flyprops, showcaps=False,
boxprops=colorprops, whiskerprops={'color' : 'tab:blue'},
labels=label, patch_artist=True)
我嘗試了多種操作 MPL 中可用的軸刻度的方法。
1) 試圖讓 MPL 完成工作: ax.xaxis.set_major_locator(MultipleLocator(20))
2) 嘗試手動設置刻度: ax.set_xticks([list_of_ticks])
3)嘗試了解決方法
ax.xaxis.set_minor_locator(MultipleLocator(20))
# Removing major ticks, setting minor ticks
ax.xaxis.set_tick_params(which='major', size=0, width=2, direction='in')
ax.yaxis.set_tick_params(which='major', size=5, width=2, direction='in')
這些似乎都不起作用,我不確定為什么。 我認為這可能與我的label
變量有關,但如果我不以這種方式包含它,MPL 會為每個條目都包含一個軸標簽,這是一團糟。
如何在連接的箱線圖中每 1000 個條目設置一次軸刻度?
編輯:輸入數據是一個形狀為 (15, 160) 的 numpy 數組,其中有 160 個箱線圖,每個箱線圖有 15 個樣本。 3 個樣本的 5 個箱線圖的示例數據如下所示:
np.random.rand(3,5)
>>> array([[0.05942481, 0.03408175, 0.84021109, 0.27531937, 0.62428798],
[0.24658313, 0.77910387, 0.2161348 , 0.39101172, 0.14038211],
[0.40694432, 0.22979738, 0.87056873, 0.788295 , 0.29337562]])
主要問題似乎是在繪制主 plot 后需要更新刻度,以前從未如此。
( ax = fig.add_axes([0, 0, 1, 1])
也很不尋常。標准方法是fig, ax = plt.subplots(figsize=(10, 5))
讓 matplotlib a plot 周圍的空白有點靈活性。)
問題的代碼缺少一些變量和數據,但以下玩具示例應該創建類似的內容:
import numpy as np
import matplotlib.pyplot as plt
no_samples = 8000
x = np.linspace(0, no_samples, 160)
no_labels = int(np.floor(len(x) / 20))
label = [f'{i * no_samples / no_labels:.0f}' for i in range(no_labels+1)]
fig = plt.figure(figsize=(10, 5))
ax = fig.add_axes([0.1, 0.1, 0.85, 0.85])
N = 100
data = np.random.normal(np.tile(100 / (x+1000), N), 0.001).reshape(N, -1)
flyprops = {'markersize':0.01}
colorprops = None
ax.boxplot(data, flierprops=flyprops, showcaps=False,
boxprops=colorprops, whiskerprops={'color': 'tab:blue'},
patch_artist=True)
ax.set_xlabel("Samples", labelpad=10)
ax.set_ylabel("Error (MSE)", labelpad=10)
ax.set_ylim(0, 0.11)
ax.set_xticks(range(0, len(x)+1, 20))
ax.set_xticklabels(label)
plt.show()
以下是設置刻度線的示例:
import matplotlib.pyplot as plt
import numpy as np
data=np.random.rand(3,50)
fig = plt.figure(figsize=(10,5))
ax = fig.add_axes([0,0,1,1])
ax.set_xlabel("Samples", labelpad=10)
ax.set_ylabel("Error (MSE)", labelpad=10)
ax.boxplot(data,
showcaps=False,
whiskerprops={'color' : 'tab:blue'},
patch_artist=True
)
plt.xticks([10, 20, 30, 40, 50],
["10", "20", "30", "40", "50"])
編輯:您還可以避免弄亂字符串並像這樣設置標記:
N=50
plt.xticks(np.linspace(0, N, num=6), np.linspace(0, N, num=6))
可以使用與此處類似的方式獲得簡單的刻度(注意data
為轉置的 numpy 數組)
import numpy as np
import matplotlib.pyplot as plt
data = np.array([ np.random.rand(100) for i in range(3) ]).T
plt.boxplot(data)
plt.xticks([1, 2, 3], ['mon', 'tue', 'wed'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.