pandas boxplot中每个子图的独立轴

问题描述 投票:1回答:3

以下代码有助于获得具有独特彩色框的子图。但是所有子图共享一组共同的x和y轴。我期待每个子图有独立的轴:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch

df = pd.DataFrame(np.random.rand(140, 4), columns=['A', 'B', 'C', 'D'])

df['models'] = pd.Series(np.repeat(['model1','model2', 'model3', 'model4',     'model5', 'model6', 'model7'], 20))

bp_dict = df.boxplot(
by="models",layout=(2,2),figsize=(6,4),
return_type='both',
patch_artist = True,
)

colors = ['b', 'y', 'm', 'c', 'g', 'b', 'r', 'k', ]
for row_key, (ax,row) in bp_dict.iteritems():
    ax.set_xlabel('')
    for i,box in enumerate(row['boxes']):
        box.set_facecolor(colors[i])

plt.show()

以下是上述代码的输出:enter image description here

我试图为每个子图设置单独的x和y轴... enter image description here

python pandas dataframe matplotlib boxplot
3个回答
2
投票

您需要事先创建图形和子图,并将其作为参数传递给df.boxplot()。这也意味着您可以删除参数layout=(2,2)

fig, axes = plt.subplots(2,2,sharex=False,sharey=False)

然后使用:

bp_dict = df.boxplot(
by="models", ax=axes, figsize=(6,4),
return_type='both',
patch_artist = True,
)

1
投票

您可以再次设置可见标签,例如通过

plt.setp(ax.get_xticklabels(), visible=True)

这不会使轴独立,但它们仍然相互绑定,但似乎你在询问可见性,而不是这里的共享行为。


0
投票

如果你真的认为有必要在创建boxplot阵列之后取消共享轴,你可以这样做,但你必须“手动”做所有事情。通过stackoverflow搜索一段时间并查看matplotlib文档页面我提出了以下解决方案来取消共享yaxes实例的Axes,对于xaxes,你必须类似地:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
from matplotlib.ticker import AutoLocator, AutoMinorLocator

##using differently scaled data for the different random series:
df = pd.DataFrame(
    np.asarray([
        np.random.rand(140),
        2*np.random.rand(140),
        4*np.random.rand(140),
        8*np.random.rand(140),
    ]).T,
    columns=['A', 'B', 'C', 'D']
)

df['models'] = pd.Series(np.repeat([
    'model1','model2', 'model3', 'model4',   'model5', 'model6', 'model7'
], 20))

##creating the boxplot array:
bp_dict = df.boxplot(
    by="models",layout = (2,2),figsize=(6,8),
    return_type='both',
    patch_artist = True,
    rot = 45,
)

colors = ['b', 'y', 'm', 'c', 'g', 'b', 'r', 'k', ]

##adjusting the Axes instances to your needs
for row_key, (ax,row) in bp_dict.items():
    ax.set_xlabel('')

    ##removing shared axes:
    grouper = ax.get_shared_y_axes()
    shared_ys = [a for a in grouper]
    for ax_list in shared_ys:
        for ax2 in ax_list:
            grouper.remove(ax2)

    ##setting limits:
    ax.axis('auto')
    ax.relim()      #<-- maybe not necessary

    ##adjusting tick positions:
    ax.yaxis.set_major_locator(AutoLocator())
    ax.yaxis.set_minor_locator(AutoMinorLocator())

    ##making tick labels visible:    
    plt.setp(ax.get_yticklabels(), visible=True)

    for i,box in enumerate(row['boxes']):
        box.set_facecolor(colors[i])

plt.show()

结果图如下所示:

result of the above code

说明:

您首先需要告诉每个Axes实例它不应该与任何其他yaxis实例共享其AxisThis post让我进入了如何做到这一点的方向 - Axes.get_shared_y_axes()返回一个Grouper object,其中包含所有其他Axes实例的引用,当前Axes应与其分享xaxis。通过这些实例循环并调用Grouper.remove实际上是不共享的。

一旦yaxis不共享,y限制和y蜱需要调整。前者可以通过ax.axis('auto')ax.relim()实现(不确定是否需要第二个命令)。可以使用ax.yaxis.set_major_locator()ax.yaxis.set_minor_locator()以及适当的Locators来调整蜱。最后,使用plt.setp(ax.get_yticklabels(), visible=True)see here)可以看到刻度标签。

考虑到这一切,@ DavidG的answer在我看来是更好的方法。

© www.soinside.com 2019 - 2024. All rights reserved.