本文介绍了 pandas 箱图,在每个子图中按不同的ylim分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,我想将其绘制为:
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()
结果是我想要的,但是所有子图都具有相同的ylim.这使得无法正确地可视化结果.如何为每个子图设置不同的ylim?
The result is what I want but all the subplots have the same ylim. This makes impossible to visualize the result properly. How can I set different ylim for each subplot?
推荐答案
您要的是分别为每个轴设置y轴.我认为应该是ax.set_ylim([a, b])
.但是每次我为每个轴运行它时,所有轴都会更新.
What you asked for was to set the y axis separately for each axes. I believe that should be 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')
这篇关于 pandas 箱图,在每个子图中按不同的ylim分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!