Change the title of factor plot in seaborn
Asked Answered
M

1

6

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

Mcnally answered 16/7, 2014 at 20:5 Comment(1)
Please provide the relevant code that constructs these plots.Contain
S
12

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)
Spiers answered 16/7, 2014 at 21:35 Comment(4)
sorry for the late reply! Thank you so much for the answer. I was trying these codes but nothing happens. I first run the factor plot function then the title for loop nothing happened. so I rerun the factor plot function again, still nothing changed. Does that have anything to do with the order?Thanks! @SpiersMcnally
Are you plotting in an IPython notebook? If so, this all has to happen in the same cell. That might be the problem.Spiers
@Spiers Sorry for the thread necromancy, but I wanted to see if you had an answer for the other part of OP's question. Is it possible to adjust the column names that get placed in the legend, without changing the underlying dataframe? I want to be able to use unicode text in my column titles.Heartrending
For those interested in only keeping what is on the right side of the equal sign in the title (so removing "classifier = " in the OP's first figure), the relevant code is g.set_titles('{col_name}'), as detailed in the Seaborn FacetGrid documentation linked in the answer.Preiser

© 2022 - 2024 — McMap. All rights reserved.