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...
or the legend in the second image
-
4Please provide the relevant code that constructs these plots. – famousgarkin Jul 16 '14 at 20:50
1 Answers
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)

- 46,693
- 16
- 125
- 127
-
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! @mwaskom – MYjx Jul 17 '14 at 15:35
-
1Are you plotting in an IPython notebook? If so, this all has to happen in the same cell. That might be the problem. – mwaskom Jul 18 '14 at 00:26
-
@mwaskom 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. – Ian Fiddes Apr 27 '16 at 20:54
-
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. – joelostblom Jul 06 '16 at 18:55