以下代码有助于获得具有独特彩色框的子图。但是所有子图共享一组共同的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()
您需要事先创建图形和子图,并将其作为参数传递给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,
)
您可以再次设置可见标签,例如通过
plt.setp(ax.get_xticklabels(), visible=True)
这不会使轴独立,但它们仍然相互绑定,但似乎你在询问可见性,而不是这里的共享行为。
如果你真的认为有必要在创建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()
结果图如下所示:
说明:
您首先需要告诉每个Axes
实例它不应该与任何其他yaxis
实例共享其Axis
。 This 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在我看来是更好的方法。