I've created this plot using Seaborn and a pandas dataframe (data
):
My code:
g = sns.lmplot('credibility', 'percentWatched', data=data, hue = 'millennial', markers = ["+", "."], x_jitter = True, y_jitter = True, size=5) g.set(xlabel = 'Credibility Ranking\n ← Low High →', ylabel = 'Percent of Video Watched [%]')
You may notice the plot's legend title is simply the variable name ('millennial') and the legend items are the variable's values (0, 1). How can I edit the legend's title and labels? Ideally, the legend's title would be 'Generation' and the labels would be "Millennial" and "Older Generations"
To change the position of a legend in a seaborn plot, you can use the plt. legend() command. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.
Use the set_xlabel() and set_ylabel() Functions to Set the Axis Labels in a Seaborn Plot. A seaborn plot returns a matplotlib axes instance type object. We can use the set_xlabel() and set_ylabel to set the x and y-axis label respectively. We can use the fontsize parameter to control the size of the font.
By default, seaborn automatically adds a legend to the graph. Notice the legend is at the top right corner. If we want to explicitly add a legend, we can use the legend() function from the matplotlib library.
Took me a while to read through the above. This was the answer for me:
import seaborn as sns import matplotlib.pyplot as plt tips = sns.load_dataset("tips") g = sns.lmplot( x="total_bill", y="tip", hue="smoker", data=tips, legend=False ) plt.legend(title='Smoker', loc='upper left', labels=['Hell Yeh', 'Nah Bruh']) plt.show(g)
Reference this for more arguments: matplotlib.pyplot.legend
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With