我有一系列的箱形图,我想以xticks为中心(特别是每xtick 2个)。请考虑以下事项:
# fake up some more data
spread= rand(50) * 100
center = ones(25) * 40
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
d2 = concatenate( (spread, center, flier_high, flier_low), 0 )
data.shape = (-1, 1)
d2.shape = (-1, 1)
#data = concatenate( (data, d2), 1 )
# Making a 2-D array only works if all the columns are the
# same length. If they are not, then use a list instead.
# This is actually more efficient because boxplot converts
# a 2-D array into a list of vectors internally anyway.
data = [data, d2, d2[::2,0]]
# multiple box plots on one figure
figure()
boxplot(data)
哪个产生
然而,我想有6个箱图,其中2个居中于1,2左右,等等...如果我再添加3个,只需将它们添加到4,5,6 ...任何帮助将不胜感激
编辑通过“居中”来表达我的意思。我想在xtick标记为“1”左侧的一个箱形图,而另一个在右侧。它们可能会在y范围内重叠,因此我不希望它们相互叠加。
答案 0 :(得分:6)
要控制箱图的x位置,请使用positions
kwarg。
例如:
import numpy as np
import matplotlib.pyplot as plt
dists = [np.random.normal(i, 1, 100) for i in range(0, 10, 2)]
fig, ax = plt.subplots()
ax.boxplot(dists, positions=[0, 1, 2, 0, 1])
plt.show()
如果您希望并排分组,则需要自己计算职位。一种方法可能是这样的:
def grouped_boxplots(data_groups, ax=None, max_width=0.8, pad=0.05, **kwargs):
if ax is None:
ax = plt.gca()
max_group_size = max(len(item) for item in data_groups)
total_padding = pad * (max_group_size - 1)
width = (max_width - total_padding) / max_group_size
kwargs['widths'] = width
def positions(group, i):
span = width * len(group) + pad * (len(group) - 1)
ends = (span - width) / 2
x = np.linspace(-ends, ends, len(group))
return x + i
artists = []
for i, group in enumerate(data_groups, start=1):
artist = ax.boxplot(group, positions=positions(group, i), **kwargs)
artists.append(artist)
ax.margins(0.05)
ax.set(xticks=np.arange(len(data_groups)) + 1)
ax.autoscale()
return artists
作为使用它的一个简单例子:
data = [[np.random.normal(i, 1, 30) for i in range(2)],
[np.random.normal(i, 1.5, 30) for i in range(3)],
[np.random.normal(i, 2, 30) for i in range(4)]]
grouped_boxplots(data)
plt.show()
......只是为了展示一个过于花哨的例子:
import numpy as np
import matplotlib.pyplot as plt
def main():
data = [[np.random.normal(i, 1, 30) for i in range(2)],
[np.random.normal(i, 1.5, 30) for i in range(3)],
[np.random.normal(i, 2, 30) for i in range(4)]]
fig, ax = plt.subplots()
groups = grouped_boxplots(data, ax, max_width=0.9,
patch_artist=True, notch=True)
colors = ['lavender', 'lightblue', 'bisque', 'lightgreen']
for item in groups:
for color, patch in zip(colors, item['boxes']):
patch.set(facecolor=color)
proxy_artists = groups[-1]['boxes']
ax.legend(proxy_artists, ['Group A', 'Group B', 'Group C', 'Group D'],
loc='best')
ax.set(xlabel='Year', ylabel='Performance', axisbelow=True,
xticklabels=['2012', '2013', '2014'])
ax.grid(axis='y', ls='-', color='white', lw=2)
ax.patch.set(facecolor='0.95')
plt.show()
def grouped_boxplots(data_groups, ax=None, max_width=0.8, pad=0.05, **kwargs):
if ax is None:
ax = plt.gca()
max_group_size = max(len(item) for item in data_groups)
total_padding = pad * (max_group_size - 1)
width = (max_width - total_padding) / max_group_size
kwargs['widths'] = width
def positions(group, i):
span = width * len(group) + pad * (len(group) - 1)
ends = (span - width) / 2
x = np.linspace(-ends, ends, len(group))
return x + i
artists = []
for i, group in enumerate(data_groups, start=1):
artist = ax.boxplot(group, positions=positions(group, i), **kwargs)
artists.append(artist)
ax.margins(0.05)
ax.set(xticks=np.arange(len(data_groups)) + 1)
ax.autoscale()
return artists
main()