本文介绍了 pandas 箱图中每个子图的独立轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码有助于获得带有唯一彩色框的子图.但是所有子图共享一个公共的x和y轴集.我期待每个子图具有独立的轴:
The below code helps in obtaining subplots with unique colored boxes. But all subplots share a common set of x and y axis. I was looking forward to having independent axis for each sub-plot:
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()
这是上面代码的输出:
Here is an output of the above code:
我正在尝试为每个子图分别设置x和y轴...
I am trying to have separate x and y axis for each subplot...
推荐答案
您需要先创建图形和子图,并将其作为参数传递给df.boxplot()
.这也意味着您可以删除参数layout=(2,2)
:
You need to create the figure and subplots before hand and pass this in as an argument to df.boxplot()
. This also means you can remove the argument 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,
)
这篇关于 pandas 箱图中每个子图的独立轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!