简体   繁体   English

pandas boxplot,groupby在每个子图中的不同ylim

[英]pandas boxplot, groupby different ylim in each subplot

I have a dataframe and I would like to plot it as: 我有一个数据框,我想将其绘制为:

>>> 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()

The result is what I want but all the subplots have the same ylim. 结果是我想要的,但所有的子图都有相同的ylim。 This makes impossible to visualize the result properly. 这使得无法正确地显示结果。 How can I set different ylim for each subplot? 如何为每个子图设置不同的ylim?

What you asked for was to set the y axis separately for each axes. 您要求的是为每个轴分别设置y轴。 I believe that should be ax.set_ylim([a, b]) . 我相信应该是ax.set_ylim([a, b]) But every time I ran it for each axes it updated for all. 但每当我为每个轴运行它时,它都会更新。

Because I couldn't figure out how to answer your question directly, I'm providing a work around. 因为我无法弄清楚如何直接回答你的问题,我正在提供一个解决方案。

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')

subplots DOCUMENTATION subplots DOCUMENTATION

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM