以下是我正在使用的代码。它是一个可以复制和粘贴的自包含代码。
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
### Load the Titanic dataset
titanic = sns.load_dataset("titanic")
### Divide the data by sex; male and female
male = titanic[titanic.sex == 'male']
female = titanic[titanic.sex == 'female']
### Now use groupby to group by fare//5 and then create a column with the survival rate for each group
male_chart= male.groupby([male.fare // 5]).survived.mean().reset_index()
female_chart= female.groupby([female.fare // 5]).survived.mean().reset_index()
#Plot male_chart
sns.set(style="whitegrid")
g = sns.factorplot(x='fare', y= 'survived', data=male_chart,
size=6, aspect =3 ,kind="bar", palette="muted")
g.despine(left=True)
g.set_ylabels("Survival Probability")
g.set_xlabels('Fare *5')
g.set_titles('Males') # set_title doesn't produce "Males" on the chart itself
#sns.plt.title('Males') #This code seems to work but not sure why it does.
#plot: female_chart
h= sns.factorplot(x='fare', y= 'survived', data=female_chart,
size=6, aspect =3 ,kind="bar", palette="muted")
h.despine(left=True)
h.set_ylabels("survival probability")
h.set_xlabels('fare *5')
h.set_titles('Females') #Once again doesn't work.
#sns.plt.title('Females') #but this works
plt.show()
我在运行" set_titles"时没有收到任何错误但它没有在图表上生成标题。 我如何错误地使用set_title?感谢你的帮助。