我猜这是因为FactorPlot本身使用了subplot。
编辑于2019年3月10日18:43 GMT:从seaborn的categorical.py源代码{{link1}}可以确认:catplot(和factorplot)使用了matplotlib的subplot。@Jojo的答案完美地解释了发生了什么。
def catplot(x=None, y=None, hue=None, data=None, row=None, col=None,
col_wrap=None, estimator=np.mean, ci=95, n_boot=1000,
units=None, order=None, hue_order=None, row_order=None,
col_order=None, kind="strip", height=5, aspect=1,
orient=None, color=None, palette=None,
legend=True, legend_out=True, sharex=True, sharey=True,
margin_titles=False, facet_kws=None, **kwargs):
...
g = FacetGrid(**facet_kws)
并且包含FacetGrid定义的axisgrid.py源代码:
class FacetGrid(Grid):
def __init(...):
...
subplot_kws = {} if subplot_kws is None else subplot_kws.copy()
gridspec_kws = {} if gridspec_kws is None else gridspec_kws.copy()
fig, axes = plt.subplots(nrow, ncol, **kwargs)
所以,是的,你在不知不觉中创建了很多子图,并且用`ax = ...`参数搞乱了它们。
@Jojo是正确的。
以下是一些其他选项:
选项1
![输入图像描述](https://istack.dev59.com/8cwQ5.webp)
选项2
![输入图像描述](https://istack.dev59.com/Ekdkj.webp)
请注意,factorplot在更高版本的seaborn中已被弃用。
import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
print(pd.__version__)
print(sns.__version__)
print(matplotlib.__version__)
n = pd.DataFrame(
{'borough': {0: 'Bronx', 1: 'Brooklyn', 2: 'EWR', 3: 'Manhattan', 4: 'Queens', 5: 'Staten Island', 6: 'Unknown'},
'kind': {0: 'n', 1: 'n', 2: 'n', 3: 'n', 4: 'n', 5: 'n', 6: 'n'},
'pickups': {0: 50.66705042597283, 1: 534.4312687082662, 2: 0.02417683628827999, 3: 2387.253281142068,
4: 309.35482385447847, 5: 1.6018880957863229, 6: 2.0571804140650674}})
low_pickups = pd.DataFrame({'borough': {2: 'EWR', 5: 'Staten Island', 6: 'Unknown'},
'kind': {0: 'low_pickups', 1: 'low_pickups', 2: 'low_pickups', 3: 'low_pickups',
4: 'low_pickups', 5: 'low_pickups', 6: 'low_pickups'},
'pickups': {2: 0.02417683628827999, 5: 1.6018880957863229, 6: 2.0571804140650674}})
new_df = n.append(low_pickups).dropna()
print(n)
print('--------------')
print(low_pickups)
print('--------------')
print(new_df)
g = sns.FacetGrid(data=new_df, col="kind", hue='kind', sharey=False)
g.map(sns.barplot, "borough", "pickups", order=sorted(new_df['borough'].unique()))
plt.show()
控制台输出:
0.24.1
0.9.0
3.0.2
borough kind pickups
0 Bronx n 50.667050
1 Brooklyn n 534.431269
2 EWR n 0.024177
3 Manhattan n 2387.253281
4 Queens n 309.354824
5 Staten Island n 1.601888
6 Unknown n 2.057180
--------------
borough kind pickups
0 NaN low_pickups NaN
1 NaN low_pickups NaN
2 EWR low_pickups 0.024177
3 NaN low_pickups NaN
4 NaN low_pickups NaN
5 Staten Island low_pickups 1.601888
6 Unknown low_pickups 2.057180
--------------
borough kind pickups
0 Bronx n 50.667050
1 Brooklyn n 534.431269
2 EWR n 0.024177
3 Manhattan n 2387.253281
4 Queens n 309.354824
5 Staten Island n 1.601888
6 Unknown n 2.057180
2 EWR low_pickups 0.024177
5 Staten Island low_pickups 1.601888
6 Unknown low_pickups 2.057180
或者尝试这个:
g = sns.barplot(data=new_df, x="kind", y="pickups", hue='borough')
g.set_yscale('log')
我必须使用对数刻度,因为数据值在一个巨大的范围内分布得非常广泛。您可以考虑使用分类(请参见pandas的cut函数)。
编辑于2019年3月10日18:43 GMT:正如@Jojo在他的答案中所述,最后一个选项确实是:
sns.catplot(data=new_df, x="borough", y="pickups", col='kind', hue='borough', sharey=False, kind='bar')
没有时间完成研究,所以所有的功劳都归他!
plt.close(2)
让我挑起了眉毛 :) - LoneWandererfactorplot
一次,而plt.close(2)
只是为了关闭此调用创建的图形。我建议的“快速解决方案”只是将此策略适应于当前情况的一种方法。 - j-i-lcatplot
时可以设置height
和aspect
。有关详细信息,请参见 此 SO 回答。 - j-i-l