【发布时间】:2017-04-23 15:32:07
【问题描述】:
我想遍历列表并绘制每个列表的箱线图。因为数据不能全部放入内存,所以我无法分配预定义数量的箱线图来绘制,所以我使用 subplot 函数迭代地添加图。
我的问题是轴标签没有被添加到带有箱线图的图中,并且只显示了最后一个标签。如何使用 subplot 迭代地标记箱线图。
下面是我想要做的一个简化示例。尽管实际上我实际上是在循环中回收同一个列表,而不是遍历列表列表,但它可以说明问题。可以看出,yaxis 中只设置了“lb”,并且第一个底部箱线图没有显示“la”,
谢谢。
%matplotlib inline
import matplotlib.pyplot as plt
la = [24, 28, 31, 34, 38, 40, 41, 42, 43, 44]
lb = [5, 8, 10, 12, 15, 18, 21, 25, 30, 39]
names = ['la', 'lb']
myList = [la] + [lb]
myList
# set fig for boxplots
fig, ax = plt.subplots(sharex=True)
# Add a horizontal grid to the plot
ax.xaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5)
ax.set_axisbelow(True)
ax.set_title('Some Title')
for i,l in enumerate(myList):
ax.boxplot(l, vert=False, positions = [i])
ax.set_yticklabels([names[i]])
ax.set_ylim(-0.5, len(myList)-0.5)
【问题讨论】:
标签: python matplotlib plot