Extra Plot when drawing Catplot

本秂侑毒 提交于 2020-06-01 05:24:27

问题


Using the following code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df1 = pd.DataFrame( {'category': {0: 'Cat', 1: 'Cat', 2: 'Cat', 3: 'Cat', 4: 'Dog', 5: 'Dog', 6: 'Dog', 7: 'Dog', 8: 'Dog', 9: 'Dog', 10: 'Dog', 11: 'Dog', 12: 'Dog', 13: 'Dog', 14: 'Dog', 15: 'Dog', 16: 'Dog', 17: 'Dog', 18: 'Dog', 19: 'Dog'}, 'shape': {0: 'Small', 1: 'Small', 2: 'Small', 3: 'Small', 4: 'Small', 5: 'Small', 6: 'Small', 7: 'Medium', 8: 'Medium', 9: 'Large', 10: 'Small', 11: 'Small', 12: 'Small', 13: 'Small', 14: 'Small', 15: 'Small', 16: 'Small', 17: 'Small', 18: 'Small', 19: 'Small'}} )
plt.figure(figsize=(5,5),facecolor='#ffffff')
ax = plt.axes(facecolor='#ffffff')
plt.xticks(rotation=25)
sns.catplot(x='shape', hue='category', kind ='count', data = df1, height=5, aspect=1,ax=ax)
plt.xlabel('Diamond Shape')
plt.show()

I got the following plot

Why there is an extra blank plot below? How to not generate that? (I need the ax to provide extra formatting for catplot)


回答1:


In this case, sns.catplot() runs on a FacetGrid which makes it a figure-level function see seaborn doc and issues raised in github it does not take in ax as argument and you can use plt to do the formatting:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(facecolor='#ffffff')
g = sns.catplot(x='shape', hue='category', kind ='count', data = df1, height=5, aspect=1)
plt.xticks(rotation=25)
plt.xlabel('Diamond Shape')




回答2:


You can use the following trick : Close the extra figure before plt.show()

plt.close(2)
plt.show()


来源:https://stackoverflow.com/questions/62103220/extra-plot-when-drawing-catplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!