我有一个数据框,我想将其绘制为:
>>> X = pd.DataFrame(np.random.normal(0, 1, (100, 3)))
>>> X['NCP'] = np.random.randint(0, 5, 100)
>>> X[X['NCP'] == 0] += 100
>>> X.groupby('NCP').boxplot()
结果是我想要的,但所有的子图都有相同的ylim。这使得无法正确地显示结果。如何为每个子图设置不同的ylim?
答案 0 :(得分:1)
您要求的是为每个轴分别设置y轴。我认为应该是ax.set_ylim([a, b])
。但每次我为每个轴运行它都会为所有轴更新。
因为我无法弄清楚如何直接回答你的问题,所以我提供了解决方法。
X = pd.DataFrame(np.random.normal(0, 1, (100, 3)))
X['NCP'] = np.random.randint(0, 5, 100)
X[X['NCP'] == 0] += 100
groups = X.groupby('NCP')
print groups.groups.keys()
# This gets a number of subplots equal to the number of groups in a single
# column. you can adjust this yourself if you need.
fig, axes = plt.subplots(len(groups.groups), 1, figsize=[10, 12])
# Loop through each group and plot boxplot to appropriate axis
for i, k in enumerate(groups.groups.keys()):
group = groups.get_group(k)
group.boxplot(ax=axes[i], return_type='axes')