I have following simple example dataframe:
import pandas as pd
data = [['Alex',25],['Bob',34],['Sofia',26],["Claire",35]]
df = pd.DataFrame(data,columns=['Name','Age'])
df["sex"]=["male","male","female","female"]
我使用以下代码绘制条形图:
import matplotlib.pyplot as plt
import seaborn as sns
age_plot=sns.barplot(data=df,x="Name",y="Age", hue="sex",dodge=False)
age_plot.get_legend().remove()
plt.setp(age_plot.get_xticklabels(), rotation=90)
plt.ylim(0,40)
age_plot.tick_params(labelsize=14)
age_plot.set_ylabel("Age",fontsize=15)
age_plot.set_xlabel("",fontsize=1)
plt.tight_layout()
生成以下条形图:
我的问题:如何控制条之间的空白?我想在男性(蓝色)和女性(橙色)条之间增加一些额外的空白。
输出应该像这样(在MS PPT
中编辑的不好看):
我发现了一些关于matplotplib
的主题(例如 https://python-graph-gallery.com/5-control-width-and-space-in-barplots/),但没有关于seaborn
的。 我更喜欢使用seaborn
,因为它可以很容易地通过hue
进行颜色功能。
谢谢。