[英]Matplotlib, shift boxplots along x-axis?
我正在沿着兩個不同的軸繪制多個箱形圖。 我的代碼如下:
fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=False)
data_1 = [array1, array2, array3]
ax1.boxplot(data_1, whis=[5,95], showfliers=True)
data_2 = [array4, array5]
ax2.boxplot(data_2, whis=[5,95], showfliers=True)
ax2.set_xlim(0,4)
這將產生一個如下圖(代替我的實際數據):
但是,我希望較低的繪圖(在ax2上)沿x軸向右移動一個單位。 也就是說,我希望在x = 2和x = 3處有2個下部箱形圖,以便它們與第2個和第3個上部箱形圖對齊。 我想對所有x軸保持相同的xlabel。
有任何想法嗎?
這應該適用於您的示例代碼。 但是,此解決方案繞過了sharex
我認為,使用箱形圖和sharex
時,軸標簽有點不直觀。
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
np.random.seed(42)
# create random data
for i in range(1,6):
x = np.random.rand(10)
exec("array%s = x" % i)
widths = 0.3
fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=False)
data_1 = [array1, array2, array3]
ax1.boxplot(data_1, widths=0.3, whis=[5,95], showfliers=True)
data_2 = [array4, array5]
positions = [2,3]
ax2.boxplot(data_2, positions=positions, widths=widths, whis=[5,95], showfliers=True)
ax2.set_xticks([1,2,3])
ax1.set_xticks([1,2,3])
ax2.set_xticklabels([1,2,3])
plt.xlim(0,4)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.