Change the title of factor plot in seaborn

MYjx picture MYjx · Jul 16, 2014 · Viewed 8.6k times · Source

Does anyone know how to change the legend and the title in seaborn? See the below. I kind of want to change the name "Gaussia" to "Guassian Naive Bayes" etc...enter image description here
enter image description hereor the legend in the second image

Answer

mwaskom picture mwaskom · Jul 16, 2014

These values are just taken from the field in the input DataFrame that you use as the col or hue variable in the factorgrid plot. So the correct thing to do would be to set the values as you want them in the original DataFrame and then pass that to seaborn.factorplot.

Alternatively, once you have plotted, the function returns an object of class FacetGrid that has a method called set_titles. This allows you to change the titles after plotting more flexibly, but it also is fundamentally based on the values in the DataFrame you passed into the function. See the docstring of that method for more detal.

The final option is to set the titles manually using matplotlib commands. The FacetGrid object that gets returned also has an axes attribute, which is a 2 dimensional array of the maptlotlib Axes in the figure. You can loop through this and set the titles to whatever you want:

g = sns.factorplot(...)
titles = ["foo", "bar", "buz"]
for ax, title in zip(g.axes.flat, titles):
    ax.set_title(title)