pandas 二次Y轴用于箱线图 [英] Pandas secondary y axis for boxplots

查看:74
本文介绍了 pandas 二次Y轴用于箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在熊猫的一些箱形图中使用辅助y轴,但似乎不可用。

I'd like to use a secondary y-axis for some boxplots in pandas, but it doesn't seem available.

import numpy as np
import pandas as pd

data = np.random.random((10, 5))
data[:,-1] += 10  # offset one column
df = pd.DataFrame(data)

现在,使用默认线图可以很容易地绘制到第二个y轴:

Now, using the default line plot it's easy enough to plot to a second y-axis:

df.plot(kind='line', secondary_y=[4])

但是如果我使用boxplot样式,则不起作用:

But if I use boxplot style, it doesn't work:

df.plot(kind='box', secondary_y=[4])

有什么办法(也许通过matplotlib)我可以让熊猫为箱线图绘制2个轴吗?最好使用 boxplot()方法,但我将采用 plot()。谢谢。

Is there any way (maybe through matplotlib) I can get pandas to plot 2 axes for boxplot? Using the boxplot() method would be preferable, but I'll take plot(). Thanks.

推荐答案

实际上,使用matplotlib并不难。

Actually not that hard with matplotlib.

In [22]: fig, ax = plt.subplots()

In [23]: df[[0, 1, 2, 3]].plot(kind='box', ax=ax)
Out[23]: <matplotlib.axes._subplots.AxesSubplot at 0x4890978>

In [24]: ax2 = ax.twinx()

In [25]: ax2.boxplot(df[4], positions=[4])

In [27]: ax.set_xlim(0, 5)
Out[27]: (0, 5)

样式,刻度标签等仍有一些清理工作。但这应该可以帮助您开始。

There's still some cleanup with the styling, ticklabels etc. But that should get you started.

这篇关于 pandas 二次Y轴用于箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆