尽管网上有关于绘制并排箱图的好例子。通过我的数据设置在两个不同的pandas DataFrames并且已经有了总和子图的方式,我无法管理让我的箱图彼此相邻而不是重叠。
我的代码如下:
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
mpl.use('agg')
fig, axarr = plt.subplots(3,sharex=True,sharey=True,figsize=(9,6))
month = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']
percentiles = [90,95,98]
nr = 0
for p in percentiles:
future_data = pd.DataFrame(np.random.randint(0,30,size=(30,12)),columns = month)
present_data = pd.DataFrame(np.random.randint(0,30,size=(30,12)),columns = month)
Future = future_data.as_matrix()
Present = present_data.as_matrix()
pp = axarr[nr].boxplot(Present,patch_artist=True, showfliers=False)
fp = axarr[nr].boxplot(Future, patch_artist=True, showfliers=False)
nr += 1
结果如下: Overlapping Boxplots
你能帮我解决一下如何确保盒子彼此相邻,这样我就可以比较它们而不会受到重叠的影响吗?
谢谢!
编辑:我已经减少了一些代码,所以它可以像这样运行。答案 0 :(得分:2)
您需要手动定位条形图,即将位置作为数组提供给boxplot的position
参数。这里将-0.2和另一个+0.2移位到它们的整数位置是有意义的。然后,您可以调整它们的宽度,使其总和小于位置差异的值。
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
fig, axarr = plt.subplots(3,sharex=True,sharey=True,figsize=(9,6))
month = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']
percentiles = [90,95,98]
nr = 0
for p in percentiles:
future_data = pd.DataFrame(np.random.randint(0,30,size=(30,12)),columns = month)
present_data = pd.DataFrame(np.random.randint(0,30,size=(30,12)),columns = month)
Future = future_data.as_matrix()
Present = present_data.as_matrix()
pp = axarr[nr].boxplot(Present,patch_artist=True, showfliers=False,
positions=np.arange(Present.shape[1])-.2, widths=0.4)
fp = axarr[nr].boxplot(Future, patch_artist=True, showfliers=False,
positions=np.arange(Present.shape[1])+.2, widths=0.4)
nr += 1
axarr[-1].set_xticks(np.arange(len(month)))
axarr[-1].set_xticklabels(month)
axarr[-1].set_xlim(-0.5,len(month)-.5)
plt.show()